using AntDesign.TableModels;
using Microsoft.AspNetCore.Components.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using wispro.sp.entity;
using wispro.sp.share;
using wispro.sp.web.Auth;

namespace wispro.sp.web.Services
{
    public class PerformanceItemServices
    {
        private readonly IHttpService _httpClient;
        private readonly JwtAuthenticationStateProvider _jwt;

        

        public PerformanceItemServices(IHttpService httpClient, AuthenticationStateProvider jwt)
        {
            _httpClient = httpClient;
            _jwt = (JwtAuthenticationStateProvider)jwt;
        }

        public async Task<ListApiResponse<PerformanceItem>> GetItems(int _pageIndex,int _pageSize)
        {
            ListApiResponse<PerformanceItem> data = await _httpClient.Get<ListApiResponse<PerformanceItem>>($"PerformanceItem/Query?pageIndex={_pageIndex}&pageSize={_pageSize}");
            return data;

        }

        public async Task<ApiSaveResponse> SaveFieldChange(int id,string Field,string value)
        {
            ApiSaveResponse data = await _httpClient.Get<ApiSaveResponse>($"PerformanceItem/UpdateFieldValue?id={id}&field={Field}&value={value}");
            return data;
        }

        public async Task<PerformanceItem> GetPerformancItem(int Id)
        {
            PerformanceItem data = await _httpClient.Get<PerformanceItem>($"PerformanceItem/Get?Id={Id}");
            return data;
        }

        public async Task<ListApiResponse<PerformanceItem>> GetMyList(int userid,jxType type,int pageIndex=1,int pageSize=5)
        {
            ListApiResponse<PerformanceItem> data = await _httpClient.Get<ListApiResponse<PerformanceItem>>($"PerformanceItem/GetMyList?userid={userid}&Type={Convert.ToInt32(type)}&pageIndex={pageIndex}&pageSize={pageSize}");
            return data;
        }

        
        public async Task<FileProcessTask> ExportData(int calMonthId)
        {
            QueryFilter query = new QueryFilter();
            query.CalMonthId = calMonthId;

            query.PageIndex = 1;
            query.PageSize = 1;
            query.ConditionTree = new List<FieldCondition>();
            query.Sorts = new List<OrderField>();

            var fileData = await _httpClient.Post<FileProcessTask>($"PerformanceItem/ExportData", query);

            return fileData;
            
        }

        public async Task<FileProcessTask> ExportJXReport(int Year,int month)
        {
            var fileData = await _httpClient.Get<FileProcessTask>($"PerformanceItem/GetStaticsReport?Year={Year}&Month={month}");
            return fileData;
        }

        public async Task<FileProcessTask> CurrentData2Excel(CalMonth calMonth)
        {
            var fileData = await _httpClient.Get<FileProcessTask>($"PerformanceItem/CurrentData2Excel?Year={calMonth.Year}&Month={calMonth.Month}");

            return fileData;
        }

        public async Task<FileProcessTask> ExportData(int calMonthId, int userid, jxType jxType,int DoingOrReview=0)
        {
            QueryFilter query = new QueryFilter();
            query.userId = userid;
            query.jxType = jxType;
            query.CalMonthId = calMonthId;
            query.DoingOrReview = DoingOrReview;

            query.PageIndex = 1;
            query.PageSize = 1;
            query.ConditionTree = new List<FieldCondition>();
            query.Sorts = new List<OrderField>();

            var fileData = await  _httpClient.Post<FileProcessTask>($"PerformanceItem/ExportData", query);

            return fileData;
        }

        public async Task<FileProcessTask> getExportDataProcessing(string id)
        {
            var fileData = await _httpClient.Get<FileProcessTask>($"FileProcesTask/Get?Id={id}");

            return fileData;
        }

        public async Task<ListApiResponse<PerformanceItem>> Query(CalMonth calMonth,int pageIndex,int pageSize,string sortFieldName)
        {
            QueryFilter query = new QueryFilter();
            query.userId = 0;
            query.jxType = jxType.all;
            query.CalMonthId = calMonth.Id;
            query.PageIndex = pageIndex;
            query.PageSize = pageSize;

            if (!string.IsNullOrEmpty(sortFieldName))
            {
                query.Sorts = new List<OrderField>()
                {
                    new OrderField(){FieldName = sortFieldName}
                };
            }

            

            var data = await _httpClient.Post<ListApiResponse<PerformanceItem>>($"PerformanceItem/QueryFilter", query);
            return data;

        }

