using HighWayIot.Log4net;
using HighWayIot.Repository.domain;
using HighWayIot.Repository.service;
using HighWayIot.Winform.Business;
using HighWayIot.Winform.UserControlPages.SysConfigPages;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HighWayIot.Winform.UserControlPages.SysConfigPages
{
    public partial class RoleConfigPage : UserControl
    {
        SysUserRoleService roleService = SysUserRoleService.Instance;

        private static LogHelper logger = LogHelper.Instance;

        /// <summary>
        /// 数据库实体类列表
        /// </summary>
        private List<SysRoleEntity> List = new List<SysRoleEntity>();

        /// <summary>
        /// XML读取类
        /// </summary>
        XmlUtil xmlUtil = XmlUtil.Instance;

        /// <summary>
        /// 页面规则偏移量配置
        /// </summary>
        List<RoleConfig> ConfigList;



        public RoleConfigPage()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            RoleDataGridView.AutoGenerateColumns = false;
            List = roleService.GetRoleInfos();

            ConfigList = xmlUtil.ConfigReader();

            //设置列数量
            RoleDataGridView.ColumnCount = ConfigList.Count + 1;

            //设置列头名称
            RoleDataGridView.Columns[0].Name = "RoleName";
            RoleDataGridView.Columns[0].DataPropertyName = "RoleName";
            RoleDataGridView.Columns[0].HeaderText = "角色名";
            RoleDataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
            RoleDataGridView.Columns[0].Width = 100;

            for (int i = 0; i < ConfigList.Count; i++)
            {
                RoleDataGridView.Columns[i + 1].Name = ConfigList[i].PageName;
                RoleDataGridView.Columns[i + 1].DataPropertyName = ConfigList[i].PageName;
                RoleDataGridView.Columns[i + 1].HeaderText = ConfigList[i].PageName;
                RoleDataGridView.Columns[i + 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }

            GridViewRefresh();
        }

        private void UpdateRole_Click(object sender, EventArgs e)
        {
            //写的很烂
            int index = RoleDataGridView.CurrentRow.Index;

            //前端数据顺序绑定的时候就是数据库的顺序，所以可以这么写。
            RoleUpdateForm form = new RoleUpdateForm(List[index]);
            form.ShowDialog();

            List = roleService.GetRoleInfos();
            GridViewRefresh();
        }

        private void AddRole_Click(object sender, EventArgs e)
        {
            RoleAddForm form = new RoleAddForm();
            form.ShowDialog();
            List = roleService.GetRoleInfos();

            GridViewRefresh();
        }

        private void DeleteRole_Click(object sender, EventArgs e)
        {
            //写的很烂
            int index = RoleDataGridView.CurrentRow.Index;
            if (MessageBox.Show($"确认要删除{List[index].RoleName}该角色信息？", "确定？", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                //前端数据顺序绑定的时候就是数据库的顺序，所以可以这么写。
                roleService.DeleteRoleInfoById(List[index].Id);
                List = roleService.GetRoleInfos();
                GridViewRefresh();
            }
        }

        /// <summary>
        /// 刷新列
        /// </summary>
        private void GridViewRefresh()
        {
            RoleDataGridView.Rows.Clear();
            //用户名部分初始化赋值
            for (int i = 0; i < List.Count; i++)
            {
                RoleDataGridView.Rows.Add();
                RoleDataGridView.Rows[i].Cells[0].Value = List[i].RoleName;
            }

            //权限部分初始化赋值
            for (int rowIndex = 0; rowIndex < List.Count; rowIndex++)
            {
                char[] roleChars = List[rowIndex].RoleSet.ToCharArray();

                for (int columnIndex = 1; columnIndex < RoleDataGridView.Columns.Count; columnIndex++)
                {
                    // 获取当前单元格
                    DataGridViewCell cell = RoleDataGridView.Rows[rowIndex].Cells[columnIndex];

                    // 获取列名
                    string columnName = RoleDataGridView.Columns[columnIndex].Name;
                    try
                    {
                        RoleDataGridView.Rows[rowIndex].Cells[columnIndex].Value =
                            roleChars[ConfigList.Where(x => x.PageName == columnName).First().RoleIndex] == '1' ? "√" : "";
                    }
                    catch (Exception ex)
                    {
                        logger.Error("用户角色初始化发生错误，请检查Configuration.xml", ex);
                    }
                }
            }
        }
    }
}
