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

namespace SplashTexts
{
  [Gadget("SplashTexts", RequiredOnClients: false)]
  public class SplashTexts : Gadget<SplashTexts>
  {
    public const string MOD_VERSION = "1.1"; // 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.

    public override IGadgetConfigMenu GetConfigMenu() { return null; }

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

    protected override void Initialize()
    {
      Logger.Log("Splash Texts 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 void ChangeMenuText()
    {
      try
      {
        var text = GetText();

        var textElement = GameObject.Find("MAIN").transform.Find("-").Find("txtALPHA").gameObject;
        var textElementShadow = textElement.transform.Find("txt0").gameObject;

        textElement.GetComponent<TextMesh>().color = SpliceColor(ref text);

        var shadowText = SpliceShadow(ref text);

        textElement.GetComponent<TextMesh>().text = text;
        textElementShadow.GetComponent<TextMesh>().text = shadowText;
      }
      catch (Exception e) { Core.logger.Log(e); }
    }

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

    private static string GetText()
    {
      List<string> texts = new List<string>();
      texts.Add("v1.5.1 Deluxe");
      texts.Add("10,000,000$ Edition");
      texts.Add("Premium Ultra Deluxe Gold Edition");
      texts.Add("#C#AAAAFF#Infinity Edition");
      texts.Add("Gadget Core " + GadgetCoreAPI.GetFullVersion());
      texts.Add("enhanced by Gadget Core");
      texts.Add("There are mods?");
      texts.Add("1 + 5 = 1#S#1 + 5 = 6");
      texts.Add("v1.5.1#S#v1.5");
      texts.Add("v1.5.1#S#        1");
      texts.Add("v1.5.1");
      texts.Add("v?.?.?");
      texts.Add("Magicite 2");
      texts.Add("Magicite In Space");
      texts.Add("I wouldn't talk to the guy with the pickaxe.");
      texts.Add("You should talk to the guy with the pickaxe.");
      texts.Add("powered by Unity");
      texts.Add("5% Explosions");
      Gadget[] gadgets = Gadgets.ListAllEnabledGadgets();
      if (gadgets.Length > 0)
      {
        Gadget g = gadgets[random.Next(gadgets.Length)];
        texts.Add(g.Info.ModName + " included");
      }
      if (random.Next(4) == 0)
      {
        texts.Add("#C#2160FF#Play Galactic Fleet");
        texts.Add("#C#AF9346#Play Church of Faust");
        texts.Add("#C#F42912#Play Starlight Rebellion");
        texts.Add("#C#632887#Play Gray Enigma");
      }
      if (random.Next(6) == 0)
      {
        texts.Add("#C#2160FF#Galactic Fleet is the best!");
        texts.Add("#C#AF9346#Church of Faust is the best!");
        texts.Add("#C#F42912#Starlight Rebellion is the best!");
        texts.Add("#C#632887#Gray Enigma is the best!");
      }
      texts.Add("The shrooms want to cuddle.");
      texts.Add("42");
      var time = DateTime.Now;
      texts.Add(time.Hour + ":" + time.Minute + ":" + time.Minute);
      texts.Add(((time.Hour + 11) % 12 + 1) + ":" + time.Minute + ":" + time.Minute + " " + (time.Hour >= 12 ? "PM" : "AM"));
      texts.Add("maybe " + (time.Year + 1) + "?");
      texts.Add(((int)(time - new DateTime(2017, 2, 14)).TotalSeconds)+ " seconds already");
      texts.Add("made on earth");
      for (int j = 0; j < 3; j++)
      {
        int i = random.Next(6);
        int @int = PreviewLabs.PlayerPrefs.GetInt(i + "isChar");
        if (@int > 0)
        {
          var name = PreviewLabs.PlayerPrefs.GetString(i + "name");
          texts.Add("Hi, " + name + "!");
          texts.Add("Have a nice day " + name + "!");
          break;
        }
      }
      texts.Add("Have a nice day!");
      texts.Add("You should go to sleep.");
      texts.Add("OwO");
      texts.Add(":3");
      texts.Add("Nice");
      texts.Add("made by Sean Young");
      texts.Add("music by Bashi Boizu");
      texts.Add("Exodus exists");
      texts.Add("end of 2019");
      texts.Add("finished since 2017");
      texts.Add("Rock Scarabs do like that.");
      texts.Add("RUN TO THE PORTALS!!!");
      texts.Add("Graphics: AMAZING");
      return texts[random.Next(texts.Count)];
    }

    private static Color SpliceColor(ref string s)
    {
      if (s.StartsWith("#C#"))
      {
        try
        {
          var ar = s.Split(new char[] { '#' }, 4);
          s = ar[3];
          if (ar[2].Length == 6)
          {
            var i1 = Convert.ToInt32(ar[2].Substring(0, 2), 16);
            var i2 = Convert.ToInt32(ar[2].Substring(2, 2), 16);
            var i3 = Convert.ToInt32(ar[2].Substring(4, 2), 16);
            return new Color(((float)i1) / 0xFF, ((float)i2) / 0xFF, ((float)i3) / 0xFF);
          }
        }
        catch { }
      }
      return new Color(((float)0xFF) / 0xFF, ((float)0xD7) / 0xFF, ((float)0x31) / 0xFF);
    }

    private static string SpliceShadow(ref string s)
    {
      if (s.Contains("#S#"))
      {
        try
        {
          var ar = s.Split(new string[] { "#S#" }, 2, StringSplitOptions.None);
          s = ar[0];
          return ar[1];
        }
        catch { }
      }
      return s;
    }
  }
}
