using MainShell.DeviceMaintance.Model;
using MainShell.ParaSetting.Model;
using MaxwellFramework.Core.Attributes;
using MaxwellFramework.Core.Interfaces;
using MwFramework.ManagerService;
using MXJM.Parameter.Maintance;
using Stylet;
using StyletIoC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static MainShell.ParaSetting.Model.SpeedSetting;

namespace MainShell.Parameter
{
    [Singleton]
    public class GlobalParameterContext : IHandle<CopyParameterWriteArgs>
    {
        private readonly IParamList _paramlist;
        private readonly object _lock = new object();

        public GlobalParameterContext(IParameterManager parameterManager, IEventAggregator eventAggregator)
        {
            _paramlist = parameterManager as IParamList;
            _heightBaseSetting = _paramlist.GetParameter<HeightBaseSetting>();
            _speedParaSysSetting = _paramlist.GetParameter<SpeedParaSysSetting>();
            _needleCalibrationSetting = _paramlist.GetParameter<NeedleCalibrationSetting>();
            _needlePrintCalibrateSetting = _paramlist.GetParameter<NeedlePrintCalibrateSetting>();
            _runSetting = _paramlist.GetParameter<RunSetting>();
            _deviceFoundationSetting = _paramlist.GetParameter<DeviceFoundationSetting>();
            eventAggregator.Unsubscribe(this);
            eventAggregator.Subscribe(this);
        }

        private readonly HeightBaseSetting _heightBaseSetting;
        public HeightBaseSetting HeightBaseSetting => _heightBaseSetting;

        private readonly SpeedParaSysSetting _speedParaSysSetting;
        public SpeedParaSysSetting SpeedParaSysSetting => _speedParaSysSetting;

        private readonly NeedleCalibrationSetting _needleCalibrationSetting;
        public NeedleCalibrationSetting NeedleCalibrationSetting => _needleCalibrationSetting;

        private readonly NeedlePrintCalibrateSetting _needlePrintCalibrateSetting;
        public NeedlePrintCalibrateSetting NeedlePrintCalibrateSetting => _needlePrintCalibrateSetting;

        private readonly RunSetting _runSetting;
        public RunSetting RunSetting => _runSetting;

        private readonly DeviceFoundationSetting _deviceFoundationSetting;
        public DeviceFoundationSetting DeviceFoundationSetting => _deviceFoundationSetting;

        public void Handle(CopyParameterWriteArgs message)
        {
            lock (_lock)
            {
                if (message == null || message.WriteRead == 1)
                {
                    return;
                }

                if (message.Parameter is HeightBaseSetting)
                {
                    _heightBaseSetting.Copy(message.Parameter);
                }
                else if (message.Parameter is SpeedParaSysSetting)
                {
                    _speedParaSysSetting.Copy(message.Parameter);
                }
                else if (message.Parameter is NeedleCalibrationSetting)
                {
                    _needleCalibrationSetting.Copy(message.Parameter);
                }
                else if (message.Parameter is NeedlePrintCalibrateSetting)
                {
                    _needlePrintCalibrateSetting.Copy(message.Parameter);
                }
                else if (message.Parameter is RunSetting)
                {
                    _runSetting.Copy(message.Parameter);
                }
                else if (message.Parameter is DeviceFoundationSetting)
                {
                    _deviceFoundationSetting.Copy(message.Parameter);
                }
            }
        }
    }
}
