using BPA.Model;

namespace BPA.SingleDevice.ViewModel
{
    public class NewRecipeViewModel : NotifyBase, IDialogDataContext
    {
        public NewRecipeViewModel()
        {
            //SqlHelper.GetInstance.GetListAsync<RawMaterTB>().Result.OnSuccess(s =>
            //{
            //    var result = s.OrderBy(r => r.DeviceNum).ThenBy(r => r.WarehouseNum);
            //    foreach (var item in result)
            //    {
            //        NewRecipeModels.Add(new NewRecipeModel() { Name = item.Name, Id = item.Id });
            //    }
            //});

            //SqlHelper.GetInstance.GetListAsync<MaterialTB>().Result.OnSuccess(s =>
            //{
            //    foreach (var item in s)
            //    {
            //        NewRecipeModels.Add(new NewRecipeModel() { Name = item.Name, Id = item.MaterialCode });
            //    }
            //});
            //GetAllRecipeModel();

            SaveCommand = new BPARelayCommand(() =>
            {
                var result = new Parameters();
                result.Add("Name", RecipeName);
                result.Add("RawMater", NewRecipeModels.Where(p => p.IsChecked == true).ToList());
                RequestClose?.Invoke(result);
            });
            CancelCommand = new BPARelayCommand(() => { RequestClose?.Invoke(null); });
        }

        public string Title => "";

        public event Action<object> RequestClose;

        public async void OnDialogOpened(object parameters)
        {
            await GetAllRecipeModel();
            if (parameters is Parameters par)
            {
                if (par.TryGetValue("Name", out string name) && par.TryGetValue("RawMater", out List<NewRecipeModel> rms))
                {
                    RecipeName = name;
                    
                    rms.ForEach(item =>
                    {
                        var res = NewRecipeModels.FirstOrDefault(p => p.Id == item.Id);
                        if (res != null)
                        {
                            res.Weight = item.Weight;
                            res.IsChecked = true;
                        }
                    });
                }
            }
        }

        public ObservableCollection<NewRecipeModel> NewRecipeModels { get; set; } = new ObservableCollection<NewRecipeModel>();

        private async Task GetAllRecipeModel()
        {
            var result= await SqlHelper.GetInstance.GetListAsync<MaterialTB>();
            if (result.IsSuccess)
            {
                NewRecipeModels.Clear();
                foreach (var item in result.Content)
                {
                    NewRecipeModels.Add(new NewRecipeModel() { Name = item.Name, Id = item.MaterialCode });
                }
            }
        }

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

        public BPARelayCommand SaveCommand { get; set; }

        public BPARelayCommand CancelCommand { get; set; }
    }
}