// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2010 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 Hiperscan.Common.IO;


namespace Hiperscan.Extensions.ApoIdent
{

    public static class ReferencesArchive
    {
        public static void Export(Dictionary<string,string> compress_items, string export_file, int ndays)
        {
            ReferencesArchive.CompressItems = compress_items;
            ReferencesArchive.Days = ndays;

            ReferencesArchive.ZipArchive = new Zip(export_file, true);
            ReferencesArchive.ZipArchive.AddToArchiveCondition = ReferencesArchive.ZipCondition;

            foreach(string dir in compress_items.Keys)
                ReferencesArchive.ZipArchive.Add(dir, FileIOHelper.ConvertToValidFilename(compress_items[dir]));

            ReferencesArchive.ZipArchive.Finish();
        }

        private static bool ZipCondition(string file)
        {
            if (Path.GetExtension(file).ToLower() != ".pdf" && Path.GetExtension(file).ToLower() != ".csv")
                return false;
            
            Regex regex = new Regex("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]_[0-9][0-9]-[0-9][0-9]-[0-9][0-9]");
            Match match = regex.Match(Path.GetFileName(file));
            if (match.Success == false)
                 return false;
                
            TimeSpan span = DateTime.Now - DateTime.ParseExact(match.Value, "yyyy-MM-dd_HH-mm-ss", null);
            if (ReferencesArchive.Days > -1 && span.Days > ReferencesArchive.Days)
                return false;
            
            return true;
        }

        private static Dictionary<string, string> CompressItems {get; set;}
        private static Zip ZipArchive                             {get; set;}
        private static int Days                                    {get; set;}
    }
}