using System;
using System.IO;
using System.Xml.Linq;
using Barotrauma;
using Barotrauma.Networking;

namespace BetterHealthUI
{
    partial class BetterHealthUIMod : ACsMod
    {
        public static bool DisableHeartbeatSound { get; private set; } = false;
        public static bool DisableHeartbeatBar { get; private set; } = false;

        private const string ConfigFolderName = "BHUConfig";
        private const string ConfigFileName = "BetterHealthUIConfig.xml";

        public BetterHealthUIMod()
        {
            LuaCsSetup.PrintCsMessage("BetterHealthUIMod initialized.");
            EnsureConfigExists();
            LoadConfig();
            #if CLIENT
                InitClient();
            #endif
        }

        private void EnsureConfigExists()
        {
            string gameDirectory = Directory.GetCurrentDirectory();
            string configFolderPath = Path.Combine(gameDirectory, ConfigFolderName);
            string configFilePath = Path.Combine(configFolderPath, ConfigFileName);

            if (!Directory.Exists(configFolderPath))
            {
                Directory.CreateDirectory(configFolderPath);
                LuaCsSetup.PrintCsMessage($"Created config folder at: {configFolderPath}");
            }

            if (!File.Exists(configFilePath))
            {
                XDocument defaultConfig = new XDocument(
                    new XElement("Config",
                        new XElement("DisableHeartbeatSound", false),
                        new XElement("DisableHeartbeatBar", false)
                    )
                );

                defaultConfig.Save(configFilePath);
                LuaCsSetup.PrintCsMessage($"Created default config file at: {configFilePath}");
            }
        }

        private void LoadConfig()
        {
            string gameDirectory = Directory.GetCurrentDirectory();
            string configFilePath = Path.Combine(gameDirectory, ConfigFolderName, ConfigFileName);

            if (!File.Exists(configFilePath))
            {
                LuaCsSetup.PrintCsMessage($"Config file not found: {configFilePath}");
                return;
            }

            try
            {
                var doc = XDocument.Load(configFilePath);
                var root = doc.Root;
                if (root == null)
                {
                    LuaCsSetup.PrintCsMessage("Config file has no root element.");
                    return;
                }

                bool.TryParse(root.Element("DisableHeartbeatSound")?.Value, out bool disableSound);
                bool.TryParse(root.Element("DisableHeartbeatBar")?.Value, out bool disableBar);

                DisableHeartbeatSound = disableSound;
                DisableHeartbeatBar = disableBar;

                LuaCsSetup.PrintCsMessage($"Config loaded: DisableHeartbeatSound={disableSound}, DisableHeartbeatBar={disableBar}");
            }
            catch (Exception ex)
            {
                LuaCsSetup.PrintCsMessage($"Error loading config file: {ex.Message}");
            }
        }

        public override void Stop()
        {
            LuaCsSetup.PrintCsMessage("BetterHealthUIMod stopped.");
        }
    }
}
