using CommunityToolkit.Mvvm.Input;
using MegaRobo.C00225155.AppServer;
using MegaRobo.C00225155.Entities.Entity_DB;
using MegaRobo.Contract;
using MegaRobo.WpfInfrastructure.Abstractions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MegaRobo.C00225155App
{
    public class DataResultViewModel : ViewModelBaseEx
    {
        private WorkService workService { get; set; }

        public ICommand CloseNoticeCommand { get; set; }

        private bool hasMadeTask = false;
        public bool HasMadeTask
        {
            get => hasMadeTask;
            set => SetProperty(ref hasMadeTask, value);
        }

        private string noticeinfo;
        public string NoticeInfo
        {
            get { return noticeinfo; }
            set { SetProperty(ref noticeinfo, value); }
        }

        private bool showNotice;
        public bool ShowNotice
        {
            get { return showNotice; }
            set { SetProperty(ref showNotice, value); }
        }

        private ObservableCollection<DataResult_Dose> isUsingResultModeList = new ObservableCollection<DataResult_Dose>();
        /// <summary>
        /// 绑定的样品瓶结果列表
        /// </summary>
        public ObservableCollection<DataResult_Dose> IsUsingResultModeList
        {
            get { return isUsingResultModeList; }
            set
            {
                SetProperty(ref isUsingResultModeList, value);
                //OnPropertyChanged(nameof(IsUsingResultModeList));
            }
        }

        public DataResultViewModel()
        {

        }

        public override void Initialize(ICoreService coreService, string title, params object[] args)
        {
            base.Initialize(coreService, title, args);
        }

        protected override void LoadServices()
        {
            base.LoadServices();
            workService = this.CoreService.GetServiceInstance<WorkService>();
            workService.stationService_Dose.UpdateAdditiveEvent += UpdateAdditivesEvent_Dose;
        }

        protected override void LoadDatas()
        {
            base.LoadDatas();
        }

        protected override void Register()
        {
            base.Register();
            CloseNoticeCommand = new RelayCommand(OnCloseNoticeCommand);
        }

        private void OnCloseNoticeCommand()
        {
            ShowNotice = false;
        }

        private async void UpdateAdditivesEvent_Dose(object sender, List<DataResult_Dose> e)
        {
            var data = e.ToList();
            try
            {
                if (e == null || e.Count == 0)
                {
                    IsUsingResultModeList = new ObservableCollection<DataResult_Dose>();
                    HasMadeTask = false;
                    return;
                }

                var list = new ObservableCollection<DataResult_Dose>();
                foreach (var taskAdd in e)
                {
                    list.Add(new DataResult_Dose()
                    {
                        BoxId = taskAdd.BoxId,
                        BottleId = taskAdd.BottleId,
                        SampleCode = taskAdd.SampleCode,
                        LiquidAdditives = taskAdd.LiquidAdditives,
                        PowderAdditives = taskAdd.PowderAdditives,
                    });
                }

                IsUsingResultModeList = list;
                HasMadeTask = list.Count() > 0;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
