using BBWYB.Client.APIServices;
using BBWYB.Client.Models;
using BBWYB.Client.Views;
using BBWYB.Common.Extensions;
using BBWYB.Common.Models;
using CommunityToolkit.Mvvm.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace BBWYB.Client.ViewModels
{
    public class WebVM : BaseVM, IDenpendency
    {
        private MdsApiService mdsApiService;
        private MenuModel selectedMenuModel;
        private bool isLoading;
        private IList<string> managerDepartment;
        ShopService shopService;
        public GlobalContext GlobalContext { get; set; }
        public bool IsLoading { get => isLoading; set { SetProperty(ref isLoading, value); } }

        public WebVM(GlobalContext globalContext,
                            MdsApiService mdsApiService,
                            ShopService shopService)
        {
            this.mdsApiService = mdsApiService;
            this.GlobalContext = globalContext;
            this.shopService = shopService;
            this.managerDepartment = new List<string>() { "董事办", "财务部", "技术部", "总经办" };
            Task.Factory.StartNew(Login);
        }

        private void Login()
        {
            IsLoading = true;
            try
            {
                var mdsUserResponse = mdsApiService.GetUserInfo(GlobalContext.UserToken);
                if (!mdsUserResponse.Success)
                    throw new Exception($"获取磨刀石用户信息失败 {mdsUserResponse.Msg}");


                GlobalContext.User = mdsUserResponse.Data.Map<User>();
                GlobalContext.User.Token = GlobalContext.UserToken;
                GlobalContext.User.SonDepartmentNames = string.Empty;
                if (mdsUserResponse.Data.SonDepartmentList != null && mdsUserResponse.Data.SonDepartmentList.Count > 0)
                    GlobalContext.User.SonDepartmentNames = string.Join(',', mdsUserResponse.Data.SonDepartmentList.Select(sd => sd.DepartmentName));


                IList<Department> departmentList = null;

                if (managerDepartment.Contains(GlobalContext.User.TeamName) ||
                   managerDepartment.Any(m => GlobalContext.User.SonDepartmentNames.Contains(m)))
                {
                    var res = shopService.GetDepartmentList();
                    if (!res.Success)
                        throw new Exception(res.Msg);
                    var allDepartmentList = res.Data.Map<IList<Department>>();


                    var shopList = new List<Shop>();
                    foreach (var d in allDepartmentList)
                        shopList.AddRange(d.ShopList);
                    GlobalContext.User.ShopList = shopList;
                    departmentList = allDepartmentList.Where(d => d.Name.Contains("供应链")).ToList();
                }
                else
                {
                    var response = mdsApiService.GetShopDetailList();
                    if (!response.Success)
                        throw new Exception(response.Msg);
                    departmentList = response.Data.Where(d => d.Name.Contains("供应链")).ToList();
                }

                if (departmentList.Count == 0)
                    throw new Exception("缺少有效的部门数据");


                //departmentList = allDepartmentList.Where(d => d.Name.Contains("供应链")).ToList();
                //if (departmentList.Count == 0)
                //    throw new Exception("缺少有效的部门数据");

                //var shopIds = new List<string>();
                //foreach (var d in departmentList)
                //{
                //    if (d.ShopList != null && d.ShopList.Count > 0)
                //    {
                //        foreach (var s in d.ShopList)
                //            shopIds.Add(s.ShopId.ToString());
                //    }
                //}


                GlobalContext.User.DepartmentList = departmentList;
                WeakReferenceMessenger.Default.Send(new Message_WebB_LoginCompleted(null));
                IsLoading = false;
            }
            catch (Exception ex)
            {
                IsLoading = false;
                App.Current.Dispatcher.Invoke(() =>
                {
                    MessageBox.Show(ex.Message, "登录失败");
                });
                Environment.Exit(Environment.ExitCode);
            }
        }
    }
}