// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2017 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.IO;
using System.Reflection;

using Hiperscan.AnalysisModule;
using Hiperscan.Common;
using Hiperscan.Common.I18n;
using Hiperscan.Common.IO;
using Hiperscan.ApoIdentCommon;
using Hiperscan.Unix;

namespace Hiperscan.ApoIdentCommon.ApoConfig.IO
{
    public class ApoIdentFileSystem
    {
        public ApoIdentFileSystem(bool windows_profile_changed, string config_path)
        {
            this.Locale = CatalogLocale.GetLocaleType();
            this.WindowsProfileChanged = windows_profile_changed;
            this.ConfigDir = config_path;
            this.QuickStepWorkingDir = new FileInfo(Assembly.GetEntryAssembly().Location).Directory.FullName;
        }

        public void OpenPdf(string pdf_file)
        {
            string path = Path.Combine(this.QuickStepWorkingDir, "Plugins", "ApoIdent", "Resources", "Pdfs");
            path = Path.Combine(path, CatalogLocale.Language);
            path = Path.Combine(path, pdf_file);

            Uri uri = new Uri("file://" + path.Replace("\\", "/"));
            Console.WriteLine("Open URI: " + uri);

            if (File.Exists(uri.LocalPath) == false)
                throw new Exception(String.Format(Catalog.GetString("Cannot find file '{0}'."), uri.LocalPath));

            Process.OpenUri(uri);
        }

        public string ConfigFile
        {
            get
            {
                return Path.Combine(this.ConfigDir, "ApoIdent.config.xml");
            }
        }

        private string CacheDataDir
        {
            get
            {
                return GlobalFiles.ApoIdentCacheDir;
            }
        }

        private string LocalizedCacheDataDir
        {
            get
            {
                return Path.Combine(this.CacheDataDir,
                                    this.Locale.ToString());
            }
        }

        public string SubstanceOverviewDir
        {
            get
            {
                return Path.Combine(this.LocalizedCacheDataDir, "substance_overwiew");
            }
        }
      
        public string SubstanceOverviewAvailableFile
        {
            get
            {
                return Path.Combine(SubstanceOverviewDir, "available_substances.pdf");
            }
        }

        public string SubstanceOverviewAddedFile
        {
            get
            {
                return Path.Combine(SubstanceOverviewDir, "added_substances.pdf");
            }
        }

        public string SubstanceOverviewInsertFile
        {
            get
            {
                return Path.Combine(SubstanceOverviewDir, "insert_substances.pdf");
            }
        }

        public string GetLicenseDir(AnalysisType analysis_type)
        {
            string dir = this.GetDirExtension(analysis_type, "license");
            return Path.Combine(this.LocalizedCacheDataDir, dir);
        }

        public string GetLicenseFile(AnalysisType analysis_type)
        {
            return Path.Combine(this.GetLicenseDir(analysis_type), DefaultNames.RemoteService.LICENSE_FILE);
        }


        public string GetValidationDir(AnalysisType analysis_type)
        {
            string dir = this.GetDirExtension(analysis_type, DefaultNames.RemoteService.VALIDATION_DIRECTORY);
            return Path.Combine(this.LocalizedCacheDataDir, dir);
        }

        public string GetValidationFile(AnalysisType analysis_type, string model_class_name)
        {
            string file = FileIOHelper.ConvertToValidFilename(model_class_name) + ".pdf";
            return Path.Combine(this.GetValidationDir(analysis_type), file);
        }


        public string GetAdditionalDocuDir(AnalysisType analysis_type)
        {
            string dir = this.GetDirExtension(analysis_type, DefaultNames.RemoteService.ADDITIONAL_TEST_DIRECTORY);
            return Path.Combine(this.LocalizedCacheDataDir, dir);
        }


        public string GetAdditionalTestFile(AnalysisType analysis_type, string model_class_name = null)
        {
            model_class_name = model_class_name ?? DefaultNames.IdentServer.ADDITIONAL_TESTS_FILE;
            model_class_name = Path.GetFileNameWithoutExtension(model_class_name);
            string file = FileIOHelper.ConvertToValidFilename(model_class_name) + ".pdf";
            return Path.Combine(this.GetAdditionalDocuDir(analysis_type), file);
        }


        public string GetUpdateCertificateDir(AnalysisType analysis_type)
        {
            string dir = this.GetDirExtension(analysis_type, "update_certificate");
            return Path.Combine(this.LocalizedCacheDataDir, dir);
        }

        public string GetUpdateCertficatFile(AnalysisType analysis_type, string file)
        {
            return Path.Combine(this.GetUpdateCertificateDir(analysis_type), file);
        }

        private string GetDirExtension(AnalysisType analysis_type, string dir)
        {
            if (analysis_type == AnalysisType.Quantification)
                dir = "quantifier_" + dir;

            return dir;
        }

        public string MonoExeFile
        {
            get
            {
                return Path.Combine(this.QuickStepWorkingDir, "Mono", "bin", "mono.exe");
            }
        }

        public string MdnsServicePath
        {
            get
            {
                return Path.Combine(this.QuickStepWorkingDir, "Plugins", "ApoIdent");
            }
        }

        public string DefaultDataExportDir
        {
            get
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                if (this.WindowsProfileChanged == true)
                    path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);

                if (false == FileIO.ExistsDirectory(path))
                {
                    path = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
                    path = Path.GetDirectoryName(path);
                }
                    
                return path;
            }
        }

        public string OldLicenseFile
        {
            get
            {
                return Path.Combine(this.LocalizedCacheDataDir, "license.txt");
            }
        }

        public string ConfigDir              {get; private set;}
        private string QuickStepWorkingDir   {get; set;} 
        public bool WindowsProfileChanged  { get; private set; }
        private CatalogLocale.LocaleType Locale { get; set; }
    }
}

