using Khd.Core.Domain.Auth;
using Khd.Core.EntityFramework;
using Khd.Core.Library.Mapper;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Khd.Core.Wpf.TaskForm
{
    /// <summary>
    /// MenuList.xaml 的交互逻辑
    /// </summary>
    public partial class MenuList : Window
    {
        public List<MenuListData> MenuListDatas = new List<MenuListData>();
        private readonly long? _userId;
        private readonly IHost _host;
        public MenuList(IHost host, long? userId)
        {
            InitializeComponent();
            _host = host;
            _userId = userId;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                using var scope = this._host.Services.CreateScope();
                var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
                var sysMenus = dbContext.SysMenu.Where(t => t.MenuVisble == 1).ToList();
                var sysRoles = dbContext.SysRole.Where(t => t.UserId == _userId).ToList();
                MenuListDatas = CoreMapper.Map<List<MenuListData>>(sysMenus);
                MenuListDatas.ForEach(t =>
                {
                    t.IsSeleted = sysRoles.Any(r => r.RoleId == t.OBJID);
                });
                this.MenuListData.ItemsSource = MenuListDatas;
            }
            catch
            {
                MessageBox.Show("加载菜单列表失败");
                this.Close();
            }
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            if (sender is CheckBox checkBox && this.MenuListData.SelectedItem is MenuListData menuListData)
            {
                MenuListData data = MenuListDatas.Where(t => t.OBJID == menuListData.OBJID).First();
                data.IsSeleted = checkBox.IsChecked ?? false;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using var scope = this._host.Services.CreateScope();
                var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
                var sysRoles = dbContext.SysRole.Where(t => t.UserId == _userId).ToList();
                dbContext.RemoveRange(sysRoles);
                List<MenuListData> menuListDatas = MenuListDatas.Where(t => t.IsSeleted).ToList();
                List<SysRole> sysRolesToAdd = new List<SysRole>();
                foreach (var item in menuListDatas)
                {
                    sysRolesToAdd.Add(new SysRole
                    {
                        OBJID = Global.SnowId.NextId(),
                        RoleId = item.OBJID,
                        UserId = _userId,
                    });
                }
                dbContext.AddRange(sysRolesToAdd);
                dbContext.SaveChanges();
                MessageBox.Show("保存成功");
                this.Close();
            }
            catch
            {

            }
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }

    public class MenuListData
    {
        public long OBJID { get; set; }
        public string MenuName { get; set; }
        public string MenuIcon { get; set; }
        public bool IsSeleted { get; set; }
    }
}
