// Created with MonoDevelop
//
//    Copyright (C) 2017 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 System.Linq;
using System.Runtime.Serialization;
using Hiperscan.Unix;

namespace Hiperscan.AnalysisModule.Identification
{
    [DataContract]
    [Serializable]
    public partial class IdentSubstance : IComparable<IdentSubstance>
    {
        public static readonly string NOT_AVAILABLE = Catalog.GetString("n/a");

        public int CompareTo(IdentSubstance other)
        {
            if (other == null)
                return 1;

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

        public HashSet<string> AllSubstanceNames
        {
            get
            {
                HashSet<string> subsance_names = new HashSet<string>();
                subsance_names.Add(this.PrimaryDefaultName);
                foreach (string name in this.OtherDefaultNames)
                    subsance_names.Add(name);

                if (string.IsNullOrEmpty(this.PrimaryLatinName) == false)
                {
                    subsance_names.Add(this.PrimaryLatinName);
                    foreach (string name in this.OtherLatinNames)
                        subsance_names.Add(name);
                }

                return subsance_names;
            }
        }
        

        public HashSet<string> GetSynonyms(HashSet<string> without_names)
        {
            if (without_names == null)
                without_names = new HashSet<string>();

            return this.AllSubstanceNames.Where(n => without_names.Contains(n) == false).ToHashSet();
        }

        public IdentSubstance Clone()
        {
            IdentSubstance tmp = new IdentSubstance();

            tmp.Id = this.Id;
           
            tmp.PrimaryDefaultName = this.PrimaryDefaultName;
            tmp.OtherDefaultNames = this.OtherDefaultNames.ToList();
            tmp.PrimaryLatinName = this.PrimaryLatinName;
            tmp.OtherLatinNames = this.OtherLatinNames.ToList();

            tmp.Samples = this.Samples.Select(s => s.Clone()).ToList();

            tmp.AdditionalSubstanceInfo = this.AdditionalSubstanceInfo;
            tmp.CanInsert = this.CanInsert;
            tmp.IsVisible = this.IsVisible;
            tmp.BAKMarking = this.BAKMarking;
            tmp.PdfLinkAdditionalTest = this.PdfLinkAdditionalTest;
            tmp.FromUserSelected = this.FromUserSelected;

            return tmp;
        }

        public bool HasAlternativeNames
        {
            get
            {
                return this.OtherLatinNames.Count > 0 ||
                           this.OtherDefaultNames.Count > 0;
            }
        }

        public AnalysisType AnalysisType
        {
            get
            {
                if (this.ModelClass == null)
                    return AnalysisType.None;

                return this.ModelClass.DatabaseInfo.AnyalysisType;
            }
        }


        [DataMember] public string Id { get; set; }
        [DataMember] public string PrimaryDefaultName { get; set; }
        [DataMember] public string PrimaryLatinName { get; set; }
        [DataMember] public List<string> OtherDefaultNames { get; set; } = new List<string>();
        [DataMember] public List<string> OtherLatinNames { get; set; } = new List<string>();
        [DataMember] public List<IdentSample> Samples { get; set; } = new List<IdentSample>();
        [DataMember] public string AdditionalSubstanceInfo { get; set; }
        [DataMember] public bool CanInsert { get; set; }
        [DataMember] public bool IsVisible { get; set; }
        [DataMember] public string PdfLinkAdditionalTest { get; set; }
        [DataMember] public BAKMarkingState  BAKMarking { get; set; }

        [IgnoreDataMember] public bool FromUserSelected { get; set; }
        [IgnoreDataMember] public IdentClassification Classification { get; set; }
        [IgnoreDataMember] public IdentModelClass ModelClass { get; set; }
    }
}