using System;
using System.Linq;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using HarmonyLib.Tools;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace DavesPhatStore {
	[BepInPlugin(pluginGuid, pluginName, pluginVersion)]
	public class Main : BaseUnityPlugin {
		private const string pluginGuid = "DavesPhatStore";
		private const string pluginName = "DavesPhatStore";
		private const string pluginVersion = "1.0.0";
		
		public CustomerManager customermanager;
		private float timer = 0f;
		public static ConfigEntry<float> spawnTimer;

		public void Awake() {
			spawnTimer = Config.Bind("General", "SpawnTimer", 10f);

			SceneManager.sceneLoaded += this.OnSceneLoaded;
			Logger.LogInfo("Cyka mod loaded");
			HarmonyFileLog.Enabled = true;
			Harmony harmony = new Harmony(pluginGuid);
			harmony.PatchAll();
			var originalMethods = harmony.GetPatchedMethods();
			Logger.LogInfo("Patched " + originalMethods.Count() + " methods");
			foreach (var method in originalMethods) {
				Logger.LogInfo("Patched " + method.Name);
			}
		}

		public void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
			if (scene.name.Equals("Main Scene")) {
				customermanager = FindObjectOfType<CustomerManager>();
			}
		}

		void Update() {
			timer += Time.deltaTime;

			if (timer >= spawnTimer.Value) {
				Console.WriteLine("Cyka spawning customer");
				customermanager.SpawnCustomer();
				timer = 0f;
			}
		}
	}
}