// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2020 Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.



using System;
using System.Collections.Generic;
using System.Reflection;

namespace Hiperscan.Extensions.ApoIdentCommon.Scanner
{
    public class ScannerInfo
    {
        public static readonly DateTime ERROR_DATE = DateTime.MinValue;

        public ScannerInfo()
        {

        }

        public override string ToString()
        {
            int right_indent = 0;
            foreach (PropertyInfo info in this.GetType().GetProperties())
            {
                if (info.Name.Length > right_indent)
                    right_indent = info.Name.Length;
            }


            string value = "ScannerInfo:\n";
            foreach (PropertyInfo info in this.GetType().GetProperties())
            {
                string name = this.JustifyLeft(info.Name, right_indent);
                string prop_value = info.GetValue(this, null)?.ToString() ?? String.Empty;
                value += String.Format("\t{0}: {1}\n", name, prop_value);
            }

            return value;
        }

        private string JustifyLeft(string value, int right_justification)
        {
            return (value ?? String.Empty).PadRight(right_justification);
        }

        public string _WriteToSummarize()
        {
            SeparatedParams asp = new SeparatedParams("");
            asp.SetParamByID("ScannerInfo", "OK");

            foreach (PropertyInfo info in this.GetType().GetProperties())
                asp.SetParamByID(info.Name, info.GetValue(this, null)?.ToString() ?? String.Empty);

            string ss = asp.SumString(';');

            asp = null;
            return ss;
        }

        public ScannerInfo Clone()
        {
            ScannerInfo tmp = new ScannerInfo();
            foreach (PropertyInfo info in this.GetType().GetProperties())
            {
                foreach (PropertyInfo info_tmp in tmp.GetType().GetProperties())
                {
                    if (info.Name != info_tmp.Name)
                        continue;

                    if (info_tmp.SetMethod != null)
                        info_tmp.SetValue(tmp, info.GetValue(this, null));
                }
            }

            return tmp;
        }

        public bool _ReadFromSummarize(string avalue)
        {

            string ss = "";
            if (avalue.Length < 1)
                return false;
            SeparatedParams asp = new SeparatedParams(avalue);
            try
            {
                ss = asp.GetParamByID("ScannerInfo");
                if (ss.Equals("OK") == false)
                    return false;

                this.Ppn = asp.GetParamByID("PZN");
                this.RegisteredName = asp.GetParamByID("SubstanceName");
                this.SubstanceId = asp.GetParamByID("SubstanceID");
                this.SupplierName = asp.GetParamByID("SupplierName");
                this.BatchNumber = asp.GetParamByID("BatchNumber");
                try
                {
                    this.ExpirationDate = DateTime.Parse(asp.GetParamByID("ExpirationDate"));
                }
                catch (Exception)
                {
                    this.ExpirationDate = ScannerInfo.ERROR_DATE;
                }
   
                this.FullCode = asp.GetParamByID("FullCode");

                try
                {
                    this.AcquisitionTStamp = DateTime.Parse(asp.GetParamByID("AcqTimeStamp"));
                }
                catch (Exception)
                {
                    this.AcquisitionTStamp = ScannerInfo.ERROR_DATE;
                }

                this.DataCode = asp.GetParamByID("DataCode");
                this.WeightingCorrection = asp.GetParamByID("WeightingCorrection");

                return true;
            }
            finally
            {
                asp = null;
            }
        }

        public bool IsValid
        {
            get
            {
                return String.IsNullOrEmpty(this.SubstanceId) == false;
            }
        }

        public string Ppn { get; set; } = String.Empty;
        public string RegisteredName { get; set; } = String.Empty;
        public string SubstanceId { get; set; } = String.Empty;
        public string PrimaryDefaultName { get; set; } = String.Empty;
        public string PrimaryDefaultNameLatin { get; set; } = String.Empty;
        public string PrimaryDefaultNameEnglish { get; set; } = String.Empty;
        public List<string> AlternateNames { get; set; } = new List<string>();
        public string Monography { get; set; } = String.Empty;
        public string SupplierName { get; set; } = String.Empty;
        public string Amount { get; set; } = String.Empty;
        public string SupplierNo { get; set; } = String.Empty;

        public string BatchNumber { get; set; } = String.Empty;
        public string WeightingCorrection { get; set; } = String.Empty;
        public DateTime ExpirationDate { get; set; } = ScannerInfo.ERROR_DATE;

        public string FullCode { get; private set; } = string.Empty;
        public DateTime AcquisitionTStamp { get; private set; } = DateTime.Now;
        public string DataCode { get; private set; } = string.Empty;

        public bool CapsLockActive { get; set; } = false;




    }
}
