using 智慧物流软件系统.ViewModel;
using HandyControl.Controls;
using HandyControl.Tools.Extension;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MessageBox = HandyControl.Controls.MessageBox;
using Window = System.Windows.Window;
using WCS.Model.ApiModel;
using WCS.Model.ApiModel.User;
using 智慧物流软件系统.Api;
using WCS.Model;

namespace 智慧物流软件系统
{
    /// <summary>
    /// 角色信息
    /// </summary>
    public partial class RoleEditView : Window
    {
        public RoleEditView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>true:成功，false:取消</returns>
        public static bool Show(RoleModel roleBase, CrudEnum crudEnum)
        {
            bool isOk = false;

            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                var view = new RoleEditView();
                List<RoleEditTreeViewModel> dataList = new List<RoleEditTreeViewModel>();

                try
                {
                    if (crudEnum == CrudEnum.Read)
                        view.qr.IsEnabled = false;

                    view.textBox.Text = roleBase.Name;
                    dataList = RoleEditTreeViewModel.GetTreeViewModel(roleBase.Auths);
                    view.treeView.ItemsSource = dataList;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    view = null;
                    return;
                }

                view.qr.Click += (s, e) =>
                {
                    try
                    {
                        var treeNode = dataList.ToTreeNode();
                        var antuNames = treeNode.Where(o => o.IsSelect).Select(o => o.Name).ToList();
                        var antuNameStr = string.Join(",", antuNames);
                        RoleModel newRole = new RoleModel()
                        {
                            Id = roleBase.Id,
                            IsAdmin = roleBase.IsAdmin,
                            Name = view.textBox.Text.Trim(),
                            Auths =  treeNode.Where(o => o.IsSelect).Select(o => (int)o.Id).ToList(),
                            AuthNames = antuNameStr,
                            Time = roleBase.Time,
                        };
                        if (crudEnum == CrudEnum.Update)
                        {
                            if (string.IsNullOrEmpty(newRole.Name))
                            {
                                MessageBox.Show("请输入名称");
                                return;
                            }
                            try
                            {
                                var body = new AddRoleRequest<RoleModel>()
                                {
                                    UserName = LocalStatic.CurrentUser,
                                    DeviceType = LocalFile.Config.DeviceType,
                                    Role = newRole,
                                    AddOrUpdate = AddOrUpdate.Update
                                };
                                var Result = ApiHelp.GetDataFromHttp<ResponseCommon<object>>(LocalFile.Config.ApiIpHost + "user/addRole", body, "POST");
                                if (Result != null && Result.Code == 200)
                                {
                                    isOk = true;
                                    view.Close();
                                }
                                else
                                {
                                    Growl.Error(Result?.Message);
                                }
                            }
                            catch (Exception ex)
                            {
                                Growl.Error("修改角色失败：" + ex.Message);
                            }
                        }
                        else if (crudEnum == CrudEnum.Create)
                        {
                            newRole.Id = 0;
                            newRole.Time = DateTime.Now;

                            if (string.IsNullOrEmpty(newRole.Name))
                            {
                                MessageBox.Show("请输入名称");
                                return;
                            }
                            try
                            {
                                var body = new AddRoleRequest<RoleModel>()
                                {
                                    UserName = LocalStatic.CurrentUser,
                                    DeviceType = LocalFile.Config.DeviceType,
                                    Role = newRole,
                                    AddOrUpdate = AddOrUpdate.Add,
                                };
                                var Result = ApiHelp.GetDataFromHttp<ResponseCommon<object>>(LocalFile.Config.ApiIpHost + "user/addRole", body, "POST");
                                if (Result != null && Result.Code == 200)
                                {
                                    isOk = true;
                                    view.Close();
                                }
                                else
                                {
                                    Growl.Error(Result?.Message);
                                }
                            }
                            catch (Exception ex)
                            {
                                Growl.Error("新增角色失败：" + ex.Message);
                            }
                        }
                        else
                        {
                            MessageBox.Show("不支持此操作");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        return;
                    }

                    //isOk = true;
                    //view.Close();
                };

                view.ShowDialog();
                view = null;
            }));

            return isOk;
        }

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