using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AIStudio.Wpf.DiagramDesigner;
using AIStudio.Wpf.DiagramDesigner.Models;

namespace AIStudio.Wpf.SFC.ViewModels
{
    public class Simulate_TankViewModel : SFCNode
    {
        public Simulate_TankViewModel() : this(null)
        {
    
        }

        public Simulate_TankViewModel(IDiagramViewModel root) : base(root, SFCNodeKinds.Simulate_Tank)
        {
            ItemWidth = 50;
            ItemHeight = 120;

            ExecuteAddLeftInput(null);
            ExecuteAddLeftInput(null);
            ExecuteAddRightOutput(null);
            ExecuteAddRightOutput(null);
        }

        public Simulate_TankViewModel(IDiagramViewModel root, SelectableItemBase designer) : base(root, designer)
        {
        }

        public Simulate_TankViewModel(IDiagramViewModel root, SerializableItem serializableItem, string serializableType) : base(root, serializableItem, serializableType)
        {

        }

        public override void ExecuteAddLeftInput(object parameter)
        {
            FullyCreatedConnectorInfo connector = new FullyCreatedConnectorInfo(this, ConnectorOrientation.Left, true);
            connector.XRatio = 0;
            connector.YRatio = (30 + Input.Count * 60) / ItemHeight;
            Input.Add(Input.Count, connector);

            AddConnector(connector);
        }

        public override void ExecuteAddRightOutput(object parameter)
        {
            FullyCreatedConnectorInfo connector = new FullyCreatedConnectorInfo(this, ConnectorOrientation.Right, true);
            connector.XRatio = 1;
            connector.YRatio = (30 + Output.Count * 60) / ItemHeight;
            Output.Add(Output.Count, connector);


            AddConnector(connector);
        }


        /// <summary>
        /// 液位
        /// </summary>
        private LinkPoint linkPoint;
        public LinkPoint LinkPoint
        {
            get
            {
                return linkPoint;
            }
            set
            {
                SetProperty(ref linkPoint, value);
            }
        }

        protected override void ExecuteEditCommand(object parameter)
        {
            Simulate_TankViewModelData data = new Simulate_TankViewModelData(LinkPoint);
            if (visualiserService.ShowDialog(data) == true)
            {
                this.LinkPoint = data.LinkPoint;
            }
        }

        public void Execute()
        {
            double input = PreNode.OfType<Simulate_SolenoidViewModel>().Sum(p => p.DOLinkPoint?.Value ?? 0);
            double output = NextNode.OfType<Simulate_SolenoidViewModel>().Sum(p => p.DOLinkPoint?.Value ?? 0);
            if (LinkPoint != null)
            {
                LinkPoint.Value += input - output;
            }
        }
    }
}
