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

namespace FastReport.Forms
{
    /// <summary>
    /// Form for <see cref="Excel2007Export"/>.
    /// For internal use only.
    /// </summary>
    public partial class Excel2007ExportForm : BaseExportForm
    {
        /// <inheritdoc/>
        public override void Init(ExportBase export)
        {
            base.Init(export);
            MyRes res = new MyRes("Export,Xlsx");
            Text = res.Get("");
            Excel2007Export ooxmlExport = Export as Excel2007Export;
            cbWysiwyg.Checked = ooxmlExport.Wysiwyg;
            cbPageBreaks.Checked = ooxmlExport.PageBreaks;
            cbDataOnly.Checked = ooxmlExport.DataOnly;
            cbSeamless.Checked = ooxmlExport.Seamless;
            cbPrintOptimized.Checked = ooxmlExport.PrintOptimized;
            cbGroupOnSeparateSheet.Checked = ooxmlExport.GroupOnSeparateSheet;
            cbSplitPages.Checked = ooxmlExport.SplitPages;
            nudFontScale.Value = (decimal)ooxmlExport.FontScale;
            rbOnlyMove.Checked = true;
            rbSetPresetScaling.Checked = ooxmlExport.UsePresetScale;
            rbSetScaling.Checked = !ooxmlExport.UsePresetScale;
            cbPrintScaling.Enabled = ooxmlExport.UsePresetScale;
            nudPrintScaling.Enabled = !ooxmlExport.UsePresetScale;
            nudPrintScaling.Value = ooxmlExport.PrintScale;
            rbOtherSelection.Checked = ooxmlExport.PinX > 0 && ooxmlExport.PinY > 0;
            rbFirstColumn.Checked = !rbOtherSelection.Checked && ooxmlExport.PinX > 0;
            rbFirstLine.Checked = !rbOtherSelection.Checked && ooxmlExport.PinY > 0;
            lbPinX.Enabled = rbOtherSelection.Checked;
            lbPinY.Enabled = rbOtherSelection.Checked;
            nudPinX.Enabled = rbOtherSelection.Checked;
            nudPinY.Enabled = rbOtherSelection.Checked;
            cbUseLocaleFormating.Checked = ooxmlExport.UseLocaleFormating;
            cbCurrencyToAccounting.Checked = ooxmlExport.CurrencyToAccounting;
            cbShowGridlines.Checked = ooxmlExport.ShowGridlines;
            rbWithoutPinning.Checked = ooxmlExport.PinX == 0 && ooxmlExport.PinY == 0;
            if (rbOtherSelection.Checked)
            {
                nudPinX.Value = ooxmlExport.PinX;
                nudPinY.Value = ooxmlExport.PinY;
            }

            switch (ooxmlExport.PrintFit)
            {
                case Excel2007Export.PrintFitMode.NoScaling:
                    cbPrintScaling.SelectedIndex = 0;
                    break;
                case Excel2007Export.PrintFitMode.FitSheetOnOnePage:
                    cbPrintScaling.SelectedIndex = 1;
                    break;
                case Excel2007Export.PrintFitMode.FitAllColumsOnOnePage:
                    cbPrintScaling.SelectedIndex = 2;
                    break;
                case Excel2007Export.PrintFitMode.FitAllRowsOnOnePage:
                    cbPrintScaling.SelectedIndex = 3;
                    break;
            }

            switch (ooxmlExport.PictureProperties)
            {
                case Excel2007Export.ePictureProperties.MoveAndModify:
                    rbMoveAndModify.Checked = true;
                    break;
                case Excel2007Export.ePictureProperties.OnlyMove:
                    rbOnlyMove.Checked = true;
                    break;
                case Excel2007Export.ePictureProperties.Unchangeable:
                    rbUnchangeable.Checked = true;
                    break;
            }
        }
        
        /// <inheritdoc/>
        protected override void Done()
        {
            base.Done();
            Excel2007Export ooxmlExport = Export as Excel2007Export;
            ooxmlExport.Wysiwyg = cbWysiwyg.Checked;
            ooxmlExport.PageBreaks = cbPageBreaks.Checked;
            ooxmlExport.DataOnly = cbDataOnly.Checked;
            ooxmlExport.Seamless = cbSeamless.Checked;
            ooxmlExport.PrintOptimized = cbPrintOptimized.Checked;
            ooxmlExport.SplitPages = cbSplitPages.Checked;
            ooxmlExport.FontScale = (float)nudFontScale.Value;
            ooxmlExport.GroupOnSeparateSheet = cbGroupOnSeparateSheet.Checked;
            ooxmlExport.UsePresetScale = rbSetPresetScaling.Checked;
            ooxmlExport.PrintScale = (int)nudPrintScaling.Value;
            ooxmlExport.UseLocaleFormating = cbUseLocaleFormating.Checked;
            ooxmlExport.CurrencyToAccounting = cbCurrencyToAccounting.Checked;
            ooxmlExport.UseFileStream = cbUseFileStream.Checked;
            ooxmlExport.PinY = (int)(rbOtherSelection.Checked ? nudPinY.Value : rbFirstLine.Checked ? 1 : 0);
            ooxmlExport.PinX = (int)(rbOtherSelection.Checked ? nudPinX.Value : rbFirstColumn.Checked ? 1 : 0);
            ooxmlExport.ShowGridlines = cbShowGridlines.Checked;
            switch (cbPrintScaling.SelectedIndex)
            {
                case 0:
                    ooxmlExport.PrintFit = Excel2007Export.PrintFitMode.NoScaling;
                    break;
                case 1:
                    ooxmlExport.PrintFit = Excel2007Export.PrintFitMode.FitSheetOnOnePage;
                    break;
                case 2:
                    ooxmlExport.PrintFit = Excel2007Export.PrintFitMode.FitAllColumsOnOnePage;
                    break;
                case 3:
                    ooxmlExport.PrintFit = Excel2007Export.PrintFitMode.FitAllRowsOnOnePage;
                    break;
            }
            if (rbMoveAndModify.Checked)
                ooxmlExport.PictureProperties = Excel2007Export.ePictureProperties.MoveAndModify;
            else if (rbOnlyMove.Checked)
                ooxmlExport.PictureProperties = Excel2007Export.ePictureProperties.OnlyMove;
            else
                ooxmlExport.PictureProperties = Excel2007Export.ePictureProperties.Unchangeable;
        }

