using BBWY.Client.APIServices;
using BBWY.Client.Models.APIModel.Request;
using BBWY.Client.Models;
using BBWY.Client.Models.PackTask;
using BBWY.Client.Views.PackTask;
using BBWY.Common.Models;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Input;
using BBWY.Client.Models.APIModel.Response.PackTask;

namespace BBWY.Client.ViewModels.PackTask
{
    public class EditConsumableViewModel:BaseVM,IDenpendency
    {
        public ObservableCollection<GetConsumableTypeResponse> consumableTypeList = new ObservableCollection<GetConsumableTypeResponse>()
        {

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

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


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



        public ConsumableService consumableService;
        public ICommand EditConsumableCommand { get; set; }
        public ICommand DeletedConsumableCommand { get; set; }
        public ICommand SaveConsumableCommand { get; set; }
        public ICommand AddConsumableTypeCommand { get; set; }
        public Action ReflashDatas { get; set; }

        public EditConsumableViewModel(ConsumableService consumableService)
        {
            this.consumableService = consumableService;
            EditConsumableCommand = new RelayCommand(EditConsumable);

            DeletedConsumableCommand = new RelayCommand<ObservableCollection<ConsumableModel>>(DeletedConsumable);
            SaveConsumableCommand = new RelayCommand<object>(SaveConsumable);
            if (consumableService != null)
            {
                GetConsumableTypeList();
            }
            AddConsumableTypeCommand = new RelayCommand(AddConsumableType);
        }

        private void AddConsumableType()
        {
            var res = consumableService.AddConsumableType(AddConsumableTypeName);
            if (res.Success)
            {
                GetConsumableTypeList();
            }
        }

        private long consumableTypeId;
        public long ConsumableTypeId
        { get => consumableTypeId; set { Set(ref consumableTypeId, value); } }


        public void SaveConsumable(object obj)
        {
            if (string.IsNullOrEmpty(Name))
            {
                new TipsWindow("耗材品名不能为空!").Show();
                return;
            }

            if (this.Price == null)
            {
                new TipsWindow("价格不能为空!").Show();
                return;
            }
            ApiResponse<object> res = null;
            if (Id > 0)//修改
            {
                res = consumableService.Edit(new ConsuableRequest
                {
                    Id = Id,
                    Heigth = Heigth,
                    Length = Length,
                    Name = Name,
                    Price = Price.Value,
                    Remark = Remark,
                    Weigth = Weigth,
                    Width = Width,
                    ConsumableTypeId = ConsumableTypeId,
                });

            }
            else
            {
                res = consumableService.Add(new ConsuableRequest
                {

                    Heigth = Heigth,
                    Length = Length,
                    Name = Name,
                    Price = Price.Value,
                    Remark = Remark,
                    Weigth = Weigth,
                    Width = Width,
                    ConsumableTypeId = ConsumableTypeId,
                });

            }

            if (res != null && res.Success)
            {
                var win = obj as System.Windows.Window;

                ViewModelLocator viewModel = new ViewModelLocator();
                var con = viewModel.Consumable;
                con.SearchConsumable();
                //if (ReflashDatas != null) ReflashDatas();
                win.Close();
            }
            else
            {
                if (res != null)
                    new TipsWindow(res.Msg).Show();
            }
        }
      public  ConsumableModel model;   

        public void DeletedConsumable(ObservableCollection<ConsumableModel> list)
        {
            MessageBoxResult result = System.Windows.MessageBox.Show("是否删除?", "提示",
                                             MessageBoxButton.YesNo,
                                             MessageBoxImage.Warning);
            if (result != MessageBoxResult.Yes) return;

            var res = consumableService.Deleted(Id);
            if (res.Success)
            {
                ViewModelLocator viewModel = new ViewModelLocator();
                var con = viewModel.Consumable;
                con.SearchConsumable();
                list.Remove(model);
            }
        }



        void GetConsumableTypeList()
        {
            var res = consumableService.GetConsumableTypeList();
            if (res.Success && res.Data != null)
            {
                ConsumableTypeList = res.Data;

            }
        }


        public void EditConsumable()
        {
            GetConsumableTypeList();
            EditConsumable add = new EditConsumable(new ConsumableModel(consumableService)
            {
                Heigth = this.Heigth,
                Length = this.Length,
                Id = this.Id,
                Name = this.Name,
                Weigth = this.Weigth,
                Remark = this.Remark,
                Width = this.Width,
                Price = this.Price,
                ConsumableTypeId = this.ConsumableTypeId,
                ConsumableTypeList = this.ConsumableTypeList,
                ConsumableTypeName = this.ConsumableTypeName,

            });

            add.ShowDialog();

        }

        private long id;
        public long Id { get => id; set { Set(ref id, value); } }
        private string name;
        public string Name { get => name; set { Set(ref name, value); } }
        private decimal? price;
        public decimal? Price { get => price; set { Set(ref price, value); } }
        private double? weigth;
        public double? Weigth { get => weigth; set { Set(ref weigth, value); } }
        private double? length;
        public double? Length { get => length; set { Set(ref length, value); } }
        private double? width;
        public double? Width { get => width; set { Set(ref width, value); } }
        private double? heigth;
        public double? Heigth { get => heigth; set { Set(ref heigth, value); } }
        private string remark;
        public string Remark { get => remark; set { Set(ref remark, value); } }
    }
}