using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using wispro.sp.entity;
using wispro.sp.entity.workflowDefine;
using wispro.sp.web.Services;

namespace wispro.sp.web.Components
{
    public class UserItem
    {
        public UserType UserType { get; set; }

        public string Label { get; set; }

        public string Value { get; set; }

        public string GroupName { get; set; }
    }

    public partial class UserConditionInput
    {
        List<UserItem> UserItems = new List<UserItem>();

        UserItem SelectedUserItem;
        string _selectedValue ;

        [Parameter]
        public Workflow Workflow { get; set; }

        [Parameter]
        public UserType UserType { get; set; }

        [Parameter]
        public string UserValue { get; set; }

        //[Parameter]
        //public EventCallback<UserType> UserTypeChanged { get; set; }

        //[Parameter]
        //public EventCallback<string> UserValueChanged { get; set; }

        [Parameter]
        public EventCallback<UserItem> OnSelectedChange { get; set; }

        [Inject] public WorkflowService wfService { get; set; }

        [Inject] IUserService _UserService { get; set; }

        protected async override Task OnInitializedAsync()
        {
            UserItems = new List<UserItem>();

            if (!string.IsNullOrEmpty(Workflow.ContentObjectType))
            {
                string temString = wfService.GetBindObjectName(Workflow.ContentObjectType);
                var dic = share.Utility.UserConditionHelper.GetPropertyDescription<Staff>(Workflow.ContentObjectType);
                foreach (string key in dic.Keys)
                {
                    UserItems.Add(new UserItem()
                    {
                        Value = dic[key].ToString(),
                        Label = $"{temString}.{key}",
                        UserType = UserType.BindObjectProperty,
                        GroupName = temString //EnumHelper.GetDescription<UserType>(UserType.BindObjectProperty)
                    });
                }
            }

            var actions =await wfService.GetActions(Workflow.Id);
            //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(actions));
            foreach(var action in actions)
            {
                if (action.Id == Workflow.InitActionId)
                {
                    UserItems.Add(new UserItem()
                    {
                        Value = action.Id.ToString(),
                        Label = $"流程创建人",
                        UserType = UserType.DoActionUser,
                        GroupName = EnumHelper.GetDescription<UserType>(UserType.DoActionUser)
                    });
                }
                else
                {
                    UserItems.Add(new UserItem()
                    {
                        Value = action.Id.ToString(),
                        Label = $"{action.step.Name}.{action.Name}处理人",
                        UserType = UserType.DoActionUser,
                        GroupName = EnumHelper.GetDescription<UserType>(UserType.DoActionUser)
                    });
                }
            }

            UserItems.Add(new UserItem()
            {
                Value = "",
                Label = "登录用户",
                UserType = UserType.LoginUser,
                GroupName = EnumHelper.GetDescription<UserType>(UserType.LoginUser)
            });

            var Staffs =await  _UserService.GetAll();
            //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(Staffs));
            foreach (var sf in Staffs)
            {
                UserItems.Add(new UserItem()
                {
                    Value = sf.Id.ToString(),
                    Label = sf.Name,
                    UserType = UserType.Staff,
                    GroupName = EnumHelper.GetDescription<UserType>(UserType.Staff)
                });
            }
            
        }

        private void OnSelectedItemChangedHandler(UserItem value)
        {
            SelectedUserItem = value;

            if (OnSelectedChange.HasDelegate)
            {
                OnSelectedChange.InvokeAsync(value);
            }
            //UserType = SelectedUserItem.UserType;
            //UserValue = SelectedUserItem.Value;
            //Console.WriteLine($"selected: ${value?.Value}");
            //Console.WriteLine(UserValue);
        }
    }
}
