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

namespace Hiperscan.Extensions.ApoIdentSoftwareUpgrade
{
    public class VersionLauncher
    {
        public const string NOT_AVAILABLE = "NOT_AVAILABLE";

        public VersionLauncher(Locale.LocaleType quickstep_locale)
        {
            this.QuickStepLocale = quickstep_locale;
        }

        public string GetFullQuickStepVersion()
        {
            try
            {
                string current_istalled = this.GetCurrentInstallQuickStepVersion();
                string key_path = String.Format(@"Software\Hiperscan\{0}", current_istalled);
                RegistryKey quickstep = Registry.LocalMachine.OpenSubKey(key_path);
                return (string)quickstep.GetValue("FullVersion");
            }
            catch(Exception)
            {
                return VersionLauncher.NOT_AVAILABLE;
            }
        }

        public string GetFullIdentModuleVersion()
        {
            string current_installed = String.Empty;
            string value = String.Empty;
            try
            {
                current_installed = this.GetCurrentInstallQuickStepVersion();
                current_installed = current_installed.Split(new char[] { ' ' })[1];
                current_installed = "IdentModule " + current_installed;
                string key_path = String.Format(@"Software\Hiperscan\{0}", current_installed);
                RegistryKey ident_module = Registry.LocalMachine.OpenSubKey(key_path);
                return (string)ident_module.GetValue("FullVersion");
            }
            catch (Exception)
            {
                value = VersionLauncher.NOT_AVAILABLE;
            }


            if (String.IsNullOrEmpty(current_installed) == false)
            {
                value = current_installed = current_installed.Split(new char[] { ' ' })[1];
                value += ".0-1999-00-0000";
            }

            return value;
        }

        public Locale.LocaleType GetIdentModuleLocale()
        {
            string current_installed = String.Empty;
            Locale.LocaleType value = Locale.LocaleType.Unknown;

            try
            {
                current_installed = this.GetCurrentInstallQuickStepVersion();
                current_installed = current_installed.Split(new char[] { ' ' })[1];
                current_installed = "IdentModule " + current_installed;
                string key_path = String.Format(@"Software\Hiperscan\{0}", current_installed);
                RegistryKey ident_module = Registry.LocalMachine.OpenSubKey(key_path);
                string tmp = (string)ident_module.GetValue("Locale");
                return (Locale.LocaleType)Enum.Parse(typeof(Locale.LocaleType), tmp);
            }
            catch (Exception)
            {
                value = Locale.LocaleType.Unknown;
            }

            if (String.IsNullOrEmpty(current_installed) == false)
            {
                value = this.QuickStepLocale;
            }

            return value;
        }

        private string GetCurrentInstallQuickStepVersion()
        {
            RegistryKey hiperscan = Registry.LocalMachine.OpenSubKey(@"Software\Hiperscan\CurrentQSVersion");
            return (string)hiperscan.GetValue("VersionKey");
        }

        private Locale.LocaleType QuickStepLocale { get; set; }
    }
}
