// 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.Runtime.Serialization;


namespace Hiperscan.Spectroscopy.IdentModule
{
    [Serializable()]
    [DataContract(Name = "SubstanceInfo", Namespace = "http://www.hiperscan.com/spectroscopy/identmodule")]
    public class SubstanceInfo : IComparable<SubstanceInfo>
    {
        public SubstanceInfo(string id)
        {
            this.Id = id;
            this.SubstanceClassName = String.Empty;
            this.PrimaryDefaultName = String.Empty;
            this.PrimaryLatinName = String.Empty;
            this.OtherDefaultNames = new List<string>();
            this.OtherLatinNames = new List<string>();
            this.AdditionalSubstanceInfo = String.Empty;
            this.CanInsert = false;
            this.BatchNumbers = new List<string>();
            this.IsVisible = true;
        }

        public void SetSelectedNames(string substance_name, string batch_extension, string batch_number)
        {
            if (this.IsDefaultName(substance_name) == true)
            {
                this.SelectedDefaultName = substance_name;
                this.SelectedLatinName = this.PrimaryLatinName;
            }
            else
            {
                this.SelectedDefaultName = this.PrimaryDefaultName;
                this.SelectedLatinName = substance_name;
            }

            this.SelectedName = substance_name;

            if (String.IsNullOrEmpty(batch_number) == false)
            {
                this.SelectedDefaultName += batch_extension + batch_number;
                if (String.IsNullOrEmpty(this.SelectedLatinName) == false)
                    this.SelectedLatinName += batch_extension + batch_number;
            }

            this.SelectedBatchNumber = batch_number ?? String.Empty;
        }

        private bool IsDefaultName(string substance_name)
        {
            if (this.PrimaryDefaultName == substance_name)
                return true;

            return this.OtherDefaultNames.Contains(substance_name);
        }

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

            return this.PrimaryDefaultName.CompareTo(other.PrimaryDefaultName);
        }

        [NonSerialized] private List<string> all_names = null;

        public List<string> AllNames
        {
            get
            {
                if (this.all_names == null)
                {
                    this.all_names = new List<string>();
                    this.all_names.Add(this.PrimaryDefaultName);
                    this.all_names.AddRange(this.OtherDefaultNames);

                    if (String.IsNullOrEmpty(this.PrimaryLatinName) == false)
                    {
                        if (this.all_names.Contains(this.PrimaryLatinName) == false)
                            this.all_names.Add(this.PrimaryLatinName);

                        foreach (string latin_name in this.OtherLatinNames)
                        {
                            if (this.all_names.Contains(latin_name) == false)
                                this.all_names.Add(latin_name);
                        }
                    }
                }

                return this.all_names;
            }
        }

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

        [DataMember] public string Id { get; set; }
        [DataMember] public string SubstanceClassName { get; set; }
        [DataMember] public string PrimaryDefaultName { get; set; }
        [DataMember] public string PrimaryLatinName { get; set; }
        [DataMember] public List<string> OtherDefaultNames { get; set; }
        [DataMember] public List<string> OtherLatinNames { get; set; }

        [DataMember] public string AdditionalSubstanceInfo { get; set; }
        [DataMember] public bool CanInsert { get; set; }
        [DataMember] public bool IsVisible { get; set; }
        [DataMember] public List<string> BatchNumbers { get; set; }

        public string SelectedDefaultName { get; private set; }
        public string SelectedLatinName { get; private set; }
        public string SelectedName { get; private set; }
        public string SelectedBatchNumber { get; private set; }
    }
}