
// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2023. 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.Threading.Tasks;

using Hiperscan.Unix;
using Hiperscan.GuiExtensions.Dialog;
using Hiperscan.Extensions.ReferenceCheck.Control.Enums;
using Hiperscan.Extensions.ReferenceCheck.Control.GuiUpdate;

namespace Hiperscan.Extensions.ReferenceCheck.Control
{
    public partial class CoreApp
    {
        private void GoToRestoreSection()
        {
            this.CurrentVisibleView = ViewTypes.Restore;
            UpdateArgs args = new UpdateArgs();
            args.ViewType = ViewTypes.Restore;

            this.UpdateGuiNeeded?.Invoke(args);
        }

        public void RestoreData()
        {
            Task.Run(() => this.DoRestoreData());
        }

        private void DoRestoreData()
        {
            this.RestoreSuccess = false;

            UpdateArgs args = new UpdateArgs();
            args.DialogArgs.ZipDialogType = ZipDialogType.Open;
            args.DialogArgs.FileName = this.LastSelectedDir;
            this.UpdateGuiNeeded?.Invoke(args);

            if (args.DialogArgs.Response != DialogRespnseType.Ok)
                return;

            try
            {
                string zip_file = args.DialogArgs.FileName;

                this.CurrentProgrammState = ProgramState.Restore;
                args = new UpdateArgs();
                args.CursorState = GuiExtensions.CursorState.Busy;
                args.ViewType = ViewTypes.Restore;
                this.UpdateGuiNeeded?.Invoke(args);

                this.RestoreSessonManager(zip_file);

                this.CurrentProgrammState = ProgramState.Waiting;
                this.RestoreSuccess = true;
                this.RestoredZipFile = zip_file;

                args = new UpdateArgs();
                args.CursorState = GuiExtensions.CursorState.Normal;
                args.ViewType = Enums.ViewTypes.Restore;

                this.UpdateGuiNeeded?.Invoke(args);
            }
            catch(Exception ex)
            {
                this.CurrentProgrammState = ProgramState.Waiting;
                this.RestoreSuccess = false;

                args = new UpdateArgs();
                args.CursorState = GuiExtensions.CursorState.Normal;
                args.ViewType = Enums.ViewTypes.Restore;

                args.DialogArgs.StandardDialogType = StandardDialogType.Error;
                args.DialogArgs.Exception = ex;
                args.DialogArgs.Message = string.Format(Catalog.GetString("Cannot restore data: {0}"), ex.Message);
                this.UpdateGuiNeeded?.Invoke(args);
            }
        }

        public bool IsRestoring
        {
            get
            {
                return this.CurrentProgrammState == ProgramState.Restore;
            }
        }

        public bool RestoreSuccess { get; set; } = false;
        internal string RestoredZipFile { get; set; } = string.Empty;
    }
}
