using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using wispro.sp.entity.workflowDefine;
using wispro.sp.share;

namespace wispro.sp.web.Services
{
    public class WorkflowService
    {
        private readonly IHttpService _httpClient;

        public WorkflowService(IHttpService http)
        {
            _httpClient = http;
        }

        private List<BindObjectType> bindObjectTypes = new List<BindObjectType>()
        {
            new BindObjectType(){ Name ="绩效记录",ObjectTypeFullName = typeof(wispro.sp.entity.PerformanceItem).AssemblyQualifiedName },
                new BindObjectType(){ Name ="人员",ObjectTypeFullName = typeof(wispro.sp.entity.Staff).AssemblyQualifiedName}
        };

        public List<BindObjectType> GetBindObjects()
        {
            return bindObjectTypes;
        }

        public string GetBindObjectName(string ObjectTypeFullName)
        {
            var retObj = bindObjectTypes.FirstOrDefault<BindObjectType>(b => b.ObjectTypeFullName == ObjectTypeFullName);

            if(retObj != null)
            {
                return retObj.Name;
            }
            else
            {
                return "";
            }
        }
        

        List<entity.workflowDefine.Workflow> Workflows = new List<Workflow>();
        Dictionary<int, List<entity.workflowDefine.InputValueSetting>> InitInputValues = new Dictionary<int, List<InputValueSetting>>();
        public async Task<ApiSaveResponse> AddWorkflow(entity.workflowDefine.Workflow wf,share.ActionObject InitAction)
        {
            share.NewWorkflowObject newWorkflow = new share.NewWorkflowObject();
            newWorkflow.Workflow = wf;
            newWorkflow.InitAction = InitAction;

            //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(newWorkflow));

            var ret = await _httpClient.Post<ApiSaveResponse>("WorkflowEngine/AddNew", newWorkflow);

            return ret;
            
        }

        public async Task<List<Workflow>> getAllWorkflows()
        {
            var retList = await _httpClient.Get<List<Workflow>>($"WorkflowEngine/GetAllWorkflows");
            return retList;
        }

        public async  Task<Workflow> GetWorkflow(int Id)
        {
            Workflow ret = await _httpClient.Get<Workflow>($"WorkflowEngine/GetWorkflow?workflowId={Id}");

            //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(ret));
            return ret;
        }

        public async  Task<List<entity.workflowDefine.Step>> GetSteps(int Id)
        {
            var ret = await _httpClient.Get<List<entity.workflowDefine.Step>>($"WorkflowEngine/GetSteps?workflowId={Id}");
            return ret;
        }
        
        public async Task<List<entity.workflowDefine.Action>> GetActions(int workflowId)
        {
            var ret = await _httpClient.Get<List<entity.workflowDefine.Action>>($"WorkflowEngine/GetActions?workflowId={workflowId}");
            return ret;
        }

        public async Task<List<entity.workflowDefine.TrasferCondition>> GetTransfers(int workflowId)
        {
            var ret = await _httpClient.Get<List<entity.workflowDefine.TrasferCondition>>($"WorkflowEngine/GetTrasfers?workflowId={workflowId}");
            return ret;
        }

        public async Task<ApiSaveResponse> SaveStep(NewStepObject editStep)
        {
            var ret = await _httpClient.Post<ApiSaveResponse>("WorkflowEngine/SaveStep", editStep);
            return ret;
        }

        public async Task<ApiSaveResponse> SaveAction(entity.workflowDefine.Action editAction)
        {
            var ret = await _httpClient.Post<ApiSaveResponse>("WorkflowEngine/SaveAction", editAction);
            return ret;
        }

        public async Task<ApiSaveResponse> Delete(int id)
        {
            var ret = await _httpClient.Get<ApiSaveResponse>($"WorkflowEngine/DeleteWorkflow?workflowId={id}");
            return ret;
        }

        public async Task<ApiSaveResponse> SaveTransfer(entity.workflowDefine.TrasferCondition  trasfer)
        {
            var ret = await _httpClient.Post<ApiSaveResponse>("WorkflowEngine/SaveTransfer", trasfer);
            return ret;
        }

        public async Task<ApiSaveResponse> DeleteAction(int id)
        {
            var ret = await _httpClient.Get<ApiSaveResponse>($"WorkflowEngine/DeleteAction?Id={id}");
            return ret;
        }

        public async Task<List<share.InputValueSettingObject>> getInputValueSteeing(int actionId)
        {
            var ret = await _httpClient.Get<List<InputValueSetting>>($"WorkflowEngine/getInputValueSteein?actionId={actionId}");

            var retList = new List<share.InputValueSettingObject>();
            if(ret != null)
            {
                foreach(var iv in ret)
                {
                    if (iv.ParentSettingId == null)
                    {
                        InputValueSettingObject ivObj = new InputValueSettingObject();
                        ivObj.InputValueSetting = iv;
                        if(iv.valueType == EnumFieldType.List)
                        {
                            ivObj.Children = ret.Where<entity.workflowDefine.InputValueSetting>(s => s.ParentSettingId == iv.ParentSettingId).ToList();
                        }

                        retList.Add(ivObj);
                    }
                }
            }

            //Console.WriteLine($"Services-GetInputValueSettings:{System.Text.Json.JsonSerializer.Serialize(retList)}");
            return retList;
        }

        public async Task<ApiSaveResponse> DeleteInputValueSetting(int Id)
        {
            var ret = await _httpClient.Get<ApiSaveResponse>($"WorkflowEngine/DeleteInputValueSetting?Id={Id}");
            return ret;
        }

        public async Task<ApiSaveResponse> DeleteStep(Step selectedObject)
        {
            var ret = await _httpClient.Get<ApiSaveResponse>($"WorkflowEngine/DeleteStep?Id={selectedObject.Id}");
            return ret;
        }

        public async Task<ApiSaveResponse> DeleteTransfer(TrasferCondition transfer)
        {
            var ret = await _httpClient.Get<ApiSaveResponse>($"WorkflowEngine/DeleteTransfer?Id={transfer.Id}");
            return ret;
        }

        public async Task<InputValueSetting> getInputValueSteeingById(int id)
        {
            var ret = await _httpClient.Get<InputValueSetting>($"WorkflowEngine/getInputValueSteeingById?Id={id}");
            return ret;
        }
    }
}
