// 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.Diagnostics;
using System.IO;
using System.Reflection;
using Hiperscan.Extensions.ApoIdentCommon;

namespace Hiperscan.Extensions.ApoIdentSoftwareUpgrade
{

    public partial class Upgrader
    {
        private void Install()
        {
            Console.WriteLine("Install-type: {0}", this.Options.InstallType.Value);

            bool success = true;
            if (this.QuickStepLookupUnit.CanInstall && this.Options.InstallType == InstallType.QuickStep)
            {
                Console.WriteLine("Install Quickstep ...");
                success = this.StartInstallProcess(this.QuickStepLookupUnit);
            }

            if (success && this.IdentModuleLookupUnit.CanInstall)
            {
                if (this.Options.InstallType == InstallType.QuickStep)
                {
                    this.RestartProcess();
                    return;
                }

                Console.WriteLine("Install IdentMpdule ...");
                success = this.StartInstallProcess(this.IdentModuleLookupUnit);
            }
        }

        private void RestartProcess()
        {
            try
            {
                string log_file = Path.Combine(this.Options.WorkingDir, "install_identmodule.txt");
                Process proc = new Process();
                proc.StartInfo.FileName = Assembly.GetEntryAssembly().Location;
                proc.StartInfo.Arguments = String.Join(" ", this.Options.AllArgs.ToArray());
                proc.StartInfo.Arguments += " --install-type " + InstallType.IdentModule.ToString();
                proc.StartInfo.Arguments += " --log \"" + log_file + "\"";
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.UseShellExecute = false;

                proc.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Installer process (restart) failed: {0}", ex);
            }
        }

        private bool StartInstallProcess(LookupUnit lookup_unit)
        {
            try
            {
                Process proc = new Process();
                proc.StartInfo.FileName = this.GetDownloadFiles(lookup_unit)[0].FullName;
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;

                proc.Start();
                proc.WaitForExit();

                if (proc.ExitCode != 0)
                    throw new Exception(String.Format("Exited with {0}", proc.ExitCode));
                Console.WriteLine("Installer process success,");
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Installer process failed: {0}", ex);
                return false;
            }
        }
    }
}
