using HandyControl.Controls;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows.Controls;
using TouchSocket.Core;
using WCS.Model;
using WCS.Model.ApiModel.Home;
using WCS.Model.ApiModel.LocationInfo;
using WCS.Model.ApiModel.StoreInfo;
using 智慧物流软件系统.Tool;

namespace 智慧物流软件系统.ViewModel
{
    public class LocationInfoAddOrUpdateViewModel : BindableBase
    {
        public HandyControl.Controls.CheckComboBox checkComboBox { get; set; }
        #region Property 属性
        public LocationInfoAddOrUpdateViewModel()
        {
            
        }

        public void InitTypes()
        {
            try
            {
                var task1 = Task.Run(() =>
                {
                    LocationAreaItems = GetBaseData.GetLocationAreaInfos();
                    if (LocationAreaItems != null && LocationAreaItems.Count > 0)
                        SelectedLocationAreaItem = LocationAreaItems.First();

                });

                var task2 = Task.Run(() =>
                {
                    AllowShelfTypes = new List<ShelfTypeModel>();
                    AllowShelfTypes = GetBaseData.GetShelfType()
                                                 //.Select(t => t.ShelfTypeName)
                                                 .ToList();
                });

                Task.WaitAll([task1, task2]);
            }
            catch (Exception ex)
            {
                Growl.Warning("打开窗体失败，请关闭后重试!");
            }
        }

        public void SetValues(LocationInfoModel locationInfoModel)
        {
            if (locationInfoModel == null)
            {
                locationInfoModel = new LocationInfoModel();
            }
            LocationId = locationInfoModel.Id;

            var item = LocationAreaItems.FirstOrDefault(t => t.Id == locationInfoModel.LocationAreaId);
            if (item != null)
            {
                SelectedLocationAreaItem = item;
            }
            LocationCode = locationInfoModel.LocationCode;
            RcsStoreCode = locationInfoModel.RcsStoreCode;
            //comboBox.selec
            App.Current.Dispatcher.Invoke(() =>
            {
                locationInfoModel.AllowShelfTypesName?.ForEach(t => 
                checkComboBox.SelectedItems.Add(AllowShelfTypes.Where(a => a.ShelfTypeName == t).FirstOrDefault()));
            });

            IsEnable = locationInfoModel.IsEnable;
        }

        public LocationInfoModel GetValues()
        {
            var selectShelfTypes = checkComboBox.SelectedItems?.Cast<ShelfTypeModel>().Select(t => t.Id).ToList();

            return new LocationInfoModel()
            {
                Id = LocationId,
                LocationAreaId = SelectedLocationAreaItem.Id,
                LocationArea = SelectedLocationAreaItem.LocationAreaName,
                LocationCode = LocationCode,
                RcsStoreCode = RcsStoreCode,
                ModifyUser = LocalStatic.CurrentUser,
                AllowShelfTypes = selectShelfTypes,
                IsEnable = IsEnable,
            };
        }

        private List<LocationAreaInfoModel> locationAreaItems;
        public List<LocationAreaInfoModel> LocationAreaItems
        {
            get { return locationAreaItems; }
            set
            {
                SetProperty(ref locationAreaItems, value);
            }
        }

        private LocationAreaInfoModel selectedLocationAreaItem;
        public LocationAreaInfoModel SelectedLocationAreaItem
        {
            get { return selectedLocationAreaItem; }
            set
            {
                SetProperty(ref selectedLocationAreaItem, value);
            }
        }

        private int locationId;
        public int LocationId
        {
            get { return locationId; }
            set
            {
                SetProperty(ref locationId, value);
            }
        }

        private string locationCode;
        public string LocationCode
        {
            get { return locationCode; }
            set
            {
                SetProperty(ref locationCode, value);
            }
        }

        /// <summary>
        /// 允许放置的货架类型
        /// </summary>
        private List<ShelfTypeModel> allowShelfTypes;
        public List<ShelfTypeModel> AllowShelfTypes
        {
            get { return allowShelfTypes; }
            set
            {
                SetProperty(ref allowShelfTypes, value);
            }
        }

        private string rcsStoreCode;
        public string RcsStoreCode
        {
            get { return rcsStoreCode; }
            set
            {
                SetProperty(ref rcsStoreCode, value);
            }
        }

        private bool isEnable;
        public bool IsEnable
        {
            get { return isEnable; }
            set
            {
                SetProperty(ref isEnable, value);
            }
        }
        #endregion
    }
}
