using BBWY.Client.Models;
using BBWY.Controls;
using GalaSoft.MvvmLight.Command;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace BBWY.Client.Views.BatchPurchase
{
    /// <summary>
    /// PackSkuSplitCountAndStoreWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PackSkuSplitCountAndStoreWindow : BWindow
    {
        public int Quantity { get; set; }

        public StoreResponse Store { get; set; }
        public int StoreTypeIndex
        {
            get => storeTypeIndex;
            set
            {
                if (Set(ref storeTypeIndex, value))
                {
                    OnStoreIndexChanged();
                    if (value > 0)
                        IsJST = false;
                }
            }
        }

        public bool IsJST
        {
            get => isJST;
            set
            {
                if (Set(ref isJST, value))
                {
                    OnStoreIndexChanged();
                    if (value)
                        StoreTypeIndex = 0;
                }
            }
        }

        public ICommand SetStoreIndexCommand { get; set; }
        public ICommand SetJSTCommand { get; set; }

        private IList<StoreResponse> storeList;

        private int storeTypeIndex;
        private bool isJST;

        public PackSkuSplitCountAndStoreWindow(int quantity, StoreResponse store, IList<StoreResponse> storeList, bool isJST)
        {
            InitializeComponent();
            this.DataContext = this;
            this.Quantity = quantity;
            this.Store = store;
            this.storeList = storeList;
            this.IsJST = isJST;
            if (!isJST)
            {
                StoreTypeIndex = store == null ? 1 : (int)store.Type;
            }

            SetStoreIndexCommand = new RelayCommand<int>(SetStoreIndex);
            SetJSTCommand = new RelayCommand(() => this.IsJST = true);
            this.Loaded += PackSkuSplitCountAndStoreWindow_Loaded;
        }

        private void SetStoreIndex(int index)
        {
            StoreTypeIndex = index;
        }

        private void OnStoreIndexChanged()
        {
            if (IsJST)
            {
                this.cbx_stroeList.ItemsSource = null;
                this.cbx_stroeList.SelectedItem = null;
                this.Store = null;
            }
            else
            {
                var type = (StockType)StoreTypeIndex;
                this.cbx_stroeList.ItemsSource = storeList.Where(s => s.Type == type);
                this.cbx_stroeList.SelectedItem = null;
            }
        }

        private void PackSkuSplitCountAndStoreWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            this.txtQuantity.Text = Quantity.ToString();


            if (isJST)
            {
                //this.cbx_stroeList.Text = Store.Name;

            }
            else
            {
                if (Store != null)
                    this.cbx_stroeList.SelectedItem = storeList.FirstOrDefault(s => s.Id == Store.Id);
            }
     

            //this.cbx_stroeList.ItemsSource = storeList;
            //if (Store != null)
            //    this.cbx_stroeList.SelectedItem = storeList.FirstOrDefault(s => s.Id == Store.Id);
            //else
            //    this.cbx_stroeList.SelectedItem = storeList.FirstOrDefault();
        }

        private void btn_save_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!int.TryParse(txtQuantity.Text, out int q))
            {
                MessageBox.Show("件数不正确", "提示");
                return;
            }
            if (q <= 0)
            {
                MessageBox.Show("件数不正确", "提示");
                return;
            }
            if (!IsJST && cbx_stroeList.SelectedItem == null)
            {
                MessageBox.Show("请选择一个仓库", "提示");
                return;
            }

            this.Quantity = q;
            this.Store = cbx_stroeList.SelectedItem as StoreResponse;

            this.DialogResult = true;
            this.Close();
        }
    }
}