using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SmartAquaViewer.INI;

namespace SmartAquaViewer.Classes
{
    class Utils
    {
        public static Utils Instance { get; } = new Utils();

        public readonly string CurrentDirectory = Environment.CurrentDirectory;

        private static INIManager? iniManager;
        public static INIManager IniManager
        {
            get
            {
                if (iniManager == null)
                {
                    string iniPath = Path.Combine(Instance.CurrentDirectory, Constants.CONFIG_INI);
                    iniManager = new INIManager(iniPath);
                }
                return iniManager;
            }
        }

        private string GetGeneralSection(String strKey, String? defaultValue)
        {
            return IniManager.ReadValue(Constants.Config.CONFIG, strKey, defaultValue);
        }

        private void WriteValue(String strKey, String value)
        {
            IniManager.WriteValue(Constants.Config.CONFIG, strKey, value);
        }

        private string? title;
        private string Title
        {
            get
            {
                if (title == null)
                {
                    title = GetGeneralSection(Constants.Config.TITLE, null);
                }
                return title;
            }
        }

        public string GetTitle()
        {
            return Title;
        }

        private string? dataFilePath;
        private string DataFilePath
        {
            get
            {
                if (dataFilePath == null)
                {
                    dataFilePath = GetGeneralSection(Constants.Config.DATA_FILE_PATH, null);
                }
                return dataFilePath;
            }
        }

        public string GetDataFilePath()
        {
            return DataFilePath;
        }

        public string GetDataFileContentPath(string filePath)
        {
            return Path.Combine(CurrentDirectory, DataFilePath, filePath);
        }

        public void CreateDirectory(string folderName)
        {
            string directoryPath = Path.Combine(Environment.CurrentDirectory, folderName);

            if (Directory.Exists(directoryPath))
                return;

            Directory.CreateDirectory(directoryPath);
            Console.WriteLine($"Directory created at: {directoryPath}");
        }

        public void DebugWriteLine(string value, params object[] args)
        {
            if (Debugger.IsAttached)
            {
                Debug.WriteLine(value, args);
                FileLog(value);
            }
        }

        public void WriteFileLog(object value)
        {
            File.AppendAllText(Path.Combine(CurrentDirectory, "log.txt"), String.Format("[{0}]\n{1}\n", DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss"), value), Encoding.Default);
        }

        public void FileLog(object value)
        {
            WriteFileLog(value);
        }
    }
}
