using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;

using Barotrauma;
using HarmonyLib;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.IO;
using System.Xml.Linq;

namespace FastForward
{
  public partial class Mod : IAssemblyPlugin
  {
    public static string ModSettingsFolder = "ModSettings\\";
    public static string SettingsFolder = "ModSettings\\Fast Forward\\";
    public static string SettingsFile = "ModSettings\\Fast Forward\\Settings.xml";

    public static void createFolders()
    {
      if (!Directory.Exists(ModSettingsFolder)) Directory.CreateDirectory(ModSettingsFolder);
      if (!Directory.Exists(SettingsFolder)) Directory.CreateDirectory(SettingsFolder);
    }


    public static void loadSettings()
    {
      try
      {
        if (!File.Exists(SettingsFile)) return;

        XElement ff = XDocument.Load(SettingsFile).Element("FastForward");
        if (ff == null) return;

        if (ff.Element("Key") != null)
        {
          if (Enum.TryParse(typeof(Keys), ff.Element("Key").Value, out object k))
          {
            FFPressKey = (Keys)k;
          }
        }

        if (ff.Element("ToggleKey") != null)
        {
          if (Enum.TryParse(typeof(Keys), ff.Element("ToggleKey").Value, out object k))
          {
            FFToggleKey = (Keys)k;
          }
        }

        if (ff.Element("Speed") != null) int.TryParse(ff.Element("Speed").Value, out FFSpeed);

      }
      catch (Exception e) { log(e.Message, Color.Orange); }
    }

    public static void saveSettings()
    {
      XElement root = new XElement("FastForward",
        new XElement("Key", FFPressKey),
        new XElement("ToggleKey", FFToggleKey),
        new XElement("Speed", FFSpeed)
      );

      XDocument xdoc = new XDocument(root);
      xdoc.Save(SettingsFile);
    }
  }
}