// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2018 Thomas Klose & 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 System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;

using Glade;

using Gtk;

using Hiperscan.Common.IO;
using Hiperscan.Common.LogWriter;
using Hiperscan.Unix;


namespace Hiperscan.Extensions.ApoIdent
{

    public partial class DataBackupDialog
    {
        public static event EventHandler StartZipping = null;
        public static event EventHandler ZippingProgress = null;
        public static event EventHandler ZippingFinished = null;
        public static event EventHandler ZippingFailed = null;

        private void SaveData(string file_name)
        {
            DataBackupDialog.StartZipping?.Invoke(this, EventArgs.Empty);

            try
            {
                Zip zip = new Zip(file_name, true);
                zip.FileAdded += this.OnZippingProgressed;

                switch (this.Mode)
                {
                    case ExportMode.Backup:
                        this.GetZipSpectraArchivesFileCount();
                        this.ZipConfiguration(zip);
                        this.CanCount = true;
                        this.ZipSpectraArchives(zip);
                        break;

                    case ExportMode.OnlyLogFiles:
                        this.ZipLogData(zip);
                        break;

                    case ExportMode.CustomerSupport:
                        this.GetZipSpectraArchivesFileCount();
                        this.ZipLogData(zip);
                        this.CanCount = true;
                        this.ZipSpectraArchives(zip);
                        break;

                    default:
                        break;
                }

                zip.Finish();

                DataBackupDialog.ZippingFinished?.Invoke(this, EventArgs.Empty);
            }
            catch (Exception ex)
            {
                this.Exception = ex;
                DataBackupDialog.ZippingFailed?.Invoke(this, EventArgs.Empty);
            }
        }

        private void OnZippingProgressed(object sender, EventArgs e)
        {
            if (this.CanCount == false)
                return;

            this.FileProgressCount++;
            DataBackupDialog.ZippingProgress?.Invoke(this, EventArgs.Empty);
        }

        public Exception Exception { get; private set; } = null;
        private bool CanCount { get; set; } = false;
    }
}
