Scriptname SHESON_DynDOLOD_MCM extends SKI_ConfigBase

ObjectReference MyMaster = None
ObjectReference CurrentRef = None
ObjectReference MyCurrentFirstborn = None
ObjectReference MyActiveFirstborn = None
String MyWorld = "None"
String MyImportFileName = "None"
String MyModName = "DynDOLOD.esp"
String MyFormID = "None"
String MyFirstbornList = "None"
String MyMinionList = "None"
int MyActiveID
int flags
Int MyCurrent = 0
bool MyActiveValue = TRUE

Event OnPageReset(string Page)
	MyWorld = StringUtil.Substring(Game.GetPlayer().GetWorldspace() as string, 13, StringUtil.Find(Game.GetPlayer().GetWorldspace() as string, "(") - 14)
	MyImportFileName = "DynDOLOD_" + JsonUtil.GetStringValue("DynDOLOD_Worlds", MyWorld, MyWorld)
	MyMaster = Game.GetFormFromFile(JsonUtil.IntListGet(MyImportFileName, "master", 0), MyModName) As ObjectReference
	MyCurrentFirstborn = StorageUtil.GetFormValue(MyMaster, "DynDOLOD_MyCurrentFirstborn", None) As ObjectReference
	MyActiveFirstborn = StorageUtil.GetFormValue(MyMaster, "DynDOLOD_MyActiveFirstborn", None) As ObjectReference
	if (MyCurrentFirstborn == None) || (StorageUtil.GetIntValue(MyMaster, "DynDOLOD_Active", 1) == 99)
		flags = OPTION_FLAG_DISABLED
		SetCursorPosition(4)
		if (StorageUtil.GetIntValue(MyMaster, "DynDOLOD_Active", 1) == 99)
			AddTextOption("(Resetting like everything)", "")
		else
			AddTextOption("(No DynDOLOD for this world)", "")
		endIf
	else
		flags = OPTION_FLAG_None
	endIf
	if (MyActiveFirstborn == None)
		MyActiveValue = FALSE
	else
		MyActiveValue = TRUE
	endIf
	SetCursorFillMode(LEFT_TO_RIGHT)
	SetCursorPosition(0)
	AddHeaderOption("Options")
	SetCursorPosition(2)
	MyActiveID = AddToggleOption("DynDOLOD is active", MyActiveValue, flags)
	SetCursorPosition(6)
	AddHeaderOption("Important")
	SetCursorPosition(10)
	AddTextOption("While DynDOLOD can be turned off here,", "")
	SetCursorPosition(12)
	AddTextOption("it is imperative to be in an interior space", "")
	SetCursorPosition(14)
	AddTextOption("before updating DynDOLOD.esp", "")

	SetCursorPosition(1)
	AddHeaderOption("Updating DynDOLOD")
	SetCursorPosition(5)
	AddTextOption("Before updating DynDOLOD.esp go inside a house", "")
	SetCursorPosition(7)
	AddTextOption("and check here that DynDOLOD is not active.", "")
	SetCursorPosition(11)
	AddTextOption("Save.", "")
	SetCursorPosition(15)
	AddTextOption("Start without DynDOLOD.esp, load last save.", "")
	SetCursorPosition(19)
	AddTextOption("Save again.", "")
	SetCursorPosition(23)
	AddTextOption("Start with new DynDOLOD.esp, load last save.", "")
endEvent

Event OnOptionHighlight(int Option)
	MyWorld = StringUtil.Substring(Game.GetPlayer().GetWorldspace() as string, 13, StringUtil.Find(Game.GetPlayer().GetWorldspace() as string, "(") - 14)
	MyImportFileName = "DynDOLOD_" + JsonUtil.GetStringValue("DynDOLOD_Worlds", MyWorld, MyWorld)
	MyMaster = Game.GetFormFromFile(JsonUtil.IntListGet(MyImportFileName, "master", 0), MyModName) As ObjectReference
	MyCurrentFirstborn = StorageUtil.GetFormValue(MyMaster, "DynDOLOD_MyCurrentFirstborn", None) As ObjectReference
	if (MyCurrentFirstborn != None)
		SetInfoText("DynDOLOD active world " + JsonUtil.GetStringValue("DynDOLOD_Worlds", MyWorld, MyWorld) + ".")
	endIf
endEvent

Event OnOptionSelect(int Option)
	if (Option == MyActiveID)
		MyWorld = StringUtil.Substring(Game.GetPlayer().GetWorldspace() as string, 13, StringUtil.Find(Game.GetPlayer().GetWorldspace() as string, "(") - 14)
		MyImportFileName = "DynDOLOD_" + JsonUtil.GetStringValue("DynDOLOD_Worlds", MyWorld, MyWorld)
		MyMaster = Game.GetFormFromFile(JsonUtil.IntListGet(MyImportFileName, "master", 0), MyModName) As ObjectReference
		MyCurrentFirstborn = StorageUtil.GetFormValue(MyMaster, "DynDOLOD_MyCurrentFirstborn", None) As ObjectReference
		if MyCurrentFirstborn != None
			MyActiveValue = !MyActiveValue
			SetToggleOptionValue(MyActiveID, MyActiveValue)
			SwitchDynDOLOD(MyActiveValue)
		endIf
	endIf
endEvent

Function SwitchDynDOLOD(Bool MyAttach)
	MyWorld = StringUtil.Substring(Game.GetPlayer().GetWorldspace() as string, 13, StringUtil.Find(Game.GetPlayer().GetWorldspace() as string, "(") - 14)
	MyImportFileName = "DynDOLOD_" + JsonUtil.GetStringValue("DynDOLOD_Worlds", MyWorld, MyWorld)
	MyMaster = Game.GetFormFromFile(JsonUtil.IntListGet(MyImportFileName, "master", 0), MyModName) As ObjectReference
	MyCurrentFirstborn = StorageUtil.GetFormValue(MyMaster, "DynDOLOD_MyCurrentFirstborn", None) As ObjectReference
	if MyCurrentFirstborn != None
		MyFormID = Math.LogicalAnd(0x00FFFFFF, MyCurrentFirstborn.GetFormID()) as string
		MyFirstbornList = JsonUtil.StringListGet(MyImportFileName, MyFormID, 0)
		MyMinionList = JsonUtil.StringListGet(MyImportFileName, MyFormID, 1)
		if MyActiveValue
			StorageUtil.SetIntValue(MyMaster, "DynDOLOD_Active", 1)
		else
			StorageUtil.SetIntValue(MyMaster, "DynDOLOD_Active", 0)
		endIf
		(MyMaster as SHESON_DynDOLOD_Master).IBowToThee(MyCurrentFirstborn, MyFirstbornList, MyMinionList, MyAttach)
		if MyActiveValue
			Debug.MessageBox("DynDOLOD activated")
		else
			Debug.MessageBox("DynDOLOD deactivated")
		endIf
	endIf
EndFunction