        public async  Task<FileProcessTask> FinishedCalMonth(CalMonth calMonth)
        {
            var ret = await _httpClient.Get<FileProcessTask>($"PerformanceItem/FinishedCalMonth?year={calMonth.Year}&month={calMonth.Month}");
            return ret;
        }

        public async Task<ListApiResponse<PerformanceItem>> Query(CalMonth calMonth, QueryModel<PerformanceItem> queryModel)
        {
            QueryFilter query = new QueryFilter();
            query.userId = 0;
            query.jxType = jxType.all;
            query.CalMonthId = calMonth.Id;

            if (queryModel != null)
            {

                query.PageIndex = queryModel.PageIndex;
                query.PageSize = queryModel.PageSize;

                query.ConditionTree = new List<FieldCondition>();

                foreach (var filter in queryModel.FilterModel)
                {
                    foreach (var f in filter.Filters)
                    {
                        FieldCondition condition = new FieldCondition() { FieldName = filter.FieldName };
                        condition.Value = f.Value.ToString();
                        condition.ValueType = typeof(PerformanceItem).GetProperty(filter.FieldName).PropertyType.ToString();
                        switch (f.FilterCompareOperator)
                        {
                            case AntDesign.TableFilterCompareOperator.Contains:
                                condition.Operator = OperatorEnum.Contains;
                                break;
                            case AntDesign.TableFilterCompareOperator.EndsWith:
                                condition.Operator = OperatorEnum.EndWith;
                                break;
                            case AntDesign.TableFilterCompareOperator.Equals:
                                condition.Operator = OperatorEnum.EndWith;
                                break;
                            case AntDesign.TableFilterCompareOperator.GreaterThan:
                                condition.Operator = OperatorEnum.Greater;
                                break;
                            case AntDesign.TableFilterCompareOperator.GreaterThanOrEquals:
                                condition.Operator = OperatorEnum.GreaterEqual;
                                break;
                            case AntDesign.TableFilterCompareOperator.LessThan:
                                condition.Operator = OperatorEnum.Less;
                                break;
                            case AntDesign.TableFilterCompareOperator.LessThanOrEquals:
                                condition.Operator = OperatorEnum.LessEqual;
                                break;
                            case AntDesign.TableFilterCompareOperator.NotContains:
                                condition.Operator = OperatorEnum.NotContains;
                                break;
                            case AntDesign.TableFilterCompareOperator.NotEquals:
                                condition.Operator = OperatorEnum.NotEqual;
                                break;
                            case AntDesign.TableFilterCompareOperator.StartsWith:
                                condition.Operator = OperatorEnum.StartsWith;
                                break;

                        }

                        switch (f.FilterCondition)
                        {
                            case AntDesign.TableFilterCondition.And:
                                condition.LogicOperate = LogicEnum.And;
                                break;
                            case AntDesign.TableFilterCondition.Or:
                                condition.LogicOperate = LogicEnum.Or;
                                break;
                            default:
                                condition.LogicOperate = LogicEnum.And;
                                break;
                        }

                        query.ConditionTree.Add(condition);

                    }
                }

                query.Sorts = new List<OrderField>();
                foreach (var sort in queryModel.SortModel)
                {
                    if (!string.IsNullOrEmpty(sort.Sort))
                    {
                        query.Sorts.Add(new OrderField() { FieldName = sort.FieldName, Sort = (sort.Sort == "descend" ? 1 : 0) });
                    }
                }
            }
            else
            {
                query.PageIndex = 1;
                query.PageSize = 1;
                query.ConditionTree = new List<FieldCondition>();
                query.Sorts = new List<OrderField>();
            }

            var data = await _httpClient.Post<ListApiResponse<PerformanceItem>>($"PerformanceItem/QueryFilter", query);
            //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(queryModel));
            return data;

        }

