using BBWY.Client.APIServices;
using BBWY.Client.Models.APIModel.Response.PackTask;
using BBWY.Client.Models.PackTask;
using BBWY.Client.Views.PackTask;
using BBWY.Common.Models;
using BBWY.Controls;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;

namespace BBWY.Client.ViewModels.PackTask
{
    public class ConsumableViewModel : BaseVM, IDenpendency
    {
        private string consumableTypeName;
        public string ConsumableTypeName
        { get => consumableTypeName; set { Set(ref consumableTypeName, value); } }
        private long? consumableTypeId;
        public long? ConsumableTypeId
        { get => consumableTypeId; set { Set(ref consumableTypeId, value); } }

        public ObservableCollection<GetConsumableTypeResponse> consumableTypeList = new ObservableCollection<GetConsumableTypeResponse>()
        {

        };
        public ObservableCollection<GetConsumableTypeResponse> ConsumableTypeList { get => consumableTypeList; set { Set(ref consumableTypeList, value); } }

        private bool isOpenAdd = true;
        public bool IsOpenAdd { get => isOpenAdd; set { Set(ref isOpenAdd, value); } }


        private int pageIndex = 1;
        public int PageIndex { get => pageIndex; set { Set(ref pageIndex, value); } }

        private int pageSize = 10;
        public int PageSize { get => pageSize; set { Set(ref pageSize, value); } }

        private int orderCount;//总数量
        public int OrderCount { get => orderCount; set { Set(ref orderCount, value); } }

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

        private string searchName;
        public string SearchName { get => searchName; set { Set(ref searchName, value); } }

        public ConsumableModel ConsumableModel { get => consumableModel; set { Set(ref consumableModel, value); } }

        private ConsumableModel consumableModel;


        private ObservableCollection<ConsumableModel> consumableList;
        public ObservableCollection<ConsumableModel> ConsumableList { get => consumableList; set { Set(ref consumableList, value); } }


        public ICommand TaskPageIndexChangedCommand { get; set; }
        public ICommand AddConsumableCommand { get; set; }
        public ICommand SearchConsumableCommand { get; set; }
       
        private readonly ConsumableService consumableService;

        public ConsumableViewModel(ConsumableService consumableService)
        {


            this.consumableService = consumableService;
            ConsumableModel = new ConsumableModel(consumableService);
            ConsumableList = new ObservableCollection<ConsumableModel>();
            AddConsumableCommand = new RelayCommand(AddConsumable);
            SearchConsumableCommand = new RelayCommand(SearchConsumable);
            TaskPageIndexChangedCommand = new RelayCommand<PageArgs>(p =>
            {
                LoadIndex(p.PageIndex);
            });
            SearchConsumable();
            GetConsumableTypeList();
        }

        void GetConsumableTypeList()
        {
            ConsumableTypeList = new ObservableCollection<GetConsumableTypeResponse>();
            var res = consumableService.GetConsumableTypeList();
            if (res.Success && res.Data != null)
            {
                ConsumableTypeList.Add(new GetConsumableTypeResponse { ConsumableTypeId = null, ConsumableTypeName = "全部" });
                foreach (var item in res.Data)
                {
                    ConsumableTypeList.Add(item);
                }  

            }
        }

        private void LoadIndex(int pageIndex)
        {
            PageIndex = pageIndex;//
            SearchConsumable();
        }


        public void SearchConsumable()
        {
            Task.Factory.StartNew(() =>
            {
                IsLoading = true;
                var res = consumableService.Search(new Models.APIModel.Request.ConsumableSearchRequest
                {
                    Name = searchName,
                    PageIndex = PageIndex,
                    PageSize = PageSize,
                    ConsumableTypeId = ConsumableTypeId,

                });
                if (res.Success)
                {
                    ConsumableList = new ObservableCollection<ConsumableModel>();
                    if (res.Data != null && res.Data.SearchConsumableList != null && res.Data.SearchConsumableList.Count() > 0)
                    {
                        OrderCount = res.Data.TotalCount;
                        foreach (var item in res.Data.SearchConsumableList)
                        {
                            App.Current.Dispatcher.Invoke(() =>
                            {
                                ConsumableList.Add(new ConsumableModel(consumableService)
                                {
                                    Heigth = item.Heigth,
                                    Id = item.Id,
                                    Name = item.Name,
                                    Length = item.Length,
                                    Price = item.Price,
                                    Remark = item.Remark,
                                    Weigth = item.Weigth,
                                    Width = item.Width,
                                    ConsumableTypeName =item.ConsumableTypeName,
                                    ConsumableTypeId = item.ConsumableTypeId,
                                });
                            });
                        }
                    }
                }
                IsLoading = false;
            });


        }

        private void AddConsumable()
        {
            //ConsumableModel = new ConsumableModel(consumableService);
            //IsOpenAdd = true;
            var model = new ConsumableModel(consumableService)
            { //ReflashDatas = ReflashDatas

            };

            model.ReflashDatas = new Action(() =>
            {
                SearchConsumable();
                Messenger.Default.Send(true);
            });

            EditConsumable add = new EditConsumable(model);
            add.ShowDialog();

        }
    }
}