using System.Collections.Generic;
using UnityEngine;

namespace RecipeMenuCore.API
{
  /// <summary>
  /// Defines a custom RecipePage. Make sure to call Register on it to enable your RecipePage and to add at least one RecipePageEntry using AddRecipePageEntry.
  /// </summary>
  public class RecipePage
  {
    /// <summary>
    /// The RecipePageType of this RecipePage
    /// </summary>
    public readonly RecipePageType Type;

    /// <summary>
    /// The Title displayed on top of this RecipePage.
    /// </summary>
    public readonly string Title;

    /// <summary>
    /// The Texture associated with this RecipePage. May be null.
    /// </summary>
    public Texture Tex { get; protected set; }

    /// <summary>
    /// The Material associated with this RecipePage. May be null.
    /// </summary>
    public Material Mat { get; protected set; }

    internal List<RecipePageEntry> Rows = new List<RecipePageEntry>();

    /// <summary>
    /// Use to create a new RecipePage. Make sure to call Register on it to enable your RecipePage.
    /// </summary>
    public RecipePage(RecipePageType Type, string Title, Texture Tex)
    {
      this.Type = Type;
      this.Title = Title;
      this.Tex = Tex;
    }

    /// <summary>
    /// Use to create a new RecipePage. Make sure to call Register on it to enable your RecipePage.
    /// </summary>
    public RecipePage(RecipePageType Type, string Title, Material Mat)
    {
      this.Type = Type;
      this.Title = Title;
      this.Mat = Mat;
    }

    /// <summary>
    /// Used to register the page and make it show.
    /// </summary>
    public virtual RecipePage Register()
    {
      PostRegister();
      switch (Type)
      {
        case RecipePageType.GearForge:
          Core.pageGearForgeInfoList.Add(this);
          break;
        case RecipePageType.AlchemyStation:
          Core.pageAlchemyStationInfoList.Add(this);
          break;
        case RecipePageType.UltimateForge:
          Core.pageUltimateForgeInfoList.Add(this);
          break;
        case RecipePageType.UniversalCrafter:
          Core.pageUniversalCrafterInfoList.Add(this);
          break;
      }
      return this;
    }

    /// <summary>
    /// Used to register the page to repalce a vanilla one and make it show.
    /// </summary>
    public virtual RecipePage RegisterAsVanilla(int i)
    {
      PostRegister();
      switch (Type)
      {
        case RecipePageType.GearForge:
          Core.pageGearForgeInfoListVanilla[i] = this;
          break;
        case RecipePageType.AlchemyStation:
          Core.pageAlchemyStationInfoListVanilla[i] = this;
          break;
        case RecipePageType.UltimateForge:
          Core.pageUltimateForgeInfoListVanilla[i] = this;
          break;
      }
      return this;
    }

    protected void PostRegister()
    {
      if (Mat == null)
      {
        Mat = new Material(Shader.Find("Unlit/Transparent"))
        {
          mainTexture = Tex
        };
      }
      else
      {
        Tex = Mat.mainTexture;
      }
    }

    /// <summary>
    /// Used to register the RecipePage to make it show up.
    /// </summary>
    public RecipePageType GetEntryType()
    {
      return Type;
    }

    /// <summary>
    /// Adds a new RecipePageEntry to the RecipePage.
    /// </summary>
    public void AddRecipePageEntry(RecipePageEntry row)
    {
      Rows.Add(row);
    }

    /// <summary>
    /// Returns the list of avalible RecipePageEntry on this RecipePage.
    /// </summary>
    public RecipePageEntry[] GetRecipePageEntries()
    {
      return Rows.ToArray();
    }
  }

  /// <summary>
  /// This indicates what types of recipes are avalible.
  /// </summary>
  public enum RecipePageType
  {
    /// <summary>
    /// This recipe is for the Gear Forge.
    /// </summary>
    GearForge,

    /// <summary>
    /// This recipe is for the Alchemy Station.
    /// </summary>
    AlchemyStation,

    /// <summary>
    /// This recipe is for the Ultimate Forge.
    /// </summary>
    UltimateForge,

    /// <summary>
    /// This recipe is for the Gadget Core Universal Crafter.
    /// </summary>
    UniversalCrafter
  }
}