
// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2021 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 Hiperscan.Common.I18n;

namespace Hiperscan.Extensions.ApoIdentSoftwareUpgrade
{
    public static class VersionComparerer
    {
        public enum UpgradeType
        {
            None,
            Minor,
            Major,
        }

        public class Result
        {
            public static readonly Version NO_VERSION = new Version(0, 0, 0);
            public static readonly Version NOT_INSTALLED_ID = new Version(1999, 0);

            public override string ToString()
            {
                string version = "NO VERSION";
                if (this.Id == Result.NOT_INSTALLED_ID)
                {
                    version = "NOT_INSTALLED";
                }
                else if (this.Version != Result.NO_VERSION)
                {
                    version = this.Version.ToString();
                }

                string id = "NO ID";
                if (this.Id == Result.NOT_INSTALLED_ID)
                {

                }
                else if (this.Id != Result.NO_VERSION)
                {
                    id = String.Format("{0}-{1}", this.Id.Major,
                                         this.Id.Minor.ToString("D2"));
                }

                string value = "Result\n";
                value += String.Format("\t UpgradeTye: {0}\n", this.UpgradeType);
                value += String.Format("\t Version:    {0}\n", version);
                value += String.Format("\t Id:         {0}\n", id);
                return value;
            }

            public UpgradeType UpgradeType { get; set; } = UpgradeType.None;
            public Version Version { get; set; } = Result.NO_VERSION;
            public Version Id { get; set; } = Result.NO_VERSION;
        }

        public static Result CompareQSFileAndRegistry(string file, string registry)
        {
            Result result = new Result();
            try
            {
                Version file_version = VersionComparerer.GetVersionFromQSFile(file);
                Version registry_version = VersionComparerer.GetVersionFromQSRegistry(registry);

                if (file_version > registry_version)
                {
                    if (file_version.Major > registry_version.Major ||
                        file_version.Minor > registry_version.Minor)
                        result.UpgradeType = UpgradeType.Major;
                    else
                        result.UpgradeType = UpgradeType.Minor;

                    result.Version = file_version;
                }
            }
            catch(Exception)
            {

            }

            return result;
        }

        public static Result CompareIMFileAndRegistry(string file, string registry)
        {
            Result result = new Result();
            try
            {
                Version file_version = VersionComparerer.GetVersionFromIMFile(file);
                Version registry_version = VersionComparerer.GetVersionFromIMRegistry(registry);

                if (file_version > registry_version)
                {
                    if (file_version.Major > registry_version.Major ||
                        file_version.Minor > registry_version.Minor)
                        result.UpgradeType = UpgradeType.Major;
                    else
                        result.UpgradeType = UpgradeType.Minor;

                    result.Version = file_version;
                    result.Id = VersionComparerer.GetIdFromIMFile(file);
                }
                else
                {
                    if (file_version == registry_version)
                    {
                        Version id_form_file = VersionComparerer.GetIdFromIMFile(file);
                        Version id_from_tegistry = GetIdFromIMRegistry(registry);
                        if (id_form_file > id_from_tegistry)
                        {
                            result.UpgradeType = UpgradeType.Minor;
                            result.Version = file_version;
                            result.Id = id_form_file;
                        }
                    }
                }
            }
            catch (Exception)
            {

            }

            return result;
        }


        private static Version GetVersionFromQSFile(string file)
        {
            string[] tmp = file.Split(new char[] { '_', '.' });

            int major = Int32.Parse(tmp[1]);
            int minor = Int32.Parse(tmp[2]);
            int built = Int32.Parse(tmp[3]);

            return new Version(major, minor, built);
        }

        private static Version GetVersionFromQSRegistry(string value)
        {
            return new Version(value);
        }


        private static Version GetVersionFromIMFile(string file)
        {
            string[] tmp = file.Split(new char[] { '_', '.', '-'});

            int major = Int32.Parse(tmp[1]);
            int minor = Int32.Parse(tmp[2]);
            int built = Int32.Parse(tmp[3]);

            return new Version(major, minor, built);
        }

        private static Version GetIdFromIMFile(string file)
        {
            string[] tmp = file.Split(new char[] { '_', '.', '-' });

            int major = Int32.Parse(tmp[4]);
            int minor = Int32.Parse(tmp[5]);
 

            return new Version(major, minor);
        }

        private static Version GetVersionFromIMRegistry(string value)
        {
            string[] tmp = value.Split(new char[] {'-'});
            return new Version(tmp[0]);
        }

        private static Version GetIdFromIMRegistry(string value)
        {
            string[] tmp = value.Split(new char[] { '_', '.', '-' });

            int major = Int32.Parse(tmp[3]);
            int minor = Int32.Parse(tmp[4]);

            return new Version(major, minor);
        }
    }
}
