using AntDesign;

using Blazor.ECharts.Options;
using Blazor.ECharts.Options.Enum;
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;
using wispro.sp.web.Services;
using P = Blazor.ECharts.Options.Series.Pie;

namespace wispro.sp.web.Pages.Report
{
    public partial class PieChart
    {
        private int iType = 1;
        private DateTime? start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        private DateTime? end = DateTime.Now;
        Blazor.ECharts.Components.EPie pieChart;

        internal class TJType
        {
            public string Name { get; set; }

            public int value { get; set; }
        }
        List<TJType> Types = new List<TJType>
        {
            new TJType { value  = 0,Name = "申诉人统计" },
            new TJType { value  = 1,Name = "申诉类型统计" },
            new TJType { value = 2, Name = "部门申诉统计" },
            //new TJType { value = 3, Name = "申诉人每月申诉统计" },
        };

        private EChartsOption<P.Pie> Option1;


        share.ChartDatas Datas=new share.ChartDatas() { 
            Title ="初始化标题",
            Datas = new List<share.ChartData>() { 
                new share.ChartData(){ type ="A", value =1, year="", CustomerType ="", CustomerYear=""},
                new share.ChartData(){ type ="B",value =5, year="",CustomerType ="", CustomerYear=""}
            },
            ATitle ="ATitle",
            BTitle="",
        };

        List<object> ShowData = new List<object>()
        {
            new {type="A",value=1},
            new {type="B",value=5}
        };
        

        [Inject] ReportService reportService { get; set; }

        [Inject] MessageService msgService { get; set; }


        [Inject] protected IAuthService _authService { get; set; }

        [Obsolete]
        protected async override Task OnInitializedAsync()
        {
            await base.OnInitializedAsync();

            await _authService.CanVisitResource();

            await OnButtonClick();
            //Datas = await reportService.GetAppealReportData(1, DateTime.Parse("2022-02-01"), null, null);

            //if (Datas != null)
            //{
            //    Option1 = GetPieOption(Datas);
            //    pieChart.Refresh();
            //    StateHasChanged();
            //}

            
        }

        private EChartsOption<P.Pie> GetPieOption(share.ChartDatas datas)
        {
            List<object> seriesData = new List<object>();

            foreach(var data in datas.Datas)
            {
                seriesData.Add(new { Name = data.type, Value = data.value });
            }

            return Option1 = new()
            {
                Toolbox = new() { 
                    Show= true,
                    Feature=new()
                    {
                        SaveAsImage = new()
                        {
                            PixelRatio =2,
                            Name="保存图片"
                        }
                    }
                },
                Title= new Blazor.ECharts.Options.Title()
                {
                    Text = datas.Title ,
                    Left = "center"
                },
                Tooltip = new()
                {
                    Trigger = TooltipTrigger.Item,
                    Formatter = "{a} <br/>{b}: {c} ({d}%)"
                },
                Legend = new()
                {
                    Show = false,
                    Orient = Orient.Vertical,
                    Left = 10,
                    Data = datas.GetSeries()
                },
                Series = new()
                {
                    new P.Pie()
                    {
                        Name = datas.Title,
                        Radius = new[] { "50%", "70%" },
                        AvoidLabelOverlap = false,
                        Label = new()
                        {
                            Show = true,
                            Formatter= "{b}\r\n{c} ({d}%)"
                        },
                        //LabelLine = new() { Show = true },
                        Data = seriesData
                    }
                }
            };
        }

        private void OnTimeRangeChange(DateRangeChangedEventArgs args)
        {
            msgService.Info($"Selected Time: {JsonSerializer.Serialize(args.Dates)}");
            
            if (args.Dates?.Length > 0){
                start = args.Dates[0];
            }

            if (args.Dates?.Length > 1)
            {
                end = args.Dates[1];
            }
            
        }

        private bool _noIconLoading;
        private async Task OnButtonClick()
        {
            _noIconLoading = true;
            Datas = await reportService.GetAppealReportData(iType, start, end, null);

            if (Datas != null)
            {
                Option1 = GetPieOption(Datas);
                pieChart.Refresh();
                StateHasChanged();
            }
            _noIconLoading = false;
        }
    }
}
