using MainShell.Recipe.Models;
using Stylet;

namespace MainShell.Recipe.ViewModel
{
    public class ProcessCompensationViewModel : PropertyChangedBase
    {
        private ProcessRecipe _processRecipe;
        public ProcessRecipe ProcessRecipe
        {
            get { return _processRecipe; }
            set
            {
                if (SetAndNotify(ref _processRecipe, value))
                {
                    NotifyCompensationPropertiesChanged();
                }
            }
        }

        public double PhsX1Compensation
        {
            get { return GetCompensationParameter().PhsX1Compensation; }
            set
            {
                ProcessAxisCompensationParameter parameter = GetCompensationParameter();
                if (parameter.PhsX1Compensation == value)
                {
                    return;
                }

                parameter.PhsX1Compensation = value;
                NotifyOfPropertyChange(nameof(PhsX1Compensation));
            }
        }

        public double PhsY1Compensation
        {
            get { return GetCompensationParameter().PhsY1Compensation; }
            set
            {
                ProcessAxisCompensationParameter parameter = GetCompensationParameter();
                if (parameter.PhsY1Compensation == value)
                {
                    return;
                }

                parameter.PhsY1Compensation = value;
                NotifyOfPropertyChange(nameof(PhsY1Compensation));
            }
        }

        public double WsXCompensation
        {
            get { return GetCompensationParameter().WsXCompensation; }
            set
            {
                ProcessAxisCompensationParameter parameter = GetCompensationParameter();
                if (parameter.WsXCompensation == value)
                {
                    return;
                }

                parameter.WsXCompensation = value;
                NotifyOfPropertyChange(nameof(WsXCompensation));
            }
        }

        public double StageYCompensation
        {
            get { return GetCompensationParameter().StageYCompensation; }
            set
            {
                ProcessAxisCompensationParameter parameter = GetCompensationParameter();
                if (parameter.StageYCompensation == value)
                {
                    return;
                }

                parameter.StageYCompensation = value;
                NotifyOfPropertyChange(nameof(StageYCompensation));
            }
        }

        private ProcessAxisCompensationParameter GetCompensationParameter()
        {
            if (ProcessRecipe == null)
            {
                return new ProcessAxisCompensationParameter();
            }

            if (ProcessRecipe.ProcessAxisCompensationParameter == null)
            {
                ProcessRecipe.ProcessAxisCompensationParameter = new ProcessAxisCompensationParameter();
            }

            return ProcessRecipe.ProcessAxisCompensationParameter;
        }

        private void NotifyCompensationPropertiesChanged()
        {
            NotifyOfPropertyChange(nameof(PhsX1Compensation));
            NotifyOfPropertyChange(nameof(PhsY1Compensation));
            NotifyOfPropertyChange(nameof(WsXCompensation));
            NotifyOfPropertyChange(nameof(StageYCompensation));
        }
    }
}
