// 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 Hiperscan.Common.RegistryManagement;
using Microsoft.Win32;

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

        public VersionSearcher(CatalogLocale.LocaleType quickstep_locale)
        {
            this.QuickStepLocale = quickstep_locale;
        }

        public string GetFullVersion(ProjectType type)
        {
            MSRegistry registry = MSRegistry.Create();
            string version = null;

            switch (type)
            {
                case ProjectType.QuickStep:
                    version = this.GetFullQuickStepVersion();
                    break;

                case ProjectType.IdentModule:
                    version = registry.IdentModule.FullVersion;
                    break;

                case ProjectType.QuantifierModule:
                    version = registry.QuantifierModule.FullVersion;
                    break;

                default:
                    break;
            }

            if (String.IsNullOrEmpty(version))
                version = VersionSearcher.NOT_AVAILABLE;

            return version;
        }

        public CatalogLocale.LocaleType GetModuleLocale(ProjectType type)
        {
            MSRegistry registry = MSRegistry.Create();

            switch (type)
            {
                case ProjectType.IdentModule:
                    return registry.IdentModule.Locale;

                case ProjectType.QuantifierModule:
                    return registry.QuantifierModule.Locale;

                default:
                    return CatalogLocale.LocaleType.Unknown;
            }
        }

  
        private 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 String.Empty;
            }
        }

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

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