        private void RbSetPresetScaling_CheckedChanged(object sender, System.EventArgs e)
        {
            nudPrintScaling.Enabled = !(sender as RadioButton).Checked;
            cbPrintScaling.Enabled = (sender as RadioButton).Checked;
        }

        private void CbGroupOnSeparateSheet_CheckedChanged(object sender, System.EventArgs e)
        {
            cbSplitPages.Enabled = !(sender as CheckBox).Checked;
        }

        private void CbSplitPages_CheckedChanged(object sender, System.EventArgs e)
        {
            cbGroupOnSeparateSheet.Enabled = !(sender as CheckBox).Checked;
        }

        private void RbOtherSelection_CheckedChanged(object sender, System.EventArgs e)
        {
            bool check = (sender as RadioButton).Checked;
            lbPinX.Enabled = check;
            lbPinY.Enabled = check;
            nudPinX.Enabled = check;
            nudPinY.Enabled = check;
        }

        /// <inheritdoc/>
        public override void Localize()
        {
            base.Localize();
            MyRes res = new MyRes("Export,Misc");
            gbOptions.Text = res.Get("Options");
            cbWysiwyg.Text = res.Get("Wysiwyg");
            cbUseFileStream.Text = res.Get("UseFileStream");
            cbPageBreaks.Text = res.Get("PageBreaks");
            cbSeamless.Text = res.Get("Seamless");
            cbDataOnly.Text = Res.Get("Export,Csv,DataOnly");
            cbPrintOptimized.Text = res.Get("PrintOptimized");
            cbSplitPages.Text = res.Get("SplitPages");
            cbGroupOnSeparateSheet.Text = Res.Get("Export,Xlsx,GroupOnSeparateSheet");
            lblFontScale.Text = Res.Get("Export,Xlsx,FontScale");
            gbPrintScaling.Text = Res.Get("Export,Xlsx,PrintScaling");
            cbPrintScaling.Items.Add(Res.Get("Export,Xlsx,NoScaling"));
            cbPrintScaling.Items.Add(Res.Get("Export,Xlsx,FitSheetOnOnePage"));
            cbPrintScaling.Items.Add(Res.Get("Export,Xlsx,FitAllColumsOnOnePage"));
            cbPrintScaling.Items.Add(Res.Get("Export,Xlsx,FitAllRowsOnOnePage"));
            gbPicturePropreties.Text = Res.Get("Export,Xlsx,PictureProperties");
            rbMoveAndModify.Text = Res.Get("Export,Xlsx,MoveAndModify");
            rbOnlyMove.Text = Res.Get("Export,Xlsx,MoveOnly");
            rbUnchangeable.Text = Res.Get("Export,Xlsx,Unchangeable");
            rbSetScaling.Text = Res.Get("Export,Xlsx,ScaleInPercent");
            gbPinnedCell.Text = Res.Get("Export,Xlsx,PinnedCells");
            rbWithoutPinning.Text = Res.Get("Export,Xlsx,WithoutPinning");
            rbFirstColumn.Text = Res.Get("Export,Xlsx,FirstColumn");
            rbFirstLine.Text = Res.Get("Export,Xlsx,FirstLine");
            rbOtherSelection.Text = Res.Get("Export,Xlsx,OtherPinning");
            lbPinX.Text = Res.Get("Export,Xlsx,Columns");
            lbPinY.Text = Res.Get("Export,Xlsx,Rows");
            panPages.Text = Res.Get("Export,Xlsx,Basic");
            pageOther.Text = Res.Get("Export,Xlsx,Other");
            cbShowGridlines.Text = Res.Get("Export,Xlsx,ShowGridlines");
            gbOtherOptions.Text = res.Get("Options");
            cbUseLocaleFormating.Text = Res.Get("Export,Xlsx,UseLocaleFormating");
            cbCurrencyToAccounting.Text = res.Get("Export,Xlsx,CurrencyToAccounting");
        }

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

