using RMYY_CallCenter_Api.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;

namespace RMYY_CallCenter_Api
{
    public class WorkOrderButton
    {
        private static string _buttonCacheKey = "ButtonCacheKey_";
        private static Bll.T_Sys_Menu menuBLL = new Bll.T_Sys_Menu();

        public static List<Button> GetButtons(MENUENUM menuName, int type, int status, int roleId = 0, int assistanceState = 0)
        {
            var desc = menuName.GetType().GetMember(menuName.ToString())[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (desc == null || desc.Length == 2)
            {
                return null;
            }
            string menuCode = (desc.Single() as DescriptionAttribute).Description;
            List<Button> buttons = GetButtonCacheData(menuCode);
            List<Button> buttonList = new List<Button>();
            if (buttons == null || buttons.Count <= 0)
            {
                return buttonList;
            }

            List<string> filterButtons = GetAssistanceFilterButtons(type);

            buttonList = buttons.Where(p => p.type == type && (p.status.IndexOf(status) >= 0 || p.status.Count == 0) && (assistanceState == 1 || (assistanceState == 0 && !filterButtons.Contains(p.key)))).ToList();

            // 获取角色对应的按钮
            List<string> roleButtons = null;
            if (roleId != 0)
            {
                roleButtons = Common.GetRoleButtons(roleId);
                if (roleButtons == null || roleButtons.Count <= 0)
                {
                    return null;
                }

                List<Button> newButtonList = new List<Button>();
                for (int i = 0; i < buttonList.Count; i++)
                {
                    if (!roleButtons.Contains(buttonList[i].id.ToString()))
                    {
                        continue;
                    }
                    else
                    {
                        newButtonList.Add(buttonList[i]);
                    }
                }


                buttonList = newButtonList;
            }


            return buttonList;
        }

        /// <summary>
        /// 获取内存数据，如果没有添加数据
        /// </summary>
        /// <param name="menuCode">菜单code</param>
        /// <returns></returns>
        private static List<Button> GetButtonCacheData(string menuCode)
        {


            // 判断缓存中是否有数据
            var buttons = CacheHelper.Get<List<Button>>(_buttonCacheKey + menuCode);
            if (buttons != null) {
                return buttons;
            }

            // 获取数据进行格式化
            List<Model.T_Sys_Menu> butList = menuBLL.GetModelList(string.Format("F_State = 1 and F_Type=3 and F_ParentId in (select F_MenuId from T_Sys_Menu where F_MenuCode = '{0}')", menuCode));

            if (butList == null || butList.Count <= 0)
            { 
                return null;
            }

            List<Button> returnList = new List<Button>();
            for (int i = 0; i < butList.Count; i++)
            {
                Button but = new Button();
                but.id = butList[i].F_MenuId;
                but.key = butList[i].F_MenuCode;
                but.value = butList[i].F_MenuName;
                but.type = butList[i].F_MenuType??0;
                but.status = string.IsNullOrEmpty(butList[i].F_WorkState) ? new List<int>() : butList[i].F_WorkState.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToList();

                returnList.Add(but);
            }

            CacheHelper.Insert(_buttonCacheKey + menuCode, returnList);

            return returnList;
        }

        //有协作工单，并且协作工单是未完成状态，该 工单去掉完成按钮
        private static List<string> GetAssistanceFilterButtons(int type)
        {
            List<string> returnList = new List<string>();
            switch (type)
            {
                case 3000: returnList = new List<string> { "wancheng", "" }; break;
            }

            return returnList;
        }

    }


 

    /// <summary>
    /// 列表菜单，需要和T_Sys_Menu保持一致
    /// </summary>
    public enum MENUENUM
    {
        [Description("MYSUB")]
        我的提交,
        [Description("orderList")]
        工单列表,
        [Description("repairList")]
        报修列表,
        [Description("scheduleList")]
        调度工单列表
    }


    public class Button
    {
        public int id { get; set; }
        public string key { get; set; }
        public string value { get; set; }
        public int type { get; set; }
        public List<int> status { get; set; }
    }
}