// 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.CommandLineArgsParser;
using Hiperscan.Common.I18n;
using Hiperscan.Unix;
using System.Globalization;
using System.IO;
using System.Collections.Generic;
using Hiperscan.Extensions.ApoIdentCommon;

namespace Hiperscan.Extensions.ApoIdentSoftwareUpgrade
{
    public enum InstallType
    {
        QuickStep,
        IdentModule,
    }

    public class Args : Hiperscan.Common.CommandLineArgsParser.Parser
    {
        public Args(string program_name, string[] args) : base(program_name, args)
        {
            this.Exit = false;

            this.ParseUnusedArgs();
        }

        protected override void InitializeSections()
        {

            this.ProgramCallSection.Description = string.Format(Catalog.GetString("Usage:\n{0} OPTION\n" +
                                                                                  "Searches for software upgrades (QS + IM).\nCopyright © Hiperscan GmbH, 2021"),
                                                                Args.ProgramName);

        }

        protected override void InitializeOptions()
        {
            this.Debug.Disabled = true;
            this.Quiet.Disabled = true;

            this.LocaleType = new Option<Locale.LocaleType>
            {
                Value = this.DefaultLocale,
                LongName = "--locale",
                ParameterName = "LOCALE_TYPE",
                HasParameter = true,
                Parser = StandardParser.EnumParser,
                Explanation = "Defines LOCALE_TYPE for upgrade search",
            };
            this.LocaleType.AddEnumerationToPredefinedValues(typeof(Locale.LocaleType));
            this.LocaleType.PredefinedValues.Remove(Locale.LocaleType.Unknown.ToString());


            this.WorkingDir = new Option<string>
            {
                Value = this.DefaultWorkingDir,
                LongName = "--working-dir",
                ParameterName = "DIR",
                HasParameter = true,
                Parser = StandardParser.DirectoryStringParser,
                Explanation = "Defines the working DIR for this app"
            };

            this.ShowLocaleTypes = new Option
            {
                Value = null,
                LongName = "--show-locale-types",
                HasParameter = false,
                Parser = this.ShowLocaleTypesParser,
                CloseProgramAfterParsing = true,
                Explanation = Catalog.GetString("Lists possible locale types"),
                ParsePriority = Int32.MinValue,
            };

            this.ShowAvailableUpgrades = new Option<bool>
            {
                Value = false,
                LongName = "--show-available-upgrades",
                HasParameter = false,
                Parser = StandardParser.SwitchOn,
                Explanation = "Lists available (installable) upgrades for QuickStep and Ident-Module"
            };

            this.LookForUpgrades = new Option<bool>
            {
                Value = false,
                LongName = "--look-for-upgrades",
                HasParameter = false,
                Parser = StandardParser.SwitchOn,
                Explanation = "Looks for possible upgrades for QuickStep and Ident-Module"
            };

            this.DownloadUpgrades = new Option<bool>
            {
                Value = false,
                LongName = "--download-upgrades",
                HasParameter = false,
                Parser = StandardParser.SwitchOn,
                Explanation = "Downloads possible upgrades for QuickStep and Ident-Module"
            };

            this.InstallUpgrades = new Option<bool>
            {
                Value = false,
                LongName = "--install-upgrades",
                HasParameter = false,
                Parser = StandardParser.SwitchOn,
                Explanation = "Installs possible upgrades for QuickStep and Ident-Module"
            };

            this.DelayInDays = new Option<uint>
            {
                Value = 0,
                LongName = "--delay",
                ParameterName = "DAYS",
                HasParameter = true,
                Parser = StandardParser.UInt32HexParser,
                Explanation = "Delays possible software upgrades for DAYS."
            };

            this.InstallType = new Option<InstallType>
            {
                Value = ApoIdentSoftwareUpgrade.InstallType.QuickStep,
                LongName = "--install-type",
                ParameterName = "TYPE",
                HasParameter = true,
                Undocumented = true,
                Parser = StandardParser.EnumParser,
                Explanation = "Not visible for user",
            };
        }

        private void ParseUnusedArgs()
        {
            if (this.UnhandledArgs.Count != 0)
            {
                throw new Exception(Catalog.GetString("Invalide call."));

            }
        }

        public object ShowLocaleTypesParser(List<string> values, Type type, int number_of_calls)
        {
            Console.WriteLine("Possible values for locale types:");

            foreach (Locale.LocaleType item in Enum.GetValues(typeof(Locale.LocaleType)))
            {
                if (item == Locale.LocaleType.Unknown)
                    continue;
                Console.WriteLine("\t{0}", item);
            }

            Console.WriteLine();
            return null;
        }

        private Locale.LocaleType DefaultLocale
        {
            get
            {
                CultureInfo culture = CultureInfo.CurrentCulture;
                if (culture.ToString().StartsWith("de", StringComparison.InvariantCulture))
                    return Locale.LocaleType.de_DE;


                if (culture.ToString().StartsWith("cs", StringComparison.InvariantCulture))
                    return Locale.LocaleType.cs_CZ;

                return Locale.LocaleType.en_GB;
            }
        }

        private string DefaultWorkingDir
        {
            get
            {
                return GlobalFiles.ApoIdentSoftwareUpggradeDir;
            }
        }

        

        public Option<string> WorkingDir { get; private set; }
        public Option<Locale.LocaleType> LocaleType { get; private set; }
        public Option ShowLocaleTypes { get; private set; }
        public Option<bool> ShowAvailableUpgrades { get; private set; }
        public Option<bool> LookForUpgrades { get; private set; }
        public Option<bool> DownloadUpgrades { get; private set; }
        public Option<bool> InstallUpgrades { get; private set; }
        public Option<uint> DelayInDays { get; private set; }

        public Option<InstallType> InstallType { get; set; }

        public bool Exit { get; private set; }
    }
}