        public async Task<ListApiResponse<PerformanceItem>> Query(int calMonthId, int userid, jxType type, QueryModel<PerformanceItem> queryModel,int DoingOrReview=0)
        {
            QueryFilter query = new QueryFilter();
            query.CalMonthId = calMonthId;
            query.userId = userid;
            query.jxType = type;
            query.DoingOrReview = DoingOrReview;

            if (queryModel != null)
            {

                query.PageIndex = queryModel.PageIndex;
                query.PageSize = queryModel.PageSize;

                query.ConditionTree = new List<FieldCondition>();

                foreach (var filter in queryModel.FilterModel)
                {
                    foreach (var f in filter.Filters)
                    {
                        FieldCondition condition = new FieldCondition() { FieldName = filter.FieldName };
                        condition.Value = f.Value.ToString();
                        condition.ValueType = typeof(PerformanceItem).GetProperty(filter.FieldName).PropertyType.ToString();
                        switch (f.FilterCompareOperator)
                        {
                            case AntDesign.TableFilterCompareOperator.Contains:
                                condition.Operator = OperatorEnum.Contains;
                                break;
                            case AntDesign.TableFilterCompareOperator.EndsWith:
                                condition.Operator = OperatorEnum.EndWith;
                                break;
                            case AntDesign.TableFilterCompareOperator.Equals:
                                condition.Operator = OperatorEnum.EndWith;
                                break;
                            case AntDesign.TableFilterCompareOperator.GreaterThan:
                                condition.Operator = OperatorEnum.Greater;
                                break;
                            case AntDesign.TableFilterCompareOperator.GreaterThanOrEquals:
                                condition.Operator = OperatorEnum.GreaterEqual;
                                break;
                            case AntDesign.TableFilterCompareOperator.LessThan:
                                condition.Operator = OperatorEnum.Less;
                                break;
                            case AntDesign.TableFilterCompareOperator.LessThanOrEquals:
                                condition.Operator = OperatorEnum.LessEqual;
                                break;
                            case AntDesign.TableFilterCompareOperator.NotContains:
                                condition.Operator = OperatorEnum.NotContains;
                                break;
                            case AntDesign.TableFilterCompareOperator.NotEquals:
                                condition.Operator = OperatorEnum.NotEqual;
                                break;
                            case AntDesign.TableFilterCompareOperator.StartsWith:
                                condition.Operator = OperatorEnum.StartsWith;
                                break;

                        }

                        switch (f.FilterCondition)
                        {
                            case AntDesign.TableFilterCondition.And:
                                condition.LogicOperate = LogicEnum.And;
                                break;
                            case AntDesign.TableFilterCondition.Or:
                                condition.LogicOperate = LogicEnum.Or;
                                break;
                            default:
                                condition.LogicOperate = LogicEnum.And;
                                break;
                        }

                        query.ConditionTree.Add(condition);

                    }
                }

                query.Sorts = new List<OrderField>();
                foreach (var sort in queryModel.SortModel)
                {
                    if (!string.IsNullOrEmpty(sort.Sort))
                    {
                        query.Sorts.Add(new OrderField() { FieldName = sort.FieldName, Sort = (sort.Sort == "descend" ? 1 : 0) });
                    }
                }
            }
            else
            {
                query.PageIndex = 1;
                query.PageSize = 1;
                query.ConditionTree = new List<FieldCondition>();
                query.Sorts = new List<OrderField>();
            }

            var data = await _httpClient.Post<ListApiResponse<PerformanceItem>>($"PerformanceItem/QueryFilter",query);
            //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(queryModel));
            return data;
        }

        public async Task<PerformanceItem> RefreshItem(int Id)
        {
            var data = await _httpClient.Get<ApiSaveResponse>($"PerformanceItem/RefreshFromIPEasyById?itemId={Id}");

            
            return await _httpClient.Get<PerformanceItem>($"PerformanceItem/PerformanceItem?Id={Id}");
           
        }

        public async Task<List<StaffStatistics>> CalMyStatistics(int year, int month, int? userid = null)
        {
            var data = await _httpClient.Get<List<StaffStatistics>>($"PerformanceItem/CalMyStatistics?userid={userid}&year={year}&month={month}");
            return data;
        }

        public async Task<List<StaffStatistics>> CalItemStatistics(int ItemId)
        {
            var data = await _httpClient.Get<List<StaffStatistics>>($"PerformanceItem/CalItemJX?itemid={ItemId}");
            return data;
        }

        #region 绩效等级计算
        public async Task StatisticsLevelCount(int year ,int month)
        {
            await _httpClient.Get<bool>($"PerformanceItem/StatisticsLevelCount?year={year}&month={month}");
        }

        public async Task<LevelStatisticsResult> CalAgentLevel(int year, int quarter, string GradeCode)
        {
            var data =await _httpClient.Get<LevelStatisticsResult>($"PerformanceItem/CalAgentLevel?year={year}&quarter={quarter}&GradeCode={GradeCode}");
            return data;
        }

        #endregion



    }
}
