using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using FastReport.Web.Application.Localizations;

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

        protected override void OnInitialized()
        {
            base.OnInitialized();
            ExportParameters["ImageFormat"] = "Jpeg";
            ExportParameters["JpegQuality"] = "90";
            ExportParameters["ResolutionX"] = "96";
            ExportParameters["ResolutionY"] = "96";
        }

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

        public int ResolutionX
        {
            get => GetResolutionX();
            set
            {
                SetResolutionX(value);
                if (ResolutionYIsDisabled())
                {
                    SetResolutionY(value);
                }
            }
        }

        public int ResolutionY
        {
            get => GetResolutionY();
            set => SetResolutionY(value);
        }

        public double JpegQuality
        {
            get => GetJpegQuality();
            set => SetJpegQuality(value);
        }

        #region Disabled

        protected bool CompressionIsDisabled()
        {
            if (ExportParameters.TryGetValue("ImageFormat", out string format))
            {
                if (format != "Jpeg")
                    return true;
                else
                    return false;
            }
            else
                return false;
        }

        protected bool ResolutionYIsDisabled()
        {
            if (ExportParameters.TryGetValue("ImageFormat", out string format))
            {
                if (format != "Tiff")
                    return true;
                else
                    return false;
            }
            else
                return false;
        }

        protected bool MultiFrameTiffIsDisabled()
        {
            if (ExportParameters.TryGetValue("ImageFormat", out string format))
            {
                if (format != "Tiff")
                    return true;
                else
                    return false;
            }
            else
                return false;
        }

        protected bool MonochromeTiffIsDisabled()
        {
            if (ExportParameters.TryGetValue("ImageFormat", out string format))
            {
                if (format != "Tiff")
                    return true;
                else
                    return false;
            }
            else
                return false;
        }

        #endregion Disabled

        #region Getters-Setter

        protected int GetResolutionX()
        {
            if (ExportParameters.TryGetValue("ResolutionX", out string value))
            {
                if (int.TryParse(value, out int parsedVal))
                {
                    return parsedVal;
                }
                else
                    return 0;
            }
            else
                return 0;
        }
        protected void SetResolutionX(double newCompression)
        {
            ExportParameters["ResolutionX"] = newCompression.ToString();
        }

        protected int GetResolutionY()
        {
            if (ExportParameters.TryGetValue("ResolutionY", out string value))
            {
                if (int.TryParse(value, out int parsedVal))
                {
                    return parsedVal;
                }
                else
                    return 0;
            }
            else
                return 0;
        }
        protected void SetResolutionY(double newCompression)
        {
            ExportParameters["ResolutionY"] = newCompression.ToString();
        }

        protected double GetJpegQuality()
        {
            if (ExportParameters.TryGetValue("JpegQuality", out string value))
            {
                if (double.TryParse(value, out double parsedVal))
                {
                    return parsedVal;
                }
                else
                    return 0;
            }
            else
                return 0;
        }

        protected void SetJpegQuality(double newCompression)
        {
            ExportParameters["JpegQuality"] = newCompression.ToString();
        }

        #endregion Getters-Setter

        void OnMultiFrameTiffClick(MouseEventArgs e)
        {
            ChangeBoolValue("MultiFrameTiff");
        }

        void OnMonochromeTiffClick(MouseEventArgs e)
        {
            ChangeBoolValue("MonochromeTiff");
        }

        void OnSeparateFilesClick(MouseEventArgs e)
        {
            ChangeBoolValue("SeparateFiles");
        }


        void OnImageFormatChange(ChangeEventArgs e)
        {
            if ((e.Value as string) == "Bmp")
            {
                ExportParameters["ImageFormat"] = "Bmp";
                ExportParameters.Remove("ResolutionY");
            }
            else if ((e.Value as string) == "Png")
            {
                ExportParameters["ImageFormat"] = "Png";
                ExportParameters.Remove("ResolutionY");
            }
            else if ((e.Value as string) == "Jpeg")
            {
                ExportParameters["ImageFormat"] = "Jpeg";
                ExportParameters.Remove("ResolutionY");
            }
            else if ((e.Value as string) == "Gif")
            {
                ExportParameters["ImageFormat"] = "Gif";
                ExportParameters.Remove("ResolutionY");
            }
            else if ((e.Value as string) == "Tiff")
            {
                ExportParameters["ImageFormat"] = "Tiff";
                ExportParameters["ResolutionY"] = "96";
            }
            else if ((e.Value as string) == "Metafile")
            {
                ExportParameters["ImageFormat"] = "Metafile";
                ExportParameters.Remove("ResolutionY");
            }

            //_ = FileEndChanged.InvokeAsync(FileEnd);
        }
    }
}
