using BBWY.Client.APIServices;
using BBWY.Client.Models;
using BBWY.Client.Models.QiKu;
using BBWY.Client.Views.BatchPurchase;
using GalaSoft.MvvmLight.Command;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace BBWY.Client.ViewModels
{
    public class PackSkuSplitConfigViewModel : BaseVM
    {
        public IList<PackSkuConfig> PackSkuConfigList { get; set; }

        private IList<StoreResponse> storeList;

        public ICommand SetSplitCountCommand { get; set; }

        public ICommand SetPackCountAndStoreCommand { get; set; }

        public ICommand SaveCommand { get; set; }

        private LogisticsService logisticsService;

        public PackSkuSplitConfigViewModel(LogisticsService logisticsService)
        {
            this.logisticsService = logisticsService;
            SetSplitCountCommand = new RelayCommand<PackSkuConfig>(SetSplitCount);
            SetPackCountAndStoreCommand = new RelayCommand<PackSkuSplitConfig>(SetPackCountAndStore);
            SaveCommand = new RelayCommand(Save);
            PackSkuConfigList = new ObservableCollection<PackSkuConfig>();
        }

        public void SetData(IList<PackSkuConfig> packSkuConfigList)
        {
            foreach (var item in packSkuConfigList)
                PackSkuConfigList.Add(item);
        }

        public IList<PackSkuConfig> GetPackSkuConfigList()
        {
            return PackSkuConfigList;
        }

        private void SetSplitCount(PackSkuConfig packSkuConfig)
        {
            if (packSkuConfig.SplitCount <= 0)
            {
                MessageBox.Show("份数不正确");
                return;
            }

            packSkuConfig.PackSkuSplitConfigList.Clear();
            for (var i = 1; i <= packSkuConfig.SplitCount; i++)
            {
                packSkuConfig.PackSkuSplitConfigList.Add(new PackSkuSplitConfig()
                {
                    Index = i,
                    PackCount = 0
                });
            }
        }

        private void SetPackCountAndStore(PackSkuSplitConfig packSkuSplitConfig)
        {
            if (storeList == null)
            {
                var response = logisticsService.GetStoreList();
                if (!response.Success)
                {
                    MessageBox.Show(response.Msg, "获取仓库");
                    return;
                }
                storeList = response.Data.Where(s=>s.Status== StockStatus.使用).ToArray();
            }
            var w = new PackSkuSplitCountAndStoreWindow(packSkuSplitConfig.PackCount, packSkuSplitConfig.Store, storeList, packSkuSplitConfig.IsJST);
            if (w.ShowDialog() == true)
            {
                packSkuSplitConfig.PackCount = w.Quantity;
                packSkuSplitConfig.Store = w.Store;
                packSkuSplitConfig.IsJST = w.IsJST;
            }
        }

        private void Save()
        {
            if (PackSkuConfigList.Any(s => s.PackSkuSplitConfigList.Count() == 0 ||
                                           s.PackSkuSplitConfigList.Any(sp => sp.PackCount <= 0)))
            {
                MessageBox.Show("装箱设置不正确", "提示");
                return;
            }

            if (PackSkuConfigList.Any(s => s.PurchaseCount != s.PackSkuSplitConfigList.Sum(sp => sp.PackCount)))
            {
                MessageBox.Show("打包份数总数与采购数量不相等", "提示");
                return;
            }

            GalaSoft.MvvmLight.Messaging.Messenger.Default.Send(true, "PackSkuConfigWindowClose");
        }
    }
}