// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2022, 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.Globalization;
using Hiperscan.Unix;
using Hiperscan.ApoIdentCore.SectionManager.FieldDefinitions;
using Hiperscan.ApoIdentCommon.ApoConfig.Cache;
using Hiperscan.ApoIdentCommon.ApoConfig.Enums;

namespace Hiperscan.ApoIdentCore.SectionManager.Specimen.Fields
{
    public class ReferenceQuantityField : InputField<ReferenceQuantityEntry>
    {
        public ReferenceQuantityField()
        {
            this.Init();
        }

        public override void SetDefaultValue()
        {
            this.Entry = new ReferenceQuantityEntry();
        }

        private bool CheckQuantityValue()
        {
            if (String.IsNullOrEmpty(this.entry.QuantityValue))
                return this.entry.QuatityUnit == ReferenceQuantityTypes.None;

            return Double.TryParse(this.entry.QuantityValue, System.Globalization.NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out _);
        }

        private bool CheckQuantityUnit()
        {
            if (this.entry.QuatityUnit == ReferenceQuantityTypes.None)
                return String.IsNullOrEmpty(this.entry.QuantityValue) == true;

            return true;
        }

        protected override bool CheckEntry()
        {
            this.IsQuantityUnitValid = this.CheckQuantityUnit();
            this.IsQuantityValueValid = this.CheckQuantityValue();

            return this.IsQuantityUnitValid && this.IsQuantityValueValid;
        }

        protected override bool Compare(ReferenceQuantityEntry value1, ReferenceQuantityEntry value2)
        {
            if (value1 == null || value1 == null)
                return false;

            return value1.QuantityValue == value2.QuantityValue &&
                         value1.QuatityUnit == value2.QuatityUnit;
        }

        public override ReferenceQuantityEntry GetEntryValue()
        {
            ReferenceQuantityEntry tmp = new ReferenceQuantityEntry();
            tmp.QuantityValue = this.entry.QuantityValue;
            tmp.QuatityUnit = this.entry.QuatityUnit;
            return tmp;
        }

        public bool IsQuantityValueValid { get; private  set; }
        public bool IsQuantityUnitValid { get; private set; }

        public override string ErrorMessage => Catalog.GetString("Both fields either have to be filled in with a number or both fields must be empty.");
        public override int FieldLength => 5;
    }
}
