// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2021 Torsten Schache, 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.ApoIdentCore;
using Hiperscan.ApoIdentCore.Reporting.ReportData;
using Hiperscan.GtkExtensions.GuiSync;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        public static ApoIdentApplication Start(IHost host)
        {
            return new ApoIdentApplication(host);
        }

        private ApoIdentApplication(IHost host)
        {
            this.CheckForRunningApoIdentInstances();

            if (ApoIdentApplication.CurrentInstance != null)
            {
                ApoIdentApplication.CurrentInstance.MainWindow.Present();
                return;
            }

            Console.WriteLine("Call ApoIdentApplication.ctor...");

            MainThread.ID = System.Threading.Thread.CurrentThread.ManagedThreadId;
            ApoIdentApplication.CurrentInstance = this;

            this.Host = host;
            this.Host.DontShowInfoMessage = true;
            this.InitControl();

            this.SearchForSoftwareUpgrades();
            this.InitDataBackup();

            this.GuiDesigner = new GuiDesigner();
            this.DrawGui();
        }

        private void SearchForSoftwareUpgrades()
        {
            // WORKAROUND: Create a temporary window to show dialoge centered (if necessary).
            this.MainWindow = this.GetTmpWindow();
            this.InitGuiSyncControl(this.MainWindow, this.Control.GuiSyncControl);
            this.Control.CheckForPendingUpgrades(Plugin.replace_gui);
            this.MainWindow.Destroy();
            this.MainWindow = null;
        }

        private void InitControl()
        {
            this.Control = new AppControl(this.Host.WindowsProfileChanged,
                                          this.Host.ConfigurationDirPath, 
                                          this.Host.IsRelease,
                                          this.Host.DeviceList,
                                          this.Host.ReleaseCandidateVersion);

            this.Control.UpdateGuiNeeded += (arg) => this.Update(arg);
            this.Control.ActionNeeded += (arg) => this.ProcessAction(arg);

            ReportInfo.DetailPlot = new DetailsPlot(this.Control.MainThreadId);
        }

        private static ApoIdentApplication CurrentInstance { get; set; }
        public IHost Host { get; private set; }
        public AppControl Control { get; private set; }
    }
}
