using System;
using System.Globalization;
using System.Windows.Forms;
using FastReport.Export;
using FastReport.Export.OoXML;
using FastReport.Utils;

namespace FastReport.Forms
{
    /// <summary>
    /// Form for <see cref="Word2007Export"/>.
    /// For internal use only.
    /// </summary>
    public partial class Word2007ExportForm : BaseExportForm
    {
        /// <inheritdoc/>
        public override void Init(ExportBase export)
        {
            base.Init(export);
            MyRes res = new MyRes("Export,Docx");
            Text = res.Get("");
            Word2007Export ooxmlExport = Export as Word2007Export;
            radioButtonTable.Checked = ooxmlExport.MatrixBased;
            radioButtonParagraph.Checked = ooxmlExport.ParagraphBased;
            radioButtonLayers.Checked = !ooxmlExport.MatrixBased;
            cbDisableSectionBreak.Checked = ooxmlExport.DisableSectionBreak;
            cbbLocalization.SelectedItem = ooxmlExport.Locale;
            cbWysiwyg.Checked = ooxmlExport.Wysiwyg;
            cbRh.SelectedIndex = ooxmlExport.RowHeight == Word2007Export.RowHeightType.Exactly ? 0 : 1;
            cbPrintOptimized.Checked = ooxmlExport.PrintOptimized;
            cbDoNotExpandShiftReturn.Checked = ooxmlExport.DoNotExpandShiftReturn;
            cbbLocalization.Enabled = ooxmlExport.ExportLocale;
            cbLocalization.Checked = ooxmlExport.ExportLocale;

            cbbLocalization.Items.Add(Res.Get("Export,Misc,UseDesigerLocale"));
            foreach (var lang in  CultureInfo.GetCultures(CultureTypes.AllCultures))
                cbbLocalization.Items.Add(lang.DisplayName);

            if(ooxmlExport.Locale != null)
                cbbLocalization.SelectedItem = ooxmlExport.Locale.DisplayName;
            else
                cbbLocalization.SelectedIndex = 0;
        }
        
        /// <inheritdoc/>
        protected override void Done()
        {
            base.Done();
            Word2007Export ooxmlExport = Export as Word2007Export;
            ooxmlExport.MatrixBased = radioButtonTable.Checked || radioButtonParagraph.Checked;
            ooxmlExport.ParagraphBased = radioButtonParagraph.Checked;
            ooxmlExport.Wysiwyg = cbWysiwyg.Checked;
            ooxmlExport.DisableSectionBreak = cbDisableSectionBreak.Checked;
            ooxmlExport.PrintOptimized = cbPrintOptimized.Checked;
            ooxmlExport.ExportLocale = cbLocalization.Checked;

            if (cbbLocalization.SelectedIndex == -1 || cbbLocalization.SelectedIndex == 0)
                ooxmlExport.Locale = null;
            else
                ooxmlExport.Locale = CultureInfo.GetCultures(CultureTypes.AllCultures)[cbbLocalization.SelectedIndex - 1];

            if (cbWysiwyg.Checked == true && radioButtonTable.Checked == true)
            {
                MyRes res = new MyRes("Export,Docx");
                if (cbRh.Text == res.Get("Exactly"))
                    ooxmlExport.RowHeight = Word2007Export.RowHeightType.Exactly;
                else if (cbRh.Text == res.Get("Minimum"))
                    ooxmlExport.RowHeight = Word2007Export.RowHeightType.Minimum;
            }
            ooxmlExport.DoNotExpandShiftReturn = cbDoNotExpandShiftReturn.Checked;
        }

        private void CbLocalization_CheckedChanged(object sender, System.EventArgs e)
        {
            cbbLocalization.Enabled = cbLocalization.Checked;
        }

        /// <inheritdoc/>
        public override void Localize()
        {
            base.Localize();
            MyRes res = new MyRes("Export,Misc");
            gbOptions.Text = res.Get("Options");
            radioButtonTable.Text = res.Get("TableBased");
            radioButtonLayers.Text = res.Get("LayerBased");
            radioButtonParagraph.Text = res.Get("ParagraphBased");
            cbWysiwyg.Text = res.Get("Wysiwyg");
            cbLocalization.Text = res.Get("Localization");
            cbPrintOptimized.Text = res.Get("PrintOptimized");
            res = new MyRes("Export,Docx");
            label1.Text = res.Get("RowHeight");
            cbRh.Items[0] = res.Get("Exactly");
            cbRh.Items[1] = res.Get("Minimum");
            cbDoNotExpandShiftReturn.Text = res.Get("DoNotExpandShiftReturn");
            cbDisableSectionBreak.Text = res.Get("DisableSectionBreak");
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Word2007ExportForm"/> class.
        /// </summary>
        public Word2007ExportForm()
        {
            InitializeComponent();
        }

        private void cbWysiwyg_CheckedChanged(object sender, EventArgs e)
        {
            VisHlr();
        }

        private void radioButtonTable_CheckedChanged(object sender, EventArgs e)
        {
            VisHlr();
        }

        void VisHlr()
        {
            /*
            if (cbWysiwyg.Checked == true && radioButtonTable.Checked == true)
            {
                cbRh.Visible = true; label1.Visible = true;
            }
            else
            {
                cbRh.Visible = false; label1.Visible = false;
            }
            */
        }

    }
}

