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

namespace Hiperscan.Extensions.ApoIdentSoftwareUpgrade
{
    public partial class Upgrader
    {
        private void ShowAvailableUpgrades()
        {
            CatalogLocale.LocaleType locale = this.Options.LocaleType;
            List<FileInfo> files = null;


            this.QuickStepLookupUnit = new LookupUnit(ProjectType.QuickStep, 
                                                      locale,
                                                      this.Options.DelayInDays,
                                                      this.VersionSearcher);
            files = this.GetDownloadFiles(this.QuickStepLookupUnit);
            if (files.Count == 1)
                this.QuickStepLookupUnit.SetFromDownloads(files[0]);

            this.IdentModuleLookupUnit = new LookupUnit(ProjectType.IdentModule, 
                                                        locale, 
                                                        this.Options.DelayInDays,
                                                        this.VersionSearcher);
            files = this.GetDownloadFiles(this.IdentModuleLookupUnit);
            if (files.Count == 1)
                this.IdentModuleLookupUnit.SetFromDownloads(files[0]);


            this.QuantifierModuleLookopUnit = new LookupUnit(ProjectType.QuantifierModule, 
                                                             locale, 
                                                             this.Options.DelayInDays,
                                                             this.VersionSearcher);
            files = this.GetDownloadFiles(this.QuantifierModuleLookopUnit);
            if (files.Count == 1)
                this.QuantifierModuleLookopUnit.SetFromDownloads(files[0]);

            this.CheckUpgrades();
            this.QuickStepLookupUnit.Write();
            this.IdentModuleLookupUnit.Write();
            this.QuantifierModuleLookopUnit.Write();

            this.WriteInstallableDownloads();
        }

        private void CheckUpgrades()
        {
            bool have_same_version = this.AllHaveSameVersions();

            this.QuickStepLookupUnit.CanInstall = false;
            if (this.QuickStepLookupUnit.VersionResult.UpgradeType != VersionComparerer.UpgradeType.None &&
                this.QuickStepLookupUnit.DelayIsOutdated)
            {
                this.QuickStepLookupUnit.CanInstall = have_same_version;
            }

            this.IdentModuleLookupUnit.CanInstall = false;
            if (this.IdentModuleLookupUnit.HasLocalInstallation == true)
            {
                if (this.IdentModuleLookupUnit.VersionResult.UpgradeType != VersionComparerer.UpgradeType.None &&
                    this.IdentModuleLookupUnit.DelayIsOutdated)
                {
                    this.IdentModuleLookupUnit.CanInstall = have_same_version;
                }
            }

            this.QuantifierModuleLookopUnit.CanInstall = false;
            if (this.QuantifierModuleLookopUnit.HasLocalInstallation == true)
            {
                if (this.QuantifierModuleLookopUnit.VersionResult.UpgradeType != VersionComparerer.UpgradeType.None &&
                    this.QuantifierModuleLookopUnit.DelayIsOutdated)
                {
                    this.QuantifierModuleLookopUnit.CanInstall = have_same_version;
                }
            }
        }

        private bool AllHaveSameVersions()
        {
            bool success = true;

            int qs_major_id = this.QuickStepLookupUnit.GetRelevantMajorId();
            int qs_minor_id = this.QuickStepLookupUnit.GetRelevantMinorId();

            if (this.IdentModuleLookupUnit.HasLocalInstallation)
            {
                int im_major_id = this.IdentModuleLookupUnit.GetRelevantMajorId();
                int im_minor_id = this.IdentModuleLookupUnit.GetRelevantMinorId();

                if (success)
                {
                    success = qs_major_id == im_major_id &&
                        qs_minor_id == im_minor_id;
                }
            }

            if (this.QuantifierModuleLookopUnit.HasLocalInstallation)
            {
                int qm_major_id = this.QuantifierModuleLookopUnit.GetRelevantMajorId();
                int qm_minor_id = this.QuantifierModuleLookopUnit.GetRelevantMinorId();

                if (success)
                {
                    success = qs_major_id == qm_major_id &&
                        qs_minor_id == qm_minor_id;
                }
            }

            return success;
        }

        private void WriteInstallableDownloads()
        {
            if (this.QuickStepLookupUnit.CanInstall)
            {
                Console.WriteLine("{0}{1}{2}", DefaultNames.SoftwareUpgrader.QUICKSTEP,
                                  DefaultNames.SoftwareUpgrader.UPGRADE_AVAILABLE,
                                  this.QuickStepLookupUnit.VersionResult.Version);
            }
            else
            {
                Console.WriteLine("{0}{1}", DefaultNames.SoftwareUpgrader.QUICKSTEP,
                                  DefaultNames.SoftwareUpgrader.NO_UPGRADE);
            }

            if (this.IdentModuleLookupUnit.CanInstall)
            {
                string id = String.Format("{0}-{1}", this.IdentModuleLookupUnit.VersionResult.Id.Major,
                                          this.IdentModuleLookupUnit.VersionResult.Id.Minor.ToString("D2"));

                Console.WriteLine("{0}{1}{2}-{3}", DefaultNames.SoftwareUpgrader.IDENTMODULE,
                                  DefaultNames.SoftwareUpgrader.UPGRADE_AVAILABLE,
                                  this.IdentModuleLookupUnit.VersionResult.Version,
                                  id);
            }
            else
            {
                Console.WriteLine("{0}{1}", DefaultNames.SoftwareUpgrader.IDENTMODULE,
                                 DefaultNames.SoftwareUpgrader.NO_UPGRADE);
            }

            if (this.QuantifierModuleLookopUnit.CanInstall)
            {
                string id = String.Format("{0}-{1}", this.QuantifierModuleLookopUnit.VersionResult.Id.Major,
                                          this.QuantifierModuleLookopUnit.VersionResult.Id.Minor.ToString("D2"));

                Console.WriteLine("{0}{1}{2}-{3}", DefaultNames.SoftwareUpgrader.QUANTIFIERMODULE,
                                  DefaultNames.SoftwareUpgrader.UPGRADE_AVAILABLE,
                                  this.QuantifierModuleLookopUnit.VersionResult.Version,
                                  id);
            }
            else
            {
                Console.WriteLine("{0}{1}", DefaultNames.SoftwareUpgrader.QUANTIFIERMODULE,
                                 DefaultNames.SoftwareUpgrader.NO_UPGRADE);
            }
        }
    }
}
