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 HttpClient _httpClient;
        private readonly JwtAuthenticationStateProvider _jwt;

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

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

        }

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

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

        public async Task<ListApiResponse<PerformanceItem>> Query(int userid, jxType type, QueryModel<PerformanceItem> queryModel)
        {
            QueryFilter query = new QueryFilter();
            query.userId = userid;
            query.jxType = type;
            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;
                            //expressTree.AddCondition. (LogicEnum.And, condition);
                            //Console.WriteLine($"添加条件：{condition.FieldName} {condition.Operator} {condition.Value} {condition.ValueType} AND");
                            break;
                        case AntDesign.TableFilterCondition.Or:
                            condition.LogicOperate = LogicEnum.Or;
                            //expressTree.AddCondition(LogicEnum.Or, condition);
                            //Console.WriteLine($"添加条件：{condition.FieldName} {condition.Operator} {condition.Value} {condition.ValueType}  OR");
                            break;
                        default:
                            condition.LogicOperate = LogicEnum.And;
                            //expressTree.AddCondition(LogicEnum.And, condition);
                            //Console.WriteLine($"添加条件：{condition.FieldName} {condition.Operator} {condition.Value} {condition.ValueType}  AND");
                            break;
                    }

                    //Console.WriteLine( expressTree.ToString());
                    query.ConditionTree.Add(condition);
                    
                }
            }

            

            query.Sorts =new  List<OrderField>();
            foreach(var sort in queryModel.SortModel)
            {
                //Console.WriteLine($"{sort.FieldName}\t{sort.Sort}");
                if (!string.IsNullOrEmpty(sort.Sort))
                {
                    query.Sorts.Add(new OrderField() { FieldName = sort.FieldName, Sort = (sort.Sort == "descend" ? 1 : 0) });
                }
            }
            var data = await _httpClient.PostAsJsonAsync($"http://localhost:39476/api/PerformanceItem/QueryFilter",query);

            var ret =  await data.Content.ReadFromJsonAsync<ListApiResponse<PerformanceItem>>();
            return ret;
        }

        public async Task<List<StaffStatistics>> CalMyStatistics(int year, int month, int? userid = null)
        {

            var data = await _httpClient.GetFromJsonAsync<List<StaffStatistics>>($"http://localhost:39476/api/PerformanceItem/CalMyStatistics?userid={userid}&year={year}&month={month}");
            return data;
        }
    }
}
