using K11_CNC.Model;
using K11_CNC.ViewModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace K11_CNC.ViewModel
{
    public class PositionViewModel : ViewModelBase
    {
        #region "Private Felder"
        private Position _position;
        private Pint _id;
        private Pint _XPos;
        private Pint _YPos;
        private Pint _ZPos;
        private Pint _dwell;
        private Pint _DaliAdr;
        private Pint _DaliVal;
        private string _changed = string.Empty;
        #endregion

        #region Konstruktor
        public PositionViewModel(Position position)
        {
            if (position != null)
            {
                _position = position;
                initializeFields();
            }
            else
            {
                _position = new Position();
                initializeFields();
                // die folgenden Anweisungen werden benötigt, um beim Hinzufügen einer neuen Person die Änderung in der View visualisieren zu können
                _id.HasChanged = true;
                _XPos.HasChanged = true;
                _YPos.HasChanged = true;
                _ZPos.HasChanged = true;
                _dwell.HasChanged = true;
                _DaliAdr.HasChanged = true;
                _DaliVal.HasChanged = true;
                Changed = "*";
            }
            _id.PropertyChanged += position_PropertyChanged;
            _XPos.PropertyChanged += position_PropertyChanged;
            _YPos.PropertyChanged += position_PropertyChanged;
            _ZPos.PropertyChanged += position_PropertyChanged;
            _dwell.PropertyChanged += position_PropertyChanged;
        }
        #endregion

        #region "Private Methoden"
        private void initializeFields()
        {
            _id = new Pint(_position.Id);
            _XPos = new Pint(_position.XPos);
            _YPos = new Pint(_position.YPos);
            _ZPos = new Pint(_position.ZPos);
            _dwell = new Pint(_position.Dwell);
            _DaliAdr = new Pint(_position.DaliAdr);
            _DaliVal = new Pint(_position.DaliVal);
        }

        // Ereignishandler
        private void position_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (_id.HasChanged || _XPos.HasChanged || _YPos.HasChanged || _ZPos.HasChanged || _dwell.HasChanged || _DaliAdr.HasChanged || _DaliVal.HasChanged) 
                Changed = "*";
            else 
                Changed = string.Empty;
        }

        #endregion

        #region "Öffentliche Eigenschaften"
        public Pint Id { get { return _id; } }
        public Pint XPos { get { return _XPos; } }
        public Pint YPos { get { return _YPos; } }
        public Pint ZPos { get { return _ZPos; } }
        public Pint Dwell { get { return _dwell; } }
        public Pint DaliAdr { get { return _DaliAdr; } }
        public Pint DaliVal { get { return _DaliVal; } }
        public string Changed
        {
            get { return _changed; }
            set { SetProperty<string>(ref _changed, value); }
        }
        #endregion

        #region "Methoden"

        public void UndoChanges()
        {
            _id.UndoChanges();
            _XPos.UndoChanges();
            _YPos.UndoChanges();
            _ZPos.UndoChanges();
            _dwell.UndoChanges();
            _DaliAdr.UndoChanges();
            _DaliVal.UndoChanges();
        }

        public void AcceptChanges()
        {
            _id.AcceptChanges();
            _XPos.AcceptChanges();
            _YPos.AcceptChanges();
            _ZPos.AcceptChanges();
            _dwell.AcceptChanges();
            _DaliAdr.AcceptChanges();
            _DaliVal.AcceptChanges();
        }

        #endregion
    }
}
