using HarmonyLib;
using GadgetCore.API;
using UnityEngine;
using System.Collections;
using System.Reflection;

namespace First.Patches
{
  [HarmonyPatch(typeof(GameScript))]
  [HarmonyPatch("InitializeGatherStorage")]
  [HarmonyGadget("First")]
  public static class Patch_GameScript_InitializeGatherStorage
  {
    [HarmonyPrefix]
    public static bool Prefix(GameScript __instance)
		{
			int i;
			for (i = 0; i < 6; i++)
			{
				GameScript.shipDroid[i] = PreviewLabs.PlayerPrefs.GetInt("sd" + i);
				if (GameScript.shipDroid[i] > 5)
				{
					GameScript.shipDroid[i] = 5;
				}
			}
			if (GameScript.shipDroid[0] == 0)
			{
				GameScript.shipDroid[0] = 1;
				PreviewLabs.PlayerPrefs.SetInt("sd" + i, 1);
			}
			Item[] items = new Item[16];

			var field = typeof(GameScript).GetField("gatherStorage", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
			field.SetValue(__instance, items);

			for (i = 0; i < 16; i++)
			{
				items[i] = new Item(71, 2, 0, 0, 0, new int[3], new int[3]);
			}
			return false; // Return false to prevent the vanilla method from running.
    }

    /*
        [HarmonyPostfix]
        public static void Postfix(ClassName __instance)
        {
            // Add code to run after `MethodName` is called.
        }
		*/
  }
}
