using BBWY.Client.APIServices;
using BBWY.Client.Models.APIModel.Request;
using BBWY.Client.ViewModels;
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.Resources;
using System.Text;
using System.Windows.Input;
using System.Windows.Media.TextFormatting;

namespace BBWY.Client.Models.PackTask
{
    public class ConsumableModel : BaseVM, IDenpendency
    {
        public ConsumableService consumableService;
        public ICommand EditConsumableCommand { get; set; }
        public ICommand DeletedConsumableCommand { get; set; }
        public ICommand SaveConsumableCommand { get; set; }
        public Action ReflashDatas { get; set; }

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

            DeletedConsumableCommand = new RelayCommand<ObservableCollection<ConsumableModel>>(DeletedConsumable);
            SaveConsumableCommand = new RelayCommand<object>(SaveConsumable);
        }


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

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

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

            }

            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
            {
                new TipsWindow(res.Msg).Show();
            }
        }

        public void DeletedConsumable(ObservableCollection<ConsumableModel> list)
        {
            var res = consumableService.Deleted(Id);
            if (res.Success)
            {
                ViewModelLocator viewModel = new ViewModelLocator();
                var con = viewModel.Consumable;
                con.SearchConsumable();
                list.Remove(this);
            }
        }



        public void EditConsumable()
        {
            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 
             
            });
          
            add.Show();
        }

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


    }
}