using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FastReport.Web.Application.Localizations;

namespace FastReport.Web.Blazor.Components.Internal.ExportSettings
{
    public partial class PdfExportSettings : ExportSettingsBase
    {

        PdfExportSettingsLocalization localization;

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

        protected override void OnInitialized()
        {
            base.OnInitialized();
            ExportParameters["PdfCompliance"] = "None";
            ExportParameters["ColorSpace"] = "RGB";
            ExportParameters["JpegQuality"] = "90";
        }

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

        void OnEmbeddedFontsClick(MouseEventArgs e)
        {
            ChangeBoolValue("EmbeddingFonts");
        }

        void OnTextInCurvesClick(MouseEventArgs e)
        {
            ChangeBoolValue("TextInCurves");
        }

        void OnInteractiveClick(MouseEventArgs e)
        {
            ChangeBoolValue("InteractiveForms");
        }

        void OnBackgroundClick(MouseEventArgs e)
        {
            ChangeBoolValue("Background");
        }


        void OnImageOptionChange(ChangeEventArgs e)
        {
            if ((e.Value as string) == "PrintOptimized")
            {
                ExportParameters["PrintOptimized"] = "true";
                ExportParameters["ImagesOriginalResolution"] = "false";
            }
            else if ((e.Value as string) == "ImagesOriginalResolution")
            {
                ExportParameters["PrintOptimized"] = "false";
                ExportParameters["ImagesOriginalResolution"] = "true";
            }
            else
            {
                ExportParameters.Remove("PrintOptimized");
                ExportParameters.Remove("ImagesOriginalResolution");
            }
        }


        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();
        }
        

        protected bool CompressionIsDisabled()
        {
            if (ExportParameters.TryGetValue("ImagesOriginalResolution", out string resolution))
            {
                if (resolution == "true")
                    return true;
                else
                    return false;
            }
            else
                return false;
        }
    }
}
