// 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.Xml.Serialization;
using System.IO;
using System.Text.RegularExpressions;

using Hiperscan.Unix;


namespace Hiperscan.Spectroscopy.IdentModule
{
	[Serializable()]
    public class IdentModuleInfo
	{
        public IdentModuleInfo()
		{
			this.PcaVersion			= String.Empty;
			this.AssemblyVersion 	= String.Empty;
			this.IdentModuleVersion	= String.Empty;
			this.Guid				= new Guid();
			this.ModelClassNames	= new List<string>();
            this.Locale			    = I18n.Locale.LocaleType.Unknown;
			this.CreationTimestamp	= DateTime.Now;
			this.IsRelease 			= false;
            this.PortNumber         = Spectroscopy.IdentModule.IdentServer.TCP_PORT;
		}

        public static IdentModuleInfo Read(string fname)
        {
            using (Stream stream = new FileStream(fname, FileMode.Open, FileAccess.Read))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(IdentModuleInfo));
                IdentModuleInfo ident_module_info = (IdentModuleInfo)serializer.Deserialize(stream);

                if (ident_module_info == null)
                    throw new Exception(Catalog.GetString("Error while deserialization."));

                return ident_module_info;
            }
        }

        public static void Write(IdentModuleInfo ident_module_info, string fname)
        {
            using (Stream stream = new FileStream(fname, FileMode.Create))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(IdentModuleInfo));
                serializer.Serialize(stream, ident_module_info);
            }
        }

		public override string ToString()
		{
			string info = "IdentModuleInfo:";
			info += String.Format("\nAssembly version:               {0}",	this.AssemblyVersion);
			info += String.Format("\nShort assembly version:         {0}",	this.ShortAssemblyVersion);
			info += String.Format("\nIdentModule version:            {0}",	this.IdentModuleVersion);
			info += String.Format("\nShort IdentModule version:      {0}",	this.ShortIdentModuleVersion);
			info += String.Format("\nIdentModule suffix:             {0}",	this.IdentModuleSuffix);
			info += String.Format("\nIs release:                     {0}",	this.IsRelease);
			info += String.Format("\nIdentModule locale:             {0}",	this.Locale);
			info += String.Format("\nIdentModule GUID:               {0}",	this.Guid);
			info += String.Format("\nIdentModule creation timestamp: {0}",	this.CreationTimestamp);
			info += String.Format("\nPCA version:                    {0}",	this.PcaVersion);
            info += String.Format("\nHostname:                       {0}",  Environment.MachineName);
            info += String.Format("\nTCP Port number:                {0}",  this.PortNumber);

            foreach (string model_class_name in this.ModelClassNames)
            {
				info += String.Format("\nModel Class name: {0}",	model_class_name);
            }

			return info;
		}

		public string ShortIdentModuleVersion
		{
			get
			{
                if (IdentModuleInfo.VersionExpression.IsMatch(this.IdentModuleVersion) == false)
                    throw new FormatException("Invalid IdentModule version: " + this.IdentModuleVersion);
                
                return IdentModuleInfo.VersionExpression.Match(this.IdentModuleVersion).Value.TrimEnd('-');
			}
		}

		public string IdentModuleSuffix
		{
			get
			{
				return IdentModuleInfo.SuffixExpression.Match(this.IdentModuleVersion).Value;
			}
		}

		public string ShortAssemblyVersion
		{
			get
			{
				string[] tmp = this.AssemblyVersion.Split(new char[]{'.'});
				return tmp[0] + "." + tmp[1];
			}
		}

        public static bool CheckSimpleIdentModuleVersion(string _version)
        {
            try
            {
                Regex regex = new Regex(@"^\d\d\d\d-\d+$");
                return regex.Match(_version).Success;
            } catch (Exception)
            {
                return false;
            }
        }

        public string PcaVersion				{get; set;}
		public I18n.Locale.LocaleType Locale    {get; set;}
		public string IdentModuleVersion		{get; set;}
		public string AssemblyVersion			{get; set;}
		public Guid Guid						{get; set;}
		public List<string> ModelClassNames	    {get; set;}
		public DateTime CreationTimestamp		{get; set;}
		public bool IsRelease					{get; set;}
        public int PortNumber                   {get; set;}

        public static Regex ReleaseExpression = new Regex(@"^\d{4}-\d{1,2}");
        public static Regex VersionExpression = new Regex(@"^\d+\.\d+-\d{4}-\d+-");
		public static Regex SuffixExpression  = new Regex(@"\d+$");
	}
}

