// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2018 Thomas Klose & 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;
using Hiperscan.AnalysisModule;
using Hiperscan.AnalysisModule.Identification;

namespace Hiperscan.ApoIdentCore.SectionManager.Substance.SubstanceSelection
{
    public class SubstanceListAutoCompletionEntry : IComparable<SubstanceListAutoCompletionEntry>
    {
        public SubstanceListAutoCompletionEntry(string substance_name, List<IdentClassification> classifications, AnalysisType analysis_type)
        {
            this.SubstanceName = substance_name;
            if (analysis_type == AnalysisType.Quantification)
                this.AnalysisType = analysis_type;

            foreach (IdentClassification classification in classifications)
            {
                ClassificationType tmp = classification.ClassificationType;
                if (tmp < this.ClassificationType || this.ClassificationType == ClassificationType.None)
                    this.ClassificationType = tmp;
            }
        }

        public int CompareTo(SubstanceListAutoCompletionEntry other)
        {
            if (this.ClassificationType == ClassificationType.None && other.ClassificationType != ClassificationType.None)
                return 1;

            if (this.ClassificationType != ClassificationType.None && other.ClassificationType == ClassificationType.None)
                return -1;

            if (this.ClassificationType == ClassificationType.None && other.ClassificationType == ClassificationType.None)
                return string.Compare(this.SubstanceName, other.SubstanceName, StringComparison.InvariantCulture);


            if (this.ClassificationType == ClassificationType.Single || 
                this.ClassificationType == ClassificationType.Multiple)
            {
                if (other.ClassificationType == ClassificationType.Single ||
                    other.ClassificationType == ClassificationType.Multiple)
                {
                    return string.Compare(this.SubstanceName, other.SubstanceName, StringComparison.InvariantCulture);
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                if (other.ClassificationType == ClassificationType.UserDefined ||
                    other.ClassificationType == ClassificationType.NotMeasurable)
                {
                    return string.Compare(this.SubstanceName, other.SubstanceName, StringComparison.InvariantCulture);
                }
                else
                {
                    return 1;
                }
            }
        }

        public AnalysisType AnalysisType { get; private set; } = AnalysisType.None;
        public string SubstanceName { get; private set; } = String.Empty;
        public ClassificationType ClassificationType { get; private set; } = ClassificationType.None;
    }
}
