using UnityEngine;
using GadgetCore.API;
using UnityEngine.SceneManagement;
using GadgetCore.API.ConfigMenu;
using System;
using System.Collections.Generic;
using AlternativeTitles.ConfigEnums;

namespace AlternativeTitles
{
  [Gadget("AlternativeTitles", RequiredOnClients: false)]
  public class AlternativeTitles : Gadget<AlternativeTitles>
  {
    public const string MOD_VERSION = "1.0"; // Set this to the version of your mod.
    public const string CONFIG_VERSION = "1.0"; // Increment this whenever you change your mod's config file.

    protected override void LoadConfig()
    {
      Config.Load();
      string fileVersion = Config.ReadString("ConfigVersion", CONFIG_VERSION, comments: "The Config Version (not to be confused with mod version)");
      if (fileVersion != CONFIG_VERSION)
      {
        Config.Reset();
        Config.WriteString("ConfigVersion", CONFIG_VERSION, comments: "The Config Version (not to be confused with mod version)");
      }

      Core.settingTitleRarity = Config.ReadEnum("TitleRarity", TitleRarityEnum.Common, comments: new string[] { "How rare sould the replacement titles be?" });

      Config.Save();
    }

    public override string GetModDescription()
    {
      return "A mod that adds random alternative titles to the main menu.";
    }

    protected override void Initialize()
    {
      Logger.Log("Alternative Titles v" + Info.Mod.Version);
      Core.logger = Logger;
      SceneManager.sceneLoaded += OnSceneLoaded;
      ChangeMenuText();
    }

    internal static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    {
      if (scene.buildIndex == 0)
      {
        ChangeMenuText();
      }
    }

    private static Texture2D texture2D = GadgetCoreAPI.LoadTexture2D("roguelandsTitle.png");
    private static Texture2D texture2DL = GadgetCoreAPI.LoadTexture2D("roguelandsTitleL2.png");

    private static void ChangeMenuText()
    {
      try
      {
        var titleElement = GameObject.Find("MAIN").transform.Find("-").gameObject;
        var renderer = titleElement.GetComponent<Renderer>();
        if (Core.defaultTitleTexture == null)
          Core.defaultTitleTexture = renderer.material.mainTexture;

        int i = 100;
        switch (Core.settingTitleRarity)
        {
          case TitleRarityEnum.UltraRare:
            i = 120;
            break;
          case TitleRarityEnum.VeryRare:
            i = 70;
            break;
          case TitleRarityEnum.Rare:
            i = 30;
            break;
          case TitleRarityEnum.Common:
            i = 10;
            break;
          case TitleRarityEnum.VeryCommon:
            i = 5;
            break;
          case TitleRarityEnum.UltraCommon:
            i = 2;
            break;
          case TitleRarityEnum.Only:
            i = 1;
            break;
        }

        if (random.Next(i) == 0)
          if (random.Next(2) == 0)
            renderer.material = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = texture2DL };
          else
            renderer.material = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = texture2D };
        else
          renderer.material = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = Core.defaultTitleTexture };
      }
      catch (Exception e) { Core.logger.Log(e); }
    }

    private static System.Random random = new System.Random();
  }
}
