// 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.I18n;
using Hiperscan.Extensions.ApoIdentCommon;

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


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

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

            this.CheckUpgrades();
            this.CheckDelayInDays();

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

            this.WriteInstallableDownloads();
        }

        private void CheckUpgrades()
        {
            this.QuickStepLookupUnit.CanInstall = false;
            if (this.QuickStepLookupUnit.VersionResult.UpgradeType == VersionComparerer.UpgradeType.Major)
            {
                this.QuickStepLookupUnit.CanInstall = 
                    this.IdentModuleLookupUnit.VersionResult.UpgradeType == VersionComparerer.UpgradeType.Major;
            }

            if (this.QuickStepLookupUnit.VersionResult.UpgradeType == VersionComparerer.UpgradeType.Minor)
                this.QuickStepLookupUnit.CanInstall = true;

            this.IdentModuleLookupUnit.CanInstall = false;
            if (this.IdentModuleLookupUnit.VersionResult.UpgradeType == VersionComparerer.UpgradeType.Major)
            {
                this.IdentModuleLookupUnit.CanInstall =
                    this.QuickStepLookupUnit.VersionResult.UpgradeType == VersionComparerer.UpgradeType.Major;

                if (this.IdentModuleLookupUnit.CanInstall)
                {
                    if (this.QuickStepLookupUnit.VersionResult.Version.Major != this.IdentModuleLookupUnit.VersionResult.Version.Major)
                    {
                        this.QuickStepLookupUnit.CanInstall = false;
                        this.IdentModuleLookupUnit.CanInstall = false;
                    }
                    if (this.QuickStepLookupUnit.VersionResult.Version.Minor != this.IdentModuleLookupUnit.VersionResult.Version.Minor)
                    {
                        this.QuickStepLookupUnit.CanInstall = false;
                        this.IdentModuleLookupUnit.CanInstall = false;
                    }
                }
            }

            if (this.IdentModuleLookupUnit.VersionResult.UpgradeType == VersionComparerer.UpgradeType.Minor)
                this.IdentModuleLookupUnit.CanInstall = true;
        }

        private void CheckDelayInDays()
        {
            DateTime date = DateTime.MaxValue;

            if (this.QuickStepLookupUnit.CanInstall && this.QuickStepLookupUnit.PublisherDate < date)
                date = this.QuickStepLookupUnit.PublisherDate;

            if (this.IdentModuleLookupUnit.CanInstall && this.IdentModuleLookupUnit.PublisherDate < date)
                date = this.IdentModuleLookupUnit.PublisherDate;

            if (date > DateTime.MaxValue.AddDays(-this.Options.DelayInDays - 1))
            {
                this.QuickStepLookupUnit.CanInstall = false;
                this.IdentModuleLookupUnit.CanInstall = false;
            }
            else if (date.AddDays(this.Options.DelayInDays) > DateTime.Now)
            {
                this.QuickStepLookupUnit.CanInstall = false;
                this.IdentModuleLookupUnit.CanInstall = false;
            }
            else
            {
                if (this.QuickStepLookupUnit.CanInstall && this.IdentModuleLookupUnit.CanInstall)
                {
                    if (this.QuickStepLookupUnit.PublisherDate != date)
                    {
                        string dir = this.GetFullDownloadDirectoryName(this.QuickStepLookupUnit.ProjectLocale);
                        FileInfo file = new FileInfo(Path.Combine(dir, this.QuickStepLookupUnit.FileName));
                        file.CreationTime = date;
                    }

                    if (this.IdentModuleLookupUnit.PublisherDate != date)
                    {
                        string dir = this.GetFullDownloadDirectoryName(this.IdentModuleLookupUnit.ProjectLocale);
                        FileInfo file = new FileInfo(Path.Combine(dir, this.IdentModuleLookupUnit.FileName));
                        file.CreationTime = date;
                    }
                }
            }
        }


        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);
            }
        }
    }
}
