// 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.Spectroscopy.IdentModule;


namespace Hiperscan.Extensions.ApoIdent
{
    public enum SubstanceCategory
    {
        None,
        UserDefined,
        NotMeasurable,
        Classifiaction,
        SingleSubstance,
    }

    public class SubstanceListAutoCompletionEntry : IComparable<SubstanceListAutoCompletionEntry>
    {
        public SubstanceListAutoCompletionEntry(string substance_name, List<ClassificationInfo> classification_infos)
        {
            this.SubstanceName = substance_name;

            foreach (ClassificationInfo classification_info in classification_infos)
            {
                SubstanceCategory tmp = SubstanceListAutoCompletionEntry.ConvertToSubstanceCategory(classification_info);
                if (this.SubstanceCategory < tmp)
                    this.SubstanceCategory = tmp;
            }
        }

        public static SubstanceCategory ConvertToSubstanceCategory(ClassificationInfo classification_info)
        {
            if (classification_info == null)
                return SubstanceCategory.None;
            
            if (classification_info.IsUserDefined == true)
                return SubstanceCategory.UserDefined;

            if (classification_info.IsNirMeasurable == false)
                return SubstanceCategory.NotMeasurable;

            if (classification_info.ClassificationType == ClassificationType.NonMultiple)
                return SubstanceCategory.SingleSubstance;

            if (classification_info.Substances.Count == 1)
                return SubstanceCategory.SingleSubstance;

            return SubstanceCategory.Classifiaction;
        }

        public int CompareTo(SubstanceListAutoCompletionEntry other)
        {
            int lhs = (int)this.SubstanceCategory;
            int rhs = (int)other.SubstanceCategory;

            if (lhs < 3 && rhs > 2)
                return 1;

            if (rhs < 3 && lhs > 2)
                return -1;

            return string.Compare(this.SubstanceName, other.SubstanceName, StringComparison.InvariantCulture);
        }

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

    }
}
