
// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2023. 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 Hiperscan.Unix;
using Hiperscan.Common.Checksum;
using Hiperscan.GuiExtensions.Dialog;
using Hiperscan.Extensions.ReferenceCheck.Control.Enums;
using Hiperscan.Extensions.ReferenceCheck.Control.GuiUpdate;

namespace Hiperscan.Extensions.ReferenceCheck.Control
{
    public partial class CoreApp
    {
        private const string PASSWORD_CHECK_SUM = "74-A9-C9-36-47-7D-29-95-21-41-FB-AE-72-C8-F3-76-FF-F7-D2-81-BC-D7-4B-61-FF-84-BD-E4-86-B9-C4-29-F7-10-83-0C-86-4D-EB-19-DC-5D-2C-62-78-8C-01-A0-0D-EA-64-AA-2A-7E-DC-75-42-23-CC-A5-72-04-E2-C8";

        private void CheckPassword()
        {
            if (String.IsNullOrEmpty(this.password))
                return;

            StringChecksum check_sum = new StringChecksum(BaseChecksum.HashType.SHA512, this.Password);

            if (check_sum.ComputeHash() == CoreApp.PASSWORD_CHECK_SUM)
            {
                this.GoToExecutionMode();
            }
            else
            {
                this.Password = String.Empty;
                UpdateArgs arg = new UpdateArgs();
                arg.ViewType = ViewTypes.Password;
                arg.UpdateCatecory = UpdateCategory.All;
                arg.DialogArgs.StandardDialogType = StandardDialogType.Error;
                arg.DialogArgs.Message = Catalog.GetString("Access denied: Invalid password.");
                this.UpdateGuiNeeded?.Invoke(arg);
            }
        }

        public bool HasPasswordEntered
        {
            get
            {
                return String.IsNullOrEmpty(this.Password) == false;
             
            }
        }

        private string password = String.Empty;
        public string Password
        {
            get
            {
                return this.password;
            }
            set
            {
                this.password = value;
                UpdateArgs arg = new UpdateArgs();
                arg.ViewType = ViewTypes.Password;
                arg.UpdateCatecory = UpdateCategory.CanNext;
                this.UpdateGuiNeeded?.Invoke(arg);
            }
        }
    }
}
