using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Yaulw.Assembly;
using Yaulw.Tools;
using Sdaleo.Systems.SQLServer;
using Sdaleo.Systems.Advantage;

namespace PlutoServer.MSL.Connectors
{
    /// <summary>
    /// Dummy wrapper class that makes it pretty to figgure out what
    /// product we are in. Just do a ProductType.IsKeyLytec() or ProductType.IsKeyMedisoft() etc. 
    /// </summary>
    internal static class ProductType
    {
        #region consts
        
        /// <summary>
        /// Parent folder name where SuperBills are to be stored
        /// </summary>
        internal const string MOBILE_SB_FILE_FOLDER_NAME = "SuperBills";

        /// <summary>
        /// The File Extension for the Mobile SB Files
        /// </summary>
        internal const string MOBILE_SB_FILE_TYPE = "msb";

        #endregion

        /// <summary>
        /// Is System Key of type Lytec
        /// </summary>
        /// <param name="SystemApiKey"></param>
        /// <returns></returns>
        internal static bool IsKeyLytec(string SystemApiKey)
        {
            return (SystemAccessVerifier.GetProductTypeFromSystemApiKey(SystemApiKey) == 'L');
        }

        /// <summary>
        /// Is System Key of type Medisoft
        /// </summary>
        /// <param name="SystemApiKey"></param>
        /// <returns></returns>
        internal static bool IsKeyMedisoft(string SystemApiKey)
        {
            return (SystemAccessVerifier.GetProductTypeFromSystemApiKey(SystemApiKey) == 'M');
        }

        /// <summary>
        /// GetProductName by bool bIsMedisoft, if false returns Lytec
        /// </summary>
        /// <param name="bIsMedisoft"></param>
        /// <returns></returns>
        internal static string GetProductName(bool bIsMedisoft)
        {
            if (bIsMedisoft)
                return "Medisoft";
            else
                return "Lytec";
        }

        /// <summary>
        /// Return the Directory for the superbills, depending on product and DataSet or Database Name.
        /// This means that the connection must already be in the DBCache, in order for this function to work.
        /// If it is not in the DBCache, it will just default to the default superbill directory.
        /// </summary>
        /// <param name="SystemApiKey"></param>
        /// <returns></returns>
        internal static string GetProductSpecificSuperBillDirectoryPath(string SystemApiKey)
        {
#if !DEBUG            
            // Create Default Superbill Directory, if it doesn't exists
            string AsmPath = AssemblyW.SpecializedAssemblyInfo.GetAssemblyPath(AssemblyW.AssemblyST.Executing);
            string SuperBillsPath = AsmPath + "\\" + MOBILE_SB_FILE_FOLDER_NAME;
            if (!Directory.Exists(SuperBillsPath))
                Directory.CreateDirectory(SuperBillsPath);

            // Figgure out which subdirectory to use, allows us to sub-divide
            // Superbills by Practice/DataSet
            string subDirectory = "";
            if (ProductType.IsKeyLytec(SystemApiKey))
            {
                // Stripe out all invalid Chars (import for SQL Databases which can have different chars than the file system)
                SQLServerCredential credential = DBCache.GetSQLServerConnection(SystemApiKey);
                if(credential != null)
                   subDirectory = PathNaming.MakeDirectoryPathValid(credential.Database);    
            }
            else if (ProductType.IsKeyMedisoft(SystemApiKey))
            {
                // Retrieve the last Directory Name (DataSet Name from the DataSource) 
                AdvantageCredential credential = DBCache.GetAdvantageConnection(SystemApiKey);
                if (credential != null)
                    subDirectory = PathNaming.RetrieveLastDirectoryNameInPath(credential.DataSource);
            }
            
            // Create the SubDirectory (and if files exist in the main directory)
            // then copy those over, make those the default files * allows people to specify the new files to use in a practice *
            if (!String.IsNullOrEmpty(subDirectory))
            {
                if (!Directory.Exists(SuperBillsPath + "\\" + subDirectory))
                {
                    // Create the directory and copy all *.msb files from the main directory into it,
                    // allows the vars to put it into the main directory to be redistributed automatically
                    Directory.CreateDirectory(SuperBillsPath + "\\" + subDirectory);
                    string[] files = Directory.GetFiles(SuperBillsPath, "*." + ProductType.MOBILE_SB_FILE_TYPE, SearchOption.TopDirectoryOnly);
                    foreach (string file in files)
                    {
                        string FileName = Path.GetFileName(file);
                        File.Copy(SuperBillsPath + "\\" + FileName, SuperBillsPath + "\\" + subDirectory + "\\" + FileName, false);
                    }                    
                }
                return PathNaming.PathEndsWithNoSlash(SuperBillsPath + "\\" + subDirectory);
            }
            return PathNaming.PathEndsWithNoSlash(SuperBillsPath); 
#else
            // Temp Path may not be perfect,
            // but at least it is a central location not in the Release folder where we
            // are building
            return PathNaming.PathEndsWithNoSlash(Path.GetTempPath());
#endif            
        }

    }
}
