using FastReport.Web.Application.Localizations;

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using System.Globalization;

namespace FastReport.Web.Blazor.Components.Internal.ExportSettings
{
    public partial class XlsxExportSettings : ExportSettingsBase
    {
        XlsxExportSettingsLocalization localization;

        protected override void LocalizeForm()
        {
            var res = WebReport.Res;
            localization = new XlsxExportSettingsLocalization(res);
        }

        public double FontScale
        {
            get => GetFontScale();
            set => SetFontScale(value);
        }

        protected override void OnInitialized()
        {
            base.OnInitialized();
            
            ExportParameters["PrintFit"] = "NoScaling";
            ExportParameters["Wysiwyg"] = "true";
            ExportParameters["PrintOptimized"] = "true";
            ExportParameters["FontScale"] = "1";
        }

        protected double GetFontScale()
        {
            if (ExportParameters.TryGetValue("FontScale", out string value))
            {
                // Console.WriteLine("GetFontScale = " + value);
                if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out double parsedVal))
                {
                    return parsedVal;
                }
                else
                    return 0;
            }
            else
                return 0;
        }

        protected void SetFontScale(double newScale)
        {
            // Console.WriteLine("newScale = " + newScale);
            ExportParameters["FontScale"] = newScale.ToString(CultureInfo.InvariantCulture);
        }



        void OnWysiwygClick(MouseEventArgs e)
        {
            ChangeBoolValue("Wysiwyg");
        }

        void OnPageBreaksClick(MouseEventArgs e)
        {
            ChangeBoolValue("PageBreaks");
        }

        void OnDataOnlyClick(MouseEventArgs e)
        {
            ChangeBoolValue("DataOnly");
        }

        void OnSeemlessTableClick(MouseEventArgs e)
        {
            ChangeBoolValue("Seamless");
        }

        void OnPrintOptimizedClick(MouseEventArgs e)
        {
            ChangeBoolValue("PrintOptimized");
        }

        void OnSplitPagesClick(MouseEventArgs e)
        {
            ChangeBoolValue("SplitPages");
        }

        void OnPrintFitChange(ChangeEventArgs e)
        {
            if ((e.Value as string) == "NoScaling")
            {
                ExportParameters["PrintFit"] = "NoScaling";
            }
            else if ((e.Value as string) == "FitSheetOnOnePage")
            {
                ExportParameters["PrintFit"] = "FitSheetOnOnePage";
            }
            else if ((e.Value as string) == "FitAllColumsOnOnePage")
            { 
                ExportParameters["PrintFit"] = "FitAllColumsOnOnePage";
            }
            else if ((e.Value as string) == "FitAllRowsOnOnePage")
            {
                ExportParameters["PrintFit"] = "FitAllRowsOnOnePage";
            }
        }

    }
}
