// 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;
namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        private void OnPpnEntryChanged(object sender, EventArgs e)
        {
            if (this.Control.IsGuiUpdating)
                return;
                
            string text = this.SpecimenPpnEntry.GtkEntry.Text;

            string corrected = String.Empty;
            foreach (char c in text)
            {
                switch (c)
                {
                    case '0':
                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                    case '8':
                    case '9':
                        corrected += c.ToString();
                        break;

                    default:
                        break;        
                }
            }

            string guid = Guid.NewGuid().ToString();
            this.Control.EnterGui(guid);
            this.SpecimenPpnEntry.GtkEntry.Text = corrected;
            this.Control.LeaveGui(guid);
            this.Control.WaitForGuiUpdateFinished(guid);

            if (this.PpnEntryOld == corrected)
                return;

            this.PpnEntryOld = corrected;
            this.Control.ProtocolState.SetFlag(ProtocolStates.ProtocolNotSaved);
            this.Update(ApoIdentApplicationState.ProtocolDataChanged);
        }

        private void OnSupplierEntryChanged(object sender, EventArgs e)
        {
            if (this.Control.IsGuiUpdating)
                return;

            this.Control.ProtocolState.UpdateSupplierField(this.SpecimenSupplierEntry.IsValid);
            this.Control.ProtocolState.SetFlag(ProtocolStates.ProtocolNotSaved);
            this.Update(ApoIdentApplicationState.ProtocolDataChanged);
        }

        private void OnBatchEntryChanged(object sender, EventArgs e)
        {
            if (this.Control.IsGuiUpdating)
                return;

            this.Control.ProtocolState.UpdateBatchField(this.SpecimenBatchEntry.IsValid);
            this.Control.ProtocolState.SetFlag(ProtocolStates.ProtocolNotSaved);
            this.Update(ApoIdentApplicationState.ProtocolDataChanged);
        }

        private void OnWeighingFactorEntryChanged(object sender, EventArgs e)
        {
            if (this.Control.IsGuiUpdating)
                return;
                
            this.Control.ProtocolState.SetFlag(ProtocolStates.ProtocolNotSaved);
            this.Update(ApoIdentApplicationState.ProtocolDataChanged);
        }

        private void OnExpiryDateChanged(object sender, EventArgs e)
        {
            if (this.Control.IsGuiUpdating)
                return;

            this.Control.ProtocolState.UpdateExpiryDateField(this.SpecimenExpiryDateBox.IsValid);
            this.Control.ProtocolState.SetFlag(ProtocolStates.ProtocolNotSaved);
            this.Update(ApoIdentApplicationState.ProtocolDataChanged);
        }

        private void OnBatchDeadLineClicked(object sender, EventArgs e)
        {
            if (this.Control.IsGuiUpdating == true)
                return;

            BatchDeadLine.SetNextDeadLineType();
            this.Control.ProtocolState.SetFlag(ProtocolStates.ProtocolNotSaved);
            this.Update(ApoIdentApplicationState.ProtocolBatchDeadLineChanged);
        }

        private string PpnEntryOld = String.Empty;

    }
}
