--=================== SINGLE PLAYER CLAIM SAFE HOUSE ===================--
--Claim Safehouse trong chế độ chơi SINGLE
--Tác dụng:
	--Hiển thị đánh dấu Safehouse của bạn trên bản đồ
	--Đánh dấu làm địa điểm không bị ảnh hưởng trong một số sự kiện
--Khi đã có SH, muốn claim SH khác thì phải Remove SH cũ
--Remove SH có 2 cách:
	--ESC -> Remove Safehouse
	--Chuột phải trong nhà SH cũ và chọn Remove Safehouse
--======================================================================--
if isClient() then return end 	--SP Only

require "OptionScreens/MainScreen"
require "ISUI/ISButton"
require "questui/vl_quest_markers"

local SetPlayerGMD_Data = VLGMD.SetPlayerGMD_Data
local MarkerList_Remove = VLF.MarkerList_Remove
local MarkerList_Add = VLF.MarkerList_Add
local GetStaticCoordOfBuilding = VLF.GetStaticCoordOfBuilding
local GetPlayerGlobalModData = VLGMD.GetPlayerGlobalModData
local IsBuildingInIgnoredList = VLF.IsBuildingInIgnoredList
local MarkerList_Remove = VLF.MarkerList_Remove

local markerIcon = getTexture("media/textures/safehouse_64.png")
local qStep = "q" .. "SPSH" .. "s001"

local function ClaimSPSafehouse(player, square)
    if not square then return end
	
	local function claimSH(x, y, z)
		--lưu player GMD tọa độ SH x, y, z
		SetPlayerGMD_Data(player, "SPSafehouseCoords", {x = x, y = y, z = z})
		
		--halotext 
		HaloTextHelper.addGoodText(player, getText("IGUI_VLQ_SPSafehouseClaimed"))
		
		--add marker
		MarkerList_Remove(qStep)
		MarkerList_Add({qStep = qStep, x = x, y = y, texture = markerIcon})
	end
	
	local building = square:getBuilding()
	local buildingDef = building:getDef()
	local x, y, z = GetStaticCoordOfBuilding(buildingDef)
	if not x or not y or not z then
		--building không hợp lệ
		HaloTextHelper.addBadText(player, getText("IGUI_VLQ_SPSafehouseBuildingInvalid"))
	end

	local playerGMD = GetPlayerGlobalModData(player)
	local safehouseData = playerGMD.Data["SPSafehouseCoords"]
	
	--Nếu đã có SH thì bỏ qua
	if safehouseData then 
		HaloTextHelper.addBadText(player, getText("IGUI_VLQ_SPSafehouseAlreadyClaimed"))
		return 
	end
	
	--Claim SH
	claimSH(x, y, z)
	
	--[[
	if not safehouseData then --chưa từng claim SH
		claimSH(x, y, z)
	else
		--nếu đã từng claim SH và tọa độ giống với building hiện tại
		if safehouseData.x and safehouseData.x == x
			and safehouseData.y and safehouseData.y == y
			and safehouseData.z and safehouseData.z == z then
			
			--báo đã ClaimSPSafehouse này rồi
			HaloTextHelper.addBadText(player, getText("IGUI_VLQ_SPSafehouseAlreadyClaimed"))
		else
			claimSH(x, y, z)
		end
	end
	]]
end

local function RemoveSPSafehouse()
	local player = getPlayer()
	local playerGMD = GetPlayerGlobalModData(player)
	
	if playerGMD and playerGMD.Data then
		if playerGMD.Data["SPSafehouseCoords"] then
			playerGMD.Data["SPSafehouseCoords"] = nil
			MarkerList_Remove(qStep)
			HaloTextHelper.addBadText(player, getText("IGUI_VLQ_SPSafehouseRemoved"))
		end
	end
end

local function AddContextMenu_ClaimSPSafehouse(playerIndex, context, worldobjects, test)
	local player = getSpecificPlayer(playerIndex)
	local mx, my = ISCoordConversion.ToWorld(getMouseXScaled(), getMouseYScaled(), player:getZ())
    local square = getCell():getGridSquare(math.floor(mx), math.floor(my), math.floor(player:getZ()))

	if square then
		local building = square:getBuilding()
		if building then
			if not IsBuildingInIgnoredList(building) then
				context:addOption(getText("IGUI_VLQ_SPSafehouseClaimMenu"), player, ClaimSPSafehouse, square)
			else
				--Hiển thị menu Remove Safehouse nếu là SH đang claim
				local playerGMD = GetPlayerGlobalModData(player)
				if playerGMD and playerGMD.Data then
					local safehouseData = playerGMD.Data["SPSafehouseCoords"]
					local buildingDef = building:getDef()
					local x, y, z = GetStaticCoordOfBuilding(buildingDef)
					if safehouseData and safehouseData.x == x and safehouseData.y == y then
						context:addOption(getText("IGUI_VLQ_SPSH_RemoveMenu"), player, RemoveSPSafehouse)
					end
				end
			end
		end
	end
end
Events.OnPreFillWorldObjectContextMenu.Remove(AddContextMenu_ClaimSPSafehouse)
Events.OnPreFillWorldObjectContextMenu.Add(AddContextMenu_ClaimSPSafehouse)

local function OnGameStart_UpdateSHMarker()
	local playerGMD = GetPlayerGlobalModData(getPlayer())
	local safehouseData = playerGMD.Data["SPSafehouseCoords"]
	
	if safehouseData then
		MarkerList_Add({qStep = qStep, x = safehouseData.x, y = safehouseData.y, texture = markerIcon})
	end
end
Events.OnGameStart.Remove(OnGameStart_UpdateSHMarker)
Events.OnGameStart.Add(OnGameStart_UpdateSHMarker)


--============================================================--
--UI NÚT BẤM REMOVE SAFEHOUSE Ở MAIN MENU KHI ESC
local mainScreenInstantiate = MainScreen.instantiate

function MainScreen:instantiate()
    mainScreenInstantiate(self)

    local mainScreen = MainScreenState.getInstance();
	if mainScreen ~= nil and (ISDemoPopup.instance == nil) then
		local player = getPlayer()
        if player then
            local playerNum = player:getPlayerNum()
            local width = 170
            local height = 50
            local x = getPlayerScreenWidth(playerNum) - width - 10
            local y = getPlayerScreenHeight(playerNum)/2 + height + 10

            local removeSHButton = ISButton:new(x, y, width, height, getText("IGUI_VLQ_SPSH_RemoveMenu"), self, RemoveSPSafehouse)
            self:addChild(removeSHButton)
        end
	end
end