using BBWY.Client.APIServices;
using BBWY.Client.Models;
using BBWY.Client.Views.Setting;
using BBWY.Common.Models;
using GalaSoft.MvvmLight.Command;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace BBWY.Client.ViewModels
{
    public class ShopSettingViewModel : BaseVM, IDenpendency
    {
        private bool isValidated;
        private bool isLoading;
        private GlobalContext globalContext;
        private ShopService shopService;
        private PurchaseAccount purchaseAccount;
        private string managePwd;
        private decimal platformCommissionRatio;
        private int panelIndex;
        private string dingDingWebHook;
        private string dingDingKey;
        private KVModel selectedSkuSafeTurnoverDay;

        private string siNanDingDingWebHook;
        private string siNanDingDingKey;

        private string qiKuDingDingWebHook;
        private string qiKuDingDingKey;


        private SiNanPolicyLevel selectedSiNanPolicyLevel;

        private Platform purchasePlatform;


        public ICommand SaveCommand { get; set; }

        public ICommand SetPanelIndexCommand { get; set; }

        public PurchaseAccount PurchaseAccount { get => purchaseAccount; set { Set(ref purchaseAccount, value); } }

        public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } }

        public string ManagePwd { get => managePwd; set { Set(ref managePwd, value); } }
        public decimal PlatformCommissionRatio { get => platformCommissionRatio; set { Set(ref platformCommissionRatio, value); } }

        public int PanelIndex { get => panelIndex; set { Set(ref panelIndex, value); } }

        public IList<KVModel> SafeDayList { get; set; }
        public string DingDingWebHook { get => dingDingWebHook; set { Set(ref dingDingWebHook, value); } }
        public string DingDingKey { get => dingDingKey; set { Set(ref dingDingKey, value); } }



        public string QiKuDingDingWebHook { get => qiKuDingDingWebHook; set { Set(ref qiKuDingDingWebHook, value); } }
        public string QiKuDingDingKey { get => qiKuDingDingKey; set { Set(ref qiKuDingDingKey, value); } }


        public KVModel SelectedSkuSafeTurnoverDay { get => selectedSkuSafeTurnoverDay; set { Set(ref selectedSkuSafeTurnoverDay, value); } }

        public string SiNanDingDingWebHook { get => siNanDingDingWebHook; set { Set(ref siNanDingDingWebHook, value); } }
        public string SiNanDingDingKey { get => siNanDingDingKey; set { Set(ref siNanDingDingKey, value); } }
        public SiNanPolicyLevel SelectedSiNanPolicyLevel { get => selectedSiNanPolicyLevel; set { Set(ref selectedSiNanPolicyLevel, value); } }

        /// <summary>
        /// 采购平台
        /// </summary>
        public Platform PurchasePlatform
        {
            get => purchasePlatform;
            set
            {
                if (Set(ref purchasePlatform, value))
                    OnPurchasePlatformChanged();
            }
        }

        public ShopSettingViewModel(GlobalContext globalContext, ShopService shopService)
        {
            PanelIndex = 0;
            this.globalContext = globalContext;
            this.shopService = shopService;
            SaveCommand = new RelayCommand(Save);
            SetPanelIndexCommand = new RelayCommand<int>(i =>
            {
                PanelIndex = i;


                if (i==3)//获取齐库钉钉配置
                {

                }
            });
            SafeDayList = new List<KVModel>()
            {
                new KVModel()
                {
                    Key="不提醒",
                    Value="0"
                },
                new KVModel()
                {
                    Key="14",
                    Value="14"
                },
                new KVModel()
                {
                    Key="21",
                    Value="21"
                },
                new KVModel()
                {
                    Key="28",
                    Value="28"
                }
            };
        }

        protected override void Load()
        {
            if (globalContext.User.Shop.PurchaseAccountList == null)
                globalContext.User.Shop.PurchaseAccountList = new List<PurchaseAccount>();

            IsLoading = false;
            if (!string.IsNullOrEmpty(globalContext.User.Shop.ManagePwd))
            {
                var validatePwd = new ValidateManagePwd(globalContext.User.Shop.ManagePwd);
                if (validatePwd.ShowDialog() != true)
                    return;
            }
            isValidated = true;
            this.ManagePwd = globalContext.User.Shop.ManagePwd;
            this.PlatformCommissionRatio = (globalContext.User.Shop.PlatformCommissionRatio ?? 0.05M) * 100;

            DingDingKey = globalContext.User.Shop.DingDingKey;
            DingDingWebHook = globalContext.User.Shop.DingDingWebHook;
            SelectedSkuSafeTurnoverDay = SafeDayList.FirstOrDefault(s => s.Value == globalContext.User.Shop.SkuSafeTurnoverDays.ToString());

            SelectedSiNanPolicyLevel = (SiNanPolicyLevel)globalContext.User.Shop.SiNanPolicyLevel;
            SiNanDingDingKey = globalContext.User.Shop.SiNanDingDingKey;
            SiNanDingDingWebHook = globalContext.User.Shop.SiNanDingDingWebHook;

            QiKuDingDingWebHook=globalContext.User.Shop.QiKuDingDingWebHook;
            QiKuDingDingKey = globalContext.User.Shop.QiKuDingDingKey;
            PurchasePlatform = Platform.阿里巴巴;
        }

        protected override void Unload()
        {
            //this.PurchaseAccount = null;
            this.PlatformCommissionRatio = 0M;
            this.ManagePwd = string.Empty;
            this.isValidated = false;
            this.DingDingKey = this.DingDingWebHook = string.Empty;
            SelectedSkuSafeTurnoverDay = SafeDayList[0];
        }

        private void Save()
        {
            if (!isValidated)
            {
                MessageBox.Show("店铺密码验证未通过", "保存店铺设置");
                return;
            }

            if (PurchaseAccount.PurchasePlatformId == Platform.拳探)
            {
                PurchaseAccount.AppKey = "BBWY2023022001";
                PurchaseAccount.AppSecret = "908e131365d5448ca651ba20ed7ddefe";
                PurchaseAccount.AppToken = PurchaseAccount.AccountName;
            }

            if (string.IsNullOrEmpty(PurchaseAccount.AppKey) ||
                string.IsNullOrEmpty(PurchaseAccount.AppSecret) ||
                string.IsNullOrEmpty(PurchaseAccount.AppToken) ||
                string.IsNullOrEmpty(PurchaseAccount.AccountName))
            {
                MessageBox.Show("采购信息不能为空", "保存店铺设置");
                return;
            }

            PurchaseAccount.AppKey = PurchaseAccount.AppKey.Trim();
            PurchaseAccount.AppSecret = PurchaseAccount.AppSecret.Trim();
            PurchaseAccount.AppToken = PurchaseAccount.AppToken.Trim();
            PurchaseAccount.AccountName = PurchaseAccount.AccountName.Trim();

            var p = PlatformCommissionRatio / 100;
            IsLoading = true;
            Task.Factory.StartNew(() => shopService.SaveShopSetting(globalContext.User.Shop.ShopId,
                                                                    ManagePwd,
                                                                    p,
                                                                    PurchaseAccount,
                                                                    DingDingWebHook,
                                                                    DingDingKey,
                                                                    int.Parse(SelectedSkuSafeTurnoverDay.Value),
                                                                    SiNanDingDingWebHook,
                                                                    SiNanDingDingKey,
                                                                    (int)SelectedSiNanPolicyLevel,
                                                                     QiKuDingDingWebHook,
                                                                    QiKuDingDingKey
                                                                    )).ContinueWith(r =>
            {
                IsLoading = false;
                var response = r.Result;
                if (!response.Success)
                {
                    App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "保存店铺设置"));
                    return;
                }

                PurchaseAccount.Id = response.Data;
                globalContext.User.Shop.PlatformCommissionRatio = p;
                globalContext.User.Shop.ManagePwd = this.ManagePwd;
                globalContext.User.Shop.DingDingKey = this.DingDingKey;
                globalContext.User.Shop.DingDingWebHook = this.DingDingWebHook;
                globalContext.User.Shop.SkuSafeTurnoverDays = int.Parse(SelectedSkuSafeTurnoverDay.Value);

                globalContext.User.Shop.SiNanDingDingKey = this.SiNanDingDingKey;
                globalContext.User.Shop.SiNanDingDingWebHook = this.SiNanDingDingWebHook;

                globalContext.User.Shop.QiKuDingDingWebHook = this.QiKuDingDingWebHook;
                globalContext.User.Shop.QiKuDingDingKey = this.QiKuDingDingKey;
                globalContext.User.Shop.SiNanPolicyLevel = (int)this.SelectedSiNanPolicyLevel;
            });
        }

        private void OnPurchasePlatformChanged()
        {
            var pa = globalContext.User.Shop.PurchaseAccountList.FirstOrDefault(pa => pa.PurchasePlatformId == PurchasePlatform);
            if (pa == null)
            {
                pa = new PurchaseAccount()
                {
                    PurchasePlatformId = PurchasePlatform,
                };
                globalContext.User.Shop.PurchaseAccountList.Add(pa);
            }

            this.PurchaseAccount = pa;
        }
    }
}