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;

namespace FastForward
{
  public partial class Mod : IAssemblyPlugin
  {
    public static List<DebugConsole.Command> addedCommands = new List<DebugConsole.Command>();
    public static void addCommands()
    {
      addedCommands ??= new List<DebugConsole.Command>();

      addedCommands.Add(new DebugConsole.Command("fastforward", "fastforward [seconds]: Fast forwards the game by x seconds. Note that large numbers may cause a long freeze.", (string[] args) =>
      {
        float seconds = 0;
        if (args.Length > 0) { float.TryParse(args[0], out seconds); }
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();
        for (int i = 0; i < seconds * Timing.FixedUpdateRate; i++)
        {
          Screen.Selected?.Update(Timing.Step);
        }
        sw.Stop();
        DebugConsole.NewMessage($"Fast-forwarded by {seconds} seconds (took {sw.ElapsedMilliseconds / 1000.0f} s).");
      }));

      addedCommands.Add(new DebugConsole.Command("fastforward_key", "", (string[] args) =>
      {
        if (args.Length > 0)
        {
          if (Enum.TryParse(typeof(Keys), args[0], out object k))
          {
            FFPressKey = (Keys)k;
          }
          else
          {
            FFPressKey = Keys.None;
          }
        }

        log($"fastforward_key = {FFPressKey}");
        saveSettings();
      }));

      addedCommands.Add(new DebugConsole.Command("fastforward_togglekey", "", (string[] args) =>
      {
        if (args.Length > 0)
        {
          if (Enum.TryParse(typeof(Keys), args[0], out object k))
          {
            FFToggleKey = (Keys)k;
          }
          else
          {
            FFToggleKey = Keys.None;
          }

          FFState = false;
        }

        log($"fastforward_key = {FFToggleKey}");
        saveSettings();
      }));

      addedCommands.Add(new DebugConsole.Command("fastforward_speed", "", (string[] args) =>
      {
        if (args.Length > 0)
        {
          if (int.TryParse(args[0], out int i))
          {
            FFSpeed = i;
          }
        }

        log($"fastforward_speed = {FFSpeed}");
        saveSettings();
      }));

      addedCommands.Add(new DebugConsole.Command("printkeys", "", (string[] args) =>
      {
        var values = typeof(Keys).GetEnumValues();
        foreach (var v in values)
        {
          log($"{(int)v} {v}");
        }
      }));

      addedCommands.Add(new DebugConsole.Command("printcolors", "", (string[] args) =>
      {
        foreach (PropertyInfo prop in typeof(Color).GetProperties(BindingFlags.Static | BindingFlags.Public))
        {
          log(prop, (Color)prop.GetValue(null));
        }
      }));

      addedCommands.ForEach(c => DebugConsole.Commands.Add(c));
    }



    public static void removeCommands()
    {
      addedCommands.ForEach(c => DebugConsole.Commands.RemoveAll(which => which.Names.Contains(c.Names[0])));

      addedCommands.Clear();
      addedCommands = null;
    }
  }
}