using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using Microsoft.Toolkit.Mvvm.Input;
using BPASmartClient.Helper;
using BPASmartClient.DosingSystem.Model;

namespace BPASmartClient.DosingSystem.ViewModel
{
    public class NewRecipeViewModel : ObservableObject
    {
        public NewRecipeViewModel()
        {
            ActionManage.GetInstance.Register(new Action<object>((o) =>
            {
                if (o != null && o is RecipeModel rm)
                {
                    RecipeName = rm.RecipeName;
                    foreach (var item in rm.RawMaterials)
                    {
                        RawMaterials.Add(item);
                    }
                    RecipCode = rm.RecipCode;
                }
            }), "Details");

            AddCommand = new RelayCommand(() =>
            {
            p:
                string guid = Guid.NewGuid().ToString();
                if (RawMaterials.FirstOrDefault(p => p.RawMaterialId == guid) == null)
                {
                    RawMaterials.Add(new RawMaterialModel()
                    {
                        RawMaterialId = guid
                    });
                }
                else goto p;
            });

            RemoveCommand = new RelayCommand<object>((obj) =>
            {
                if (obj is string rm)
                {
                    var res = RawMaterials.FirstOrDefault(p => p.RawMaterialId == rm);
                    if (res != null) RawMaterials.Remove(res);
                }
            });

            SaveCommand = new RelayCommand(() =>
            {
                for (int i = 0; i < RawMaterials.Count; i++)
                {
                    var res = DeviceListViewModel.devices.FirstOrDefault(p => p.DeviceName == RawMaterials.ElementAt(i).RawMaterialName);
                    if (res != null) RawMaterials.ElementAt(i).DeviceIp = res.IpAddress;
                }

                if (RecipCode.Length <= 0)
                {
                    var res = Array.FindIndex(Json<LocaPar>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);

                    if (res >= 0 && res < Json<LocaPar>.Data.Recipes.Count)
                    {
                        ErrorInfo = "该配方已存在，请重新输入";
                    }
                    else
                    {
                        AddRecipes();
                    }
                }
                else
                {
                    var res = Array.FindIndex(Json<LocaPar>.Data.Recipes.ToArray(), p => p.RecipCode == RecipCode);
                    if (res >= 0 && res < Json<LocaPar>.Data.Recipes.Count)
                    {
                        Json<LocaPar>.Data.Recipes.ElementAt(res).RecipeName = RecipeName;
                        Json<LocaPar>.Data.Recipes.ElementAt(res).RawMaterials.Clear();
                        foreach (var item in RawMaterials)
                        {
                            Json<LocaPar>.Data.Recipes.ElementAt(res).RawMaterials.Add(item);
                        }
                    }
                }

                ActionManage.GetInstance.Send("CloseNewRecipeView");
            });
        }

        private void AddRecipes()
        {
            string date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
            var dates = date.Split("-").ToList();
            StringBuilder sb = new StringBuilder();
            dates?.ForEach((item) => { sb.Append(item); });
            Json<LocaPar>.Data.Recipes.Add(new RecipeModel()
            {
                SerialNum = Json<LocaPar>.Data.Recipes.Count + 1,
                RawMaterials = RawMaterials,
                RecipCode = sb.ToString(),
                RecipeName = RecipeName,
            });
        }

        static NewRecipeViewModel()
        {
            RawMaterialNames.Clear();
        }

        private string RecipCode = string.Empty;

        public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
        private string _mRecipeName;

        public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
        private string _mErrorInfo;

        public RelayCommand AddCommand { get; set; }

        public RelayCommand<object> RemoveCommand { get; set; }

        public RelayCommand SaveCommand { get; set; }

        public ObservableCollection<RawMaterialModel> RawMaterials { get; set; } = new ObservableCollection<RawMaterialModel>();

        public static ObservableCollection<string> RawMaterialNames { get; set; } = new ObservableCollection<string>();
    }
}
