// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2021 Torsten Schache, 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.Text.RegularExpressions;

namespace Hiperscan.Extensions.ApoIdent
{
    public enum ProtocolStates
    {
        None = 0,
        TestUserCorrect = 1,
        SpecimentSupplierCorrect = 2,
        SpecimenBatchCorrect = 4,
        SpecimenExpiryDateCorrect = 8,
        IdentificationResultExists = 16,
        ResultAdditionalTestCorrect = 32,
        HasLabelPrinter = 128,
        ProtocolNotSaved = 256,

        ProtocolSectionActive = TestUserCorrect |
            SpecimenBatchCorrect |
            SpecimentSupplierCorrect |
            SpecimenExpiryDateCorrect |
            IdentificationResultExists |
            ResultAdditionalTestCorrect,

        ProtocolSaveActive = ProtocolSectionActive | ProtocolNotSaved,

        ProtocolLabelPrinterActive = ProtocolSectionActive | HasLabelPrinter,

    }

    public class ProtocolState : StateEnum<ProtocolStates>
    {
        public ProtocolState() : base()
        {

        }

        public override void DeleteFlag(ProtocolStates flag)
        {
            this.State &= (ProtocolStates)(uint.MaxValue - (uint)flag);
            base.DeleteFlag(flag);
        }

        public override void SetFlag(ProtocolStates flag)
        {
            this.State |= flag;
            base.SetFlag(flag);
        }

        public override bool HasFlag(ProtocolStates flag)
        {
            return (this.State & flag) == flag;
        }

        public void UpdateUserField(string input)
        {
            this.UpdateUserField(this.UserRegex.Match(input.Trim()).Success);
        }

        public void UpdateUserField(bool is_valid)
        {
            if (is_valid)
                this.SetFlag(ProtocolStates.TestUserCorrect);
            else
                this.DeleteFlag(ProtocolStates.TestUserCorrect);
        }

        public void UpdateSupplierField(bool is_valid)
        {
            if (is_valid)
                this.SetFlag(ProtocolStates.SpecimentSupplierCorrect);
            else
                this.DeleteFlag(ProtocolStates.SpecimentSupplierCorrect);
        }

        public void UpdateBatchField(bool is_valid)
        {
            if (is_valid)
                this.SetFlag(ProtocolStates.SpecimenBatchCorrect);
            else
                this.DeleteFlag(ProtocolStates.SpecimenBatchCorrect);
        }

        public void UpdateExpiryDateField(bool is_valid)
        {
            if (is_valid)
                this.SetFlag(ProtocolStates.SpecimenExpiryDateCorrect);
            else
                this.DeleteFlag(ProtocolStates.SpecimenExpiryDateCorrect);
        }

        private Regex user_regex = new Regex(@"\w{1}");
        public Regex UserRegex
        {
            get
            {
                return this.user_regex;
            }
        }

        private Regex supplier_regex = new Regex(@"\w{1}");
        public Regex SupplierRegex
        {
            get
            {
                return this.supplier_regex;
            }
        }

        private Regex batch_regex = new Regex(@"\w{1}");
        public Regex BatchRegex
        {
            get
            {
                return this.batch_regex;
            }
        }

        private Regex additional_test_regex = new Regex(@"\w{1}");
        public Regex AdditionalTestRegex
        {
            get
            {
                return this.additional_test_regex;
            }
        }
    }
}
