using DV.ThingTypes;
using HarmonyLib;
using UnityEngine;

using static CR_style_smoke_deflectors.Main;

namespace CR_style_smoke_deflectors;

[HarmonyPatch(typeof(TrainCar), "Start")]
class CarPatch {
	static void Postfix(ref TrainCar __instance)
	{
		if (__instance == null || __instance.carType != TrainCarType.LocoSteamHeavy)
		{
			return;
		}

		string bodyPath = "LocoS282A_Body/Static_LOD0";
		Transform s282Body = __instance.transform.Find(bodyPath);
		if (s282Body == null)
		{
			Error($"Couldn't find S282 body on '{__instance.transform.gameObject.name}' -> {bodyPath}");
			return;
		}

		// Smoke Deflector
		Log($"Applying {MySettings.smokeDeflectorType.ToString()}");

		switch(MySettings.smokeDeflectorType) {
			case Settings.SmokeDeflectorType.Chinese:
				ApplyChineseSmokeDeflector(ref __instance, s282Body);
				break;
		}
	}

	private static void ApplyChineseSmokeDeflector(ref TrainCar locomotive, Transform body)
	{
		//hide smoke box door
		var smokeboxDoorPath = "LocoS282A_Body/Static_LOD0/s282_locomotive_smokebox_door";
		Transform smokeBoxDoor = locomotive.transform.Find(smokeboxDoorPath);
		if (smokeBoxDoor == null)
		{
			Error($"Couldn't find S282 smoke box door on '{locomotive.transform.gameObject.name}' -> {smokeboxDoorPath}");
			return;
		}
		smokeBoxDoor.gameObject.SetActive(false);

		//show deflector and stuff
		GameObject chineseSmokeDeflector = Object.Instantiate(CRSmokeDeflectorsPrefab, body);
		chineseSmokeDeflector.transform.localPosition = smokeBoxDoor.localPosition;
	}
}
