using System;
using System.ComponentModel;

namespace SWRIS.Models
{
    public class SoftAuthorizeModel : INotifyPropertyChanged
    {
        private string machineCode;
        private FinalData finalData = new FinalData();
        public string MachineCode
        {
            get { return machineCode; }
            set { machineCode = value; OnPropertyChanged("MachineCode"); }
        }
        public FinalData FinalData
        {
            get { return finalData; }
            set { finalData = value; OnPropertyChanged("FinalData"); }
        }
        public string FinalCode => FinalData?.ToString();

        public event PropertyChangedEventHandler PropertyChanged;

        protected internal virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public class FinalData : INotifyPropertyChanged
    {

        private string first;
        private string second;
        private string third;
        private string fourth;

        public string First
        {
            get { return first; }
            set { first = value; OnPropertyChanged("First"); }
        }
        public string Second
        {
            get { return second; }
            set { second = value; OnPropertyChanged("Second"); }
        }
        public string Third
        {
            get { return third; }
            set { third = value; OnPropertyChanged("Third"); }
        }
        public string Fourth
        {
            get { return fourth; }
            set { fourth = value; OnPropertyChanged("Fourth"); }
        }

        public override string ToString()
        {
            return first + second + third + fourth;
        }
        public void Set(string finalCode)
        {
            if (finalCode is null)
            {
                throw new ArgumentNullException(nameof(finalCode));
            }
            finalCode = finalCode.Replace("-", "");
            First = finalCode.Length > 4 ? finalCode.Substring(0, 4) : finalCode;
            Second = finalCode.Length > 8 ? finalCode.Substring(4, 4) : finalCode.Substring(4);
            Third = finalCode.Length > 12 ? finalCode.Substring(8, 4) : finalCode.Substring(8);
            Fourth = finalCode.Length > 16 ? finalCode.Substring(12, 4) : finalCode.Substring(12);
        }
        public event PropertyChangedEventHandler PropertyChanged;

        protected internal virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
