using AntDesign;
using AntDesign.TableModels;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using wispro.sp.web.Services;

namespace wispro.sp.web.Pages.Workflow
{
    public partial class WorkflowDefine
    {

        private List<wispro.sp.entity.workflowDefine.Workflow> workflows = new List<wispro.sp.entity.workflowDefine.Workflow>();
        ITable table;

        
        int _pageIndex = 1;
        int _pageSize = 10;
        int _total = 0;
        bool _loading = false;
        bool isAdd = false;

        entity.workflowDefine.Workflow EditingObj = null;
        share.ActionObject InitAction = null;
        

        bool _visible = false;
        bool _initModalVisible = false;

        List<wispro.sp.entity.EnumnDescription<wispro.sp.entity.workflowDefine.FieldType>> FieldTypes
            = entity.EnumHelper.getEnumDescriptionDic<wispro.sp.entity.workflowDefine.FieldType>();

        entity.workflowDefine.FieldType fieldType = entity.workflowDefine.FieldType.ActionInputValue;

        [Inject] protected IUserService UserService { get; set; }
        [Inject] protected WorkflowService workflowService { get; set; }

        [Inject] protected MessageService _msgService { get; set; }

        [Inject] protected NavigationManager navigation { get; set; }

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

        [Inject] protected IJSRuntime JSRuntime { get; set; }


        protected override async System.Threading.Tasks.Task OnInitializedAsync()
        {
            await _authService.CanVisitResource();

            await base.OnInitializedAsync();
            workflows = await workflowService.getAllWorkflows();
        }



        private void AddNew()
        {
            isAdd = true;
            EditingObj = new entity.workflowDefine.Workflow();
            InitAction = new share.ActionObject();
            InitAction.Action = new entity.workflowDefine.Action();
            InitAction.InputValueSettingObjects = new List<share.InputValueSettingObject>();

            //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(InitAction));
            _visible = true;
        }

        async Task HandleOk()
        {
            if (EditingObj.Id ==0)
            {
                
                EditingObj.CreateTime = DateTime.Now;

                var retResponse = await workflowService.AddWorkflow(EditingObj,InitAction);

                if (retResponse.Success)
                {

                    workflows = await workflowService.getAllWorkflows();
                    _visible = false;
                    //StateHasChanged();
                }
                else
                {
                   await _msgService.Error(retResponse.ErrorMessage);
                }
            }
        }
    

        void HandleCancel()
        {
            _visible = false;
        }

        public int serialNumber(int pageIndex, int pageSize, int Id)
        {
            int iIndex = 0;
            foreach (wispro.sp.entity.workflowDefine.Workflow sf in workflows)
            {
                iIndex++;

                if (sf.Id  == Id)
                {
                    break;
                }
            }
            return iIndex;
        }

        wispro.sp.entity.workflowDefine.Workflow SelectedWorkflow;
        Dictionary<string, object> OnRow(RowData<wispro.sp.entity.workflowDefine.Workflow> row) => new()
        {
            ["id"] = row.Data.Id,
            ["onclick"] = ((Action)delegate
            {
                SelectedWorkflow = row.Data;
                //Console.WriteLine($"row {row.Data.Name} was clicked");
            })
        };

        async Task Delete(entity.workflowDefine.Workflow workflow)
        {
            var ret = await workflowService.Delete(workflow.Id);

            if (ret.Success)
            {
                workflows.Remove(workflow);
            }
            else
            {
                await _msgService.Error(ret.ErrorMessage);
            }
            
        }

        void Detail(int Id)
        {
            navigation.NavigateTo($"/Workflow/Detail/{Id}");
        }

        //IDictionary<entity.workflowDefine.Workflow, List<entity.workflowDefine.InputValueSetting>> dicFieldSettings
        //    = new Dictionary<entity.workflowDefine.Workflow, List<entity.workflowDefine.InputValueSetting>>();
        //void InitFieldSetting(entity.workflowDefine.Workflow workflow)
        //{
        //    if (dicFieldSettings.ContainsKey(workflow))
        //    {
        //        InitInputValues = dicFieldSettings[workflow];
        //    }
        //    else
        //    {
        //        InitInputValues = new List<entity.workflowDefine.InputValueSetting>();
        //        dicFieldSettings.Add(workflow,InitInputValues);
        //    }

        //    _initModalVisible = true;
        //}

        


       
    }
}
