// 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.Collections.Generic;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class SubstanceManagement
    {
        public void CreateSubstanceRequest(string substance_name = null)
        {
            if (substance_name == null || this.SubstanceNameExists(substance_name) == false)
                this.SubstanceNameBeforeRequest = this.CurrentSelectedName ?? String.Empty;
            else
                this.SubstanceNameBeforeRequest = substance_name;
               

            this.ModelClassesForRequest = new HashSet<string>();
            if (String.IsNullOrEmpty(this.SubstanceNameBeforeRequest) == false)
                this.ModelClassesForRequest = this.GetModelClassesBySubstanceName(this.SubstanceNameBeforeRequest);

            this.ModelClassNameAfterRequest = String.Empty;
            this.SubstanceNameAfterRequest = String.Empty;
        }

        public void SetRequestedSubstance(string model_class_name, string substance_name = null)
        {
            this.SubstanceNameAfterRequest = substance_name ?? this.SubstanceNameBeforeRequest;
            this.ModelClassNameAfterRequest = model_class_name;
        }

        public void UpdateAfterSubstanceRequest()
        {
            bool has_changed = false;
            if (this.SubstanceNameBeforeRequest != this.SubstanceNameAfterRequest)
                has_changed = true;

            if (this.ModelClassNameBeforeRequest != this.ModelClassNameAfterRequest)
                has_changed = true;

            this.SelectSubstanceByName(this.ModelClassNameAfterRequest, this.SubstanceNameAfterRequest);

            if (has_changed)
                this.AfterSubstanceSelectionChanged?.Invoke(this, EventArgs.Empty);
            else
                this.SubstanceSelectionNotChanged?.Invoke(this, EventArgs.Empty);
        }


        public string SubstanceNameBeforeRequest { get; private set; }
        public string ModelClassNameBeforeRequest { get; private set; }

        public HashSet<string> ModelClassesForRequest { get; private set; }

        private string ModelClassNameAfterRequest {  get; set; }
        private string SubstanceNameAfterRequest { get; set; }
    }
}
