using BBWY.Client.APIServices;
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 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 Action ReflashDatas { get; set; }

        public ConsumableViewModel(ConsumableService consumableService)
        {


            this.consumableService = consumableService;
            ConsumableModel = new ConsumableModel(consumableService);
            // ConsumableModel.ReflashDatas = ReflashDatas;
            ConsumableList = new ObservableCollection<ConsumableModel>();
            AddConsumableCommand = new RelayCommand(AddConsumable);
            SearchConsumableCommand = new RelayCommand(SearchConsumable);
            TaskPageIndexChangedCommand = new RelayCommand<PageArgs>(p =>
            {
                LoadIndex(p.PageIndex);
            });
            SearchConsumable();
        }
        private void LoadIndex(int pageIndex)
        {
            PageIndex = pageIndex;//
            SearchConsumable();
        }


        private void SearchConsumable()
        {
            Task.Factory.StartNew(() =>
            {
                IsLoading = true;
                var res = consumableService.Search(new Models.APIModel.Request.ConsumableSearchRequest
                {
                    Name = searchName,
                    PageIndex = PageIndex,
                    PageSize = PageSize,
                });
                if (res.Success)
                {
                    ConsumableList = new ObservableCollection<ConsumableModel>();
                    if (res.Data != null && res.Data.Consumables != null && res.Data.Consumables.Count() > 0)
                    {
                        OrderCount = res.Data.OrderCount;
                        foreach (var item in res.Data.Consumables)
                        {
                            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

                                });
                            });
                        }
                    }
                }
                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();

        }
    }
}