using System.Collections.Generic;
using System.IO;

namespace ZionVault.Environment
{
    public class VariableFactory
    {
        private const string CurrentDate = "CURRENTDATE";
        private const string CurrentDay = "CURRENTDAY";
        private const string CurrentMonth = "CURRENTMONTH";
        private const string CurrentYear = "CURRENTYEAR";
        private const string CuttentTime = "CURRENTTIME";
        private const string LastDayTheMonth = "LASTDAYTHEMONTH";
        private const string InputFolder = "INPUTFOLDER";
        private const string InputPath = "INPUTPATH";
        private const string InputFileName = "INPUTFILENAME";
        private const string InputFilePath = "INPUTFILEPATH";
        private const string OutputFolder = "OUTPUTFOLDER";
        private const string OutputPath = "OUTPUTPATH";
        private const string OutputFileName = "OUTPUTFILENAME";
        private const string OutputFilePath = "OUTPUTFILEPATH";
        private const string UserName = "USERNAME";
        private const string ComputerName = "COMPUTERNAME";
        private const string OutputFileCount = "OUTPUTFILECOUNT";

        public static IVariable construct(string type)
        {
            string objectToInstantiate = $"ZionVault.Environment.{type}, ZionSchematics";

            var objectType = System.Type.GetType(objectToInstantiate);

            return System.Activator.CreateInstance(objectType) as IVariable;
        }
        public static bool isVariable(string text)
        {
            try
            {
                construct(text);
                return true;
            }
            catch
            {
                return false;
            }
        }
        private static string FromDirectory(DirectoryInfo info, string var)
        {
            switch (var.Trim('%'))
            {
                case InputFolder:
                case OutputFolder:
                    return info.Name;
                case InputPath:
                case OutputPath:
                    return info.FullName;
            }

            return null;
        }
        private static string FromFile(FileInfo info, string var)
        {
            switch (var.Trim('%'))
            {
                case InputFolder:
                case OutputFolder:
                    return info.Directory.Name;
                case InputPath:
                case OutputPath:
                    return info.Directory.FullName;
                case InputFileName:
                case OutputFileName:
                    return info.Name;
                case InputFilePath:
                case OutputFilePath:
                    return info.FullName;
            }

            return null;
        }
        public static string Get(string variable, string file)
        {
            if ((File.GetAttributes(file) & FileAttributes.Directory) == FileAttributes.Directory)
                return FromDirectory((new DirectoryInfo(file)), variable);
            else
                return FromFile((new FileInfo(file)), variable);
        }
        public static string Get(string variable, List<string> files)
        {
            string result = string.Empty;

            foreach (string file in files)
            {
                FileInfo info = new FileInfo(file);

                switch (variable.Trim('%'))
                {
                    case InputFolder:
                    case OutputFolder:
                        result += info.Directory.Name;
                        break;
                    case InputPath:
                    case OutputPath:
                        result += info.Directory.FullName;
                        break;
                    case InputFileName:
                    case OutputFileName:
                        result += info.Name;
                        break;
                    case InputFilePath:
                    case OutputFilePath:
                        result += info.FullName;
                        break;
                }
                result += "|";
            }
            return result.Trim('|');
        }
    }
}
