// 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 System.IO;
using Hiperscan.Common;
using Hiperscan.Common.I18n;

namespace Hiperscan.Extensions.ApoIdentSoftwareUpgrade
{
    public class SoftwareTestEnvironment
    {
        public SoftwareTestEnvironment(Args args)
        {
            this.GetTestType(args);
            this.UpdateOptions(args);
            this.WriteTestType();
        }

        public string GetTestLink(ProjectType project_type)
        {
            string[] tmp = this.TestType.ToString().Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries); ;
            string test_type = tmp[1];

            if (project_type == ProjectType.IdentModule)
            {
                switch (this.TestType)
                {
                    case SoftwareTestType.QS_NU__IM_MI:
                    case SoftwareTestType.QS_MA__IM_MI:
                        test_type = "MI1";
                        break;

                    case SoftwareTestType.QS_MI__IM_MI:
                        test_type = "MI2";
                        break;

                    case SoftwareTestType.QS_MI__IM_NU:
                        test_type = "ERROR";
                        break;

                    default:
                        test_type = tmp[3];
                        break;
                }
            }

            return String.Format("http://web-ks.hiperscan.com/downloads/{0}/{1}/{2}/{2}_{3}.php",
                                 this.TestVersion,
                                 this.TestLocale,
                                 project_type.ToString().ToLower(),
                                 test_type);
        }

        private void GetTestType(Args args)
        {
            try
            {
                string value = Environment.GetEnvironmentVariable(Env.ApoIdent.SOFTWARE_UPGRADE_SIM);
                string[] tmp = value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                this.TestVersion = new Version(tmp[0]);
                this.TestVersion = new Version(this.TestVersion.Major, this.TestVersion.Minor);

                this.TestLocale = (Locale.LocaleType)Enum.Parse(typeof(Locale.LocaleType), tmp[1]);
                this.TestType = (SoftwareTestType)Enum.Parse(typeof(SoftwareTestType), tmp[2]);

                if (tmp.Length == 4)
                {
                    try
                    {
                        this.TestDelayInDays = UInt32.Parse(tmp[3]);
                    }
                    catch(Exception)
                    {
                        this.TestDeleteArchive = Boolean.Parse(tmp[3].ToLower().Replace("clean", "true"));
                    }
                }

                if (tmp.Length == 5)
                    this.TestDeleteArchive = Boolean.Parse(tmp[3].ToLower().Replace("clean", "true"));
            }
            catch (Exception)
            {
                this.TestLocale = args.LocaleType;
                this.TestType = SoftwareTestType.None;
            }
        }

        private void WriteTestType()
        {
            Console.Write("Software test environment: ");
            if (this.IsActive)
            {
                Console.WriteLine("activated");
                Console.WriteLine("\t- Version:       {0}", this.TestVersion);
                Console.WriteLine("\t- Locale:        {0}", this.TestLocale);
                Console.WriteLine("\t- Test type:     {0}", this.TestType);
                Console.WriteLine("\t- Delay:         {0}", this.TestDelayInDays);
                Console.WriteLine("\t- Clean Arhcive: {0}", this.TestDeleteArchive);
            }
            else
            {
                Console.WriteLine("not activated");
            }

            Console.WriteLine();
        }

        private void UpdateOptions(Args args)
        {
            if (this.IsActive)
            {
                args.LocaleType.Value = this.TestLocale;
                args.DelayInDays.Value = this.TestDelayInDays;
            }
        }

        public bool IsActive
        {
            get
            {
                return this.TestType != SoftwareTestType.None;
            }
        }

        private SoftwareTestType TestType { get; set; }
        private Locale.LocaleType TestLocale { get; set; }
        private Version TestVersion { get; set; }
        private uint TestDelayInDays { get; set; } = 0;
        public bool TestDeleteArchive { get; private set; } = false;
    }
}
