require 'TimedActions/ISReadABook.lua'
require 'TimedActions/ISReadWorldMap.lua'

Kinkocalypse = Kinkocalypse or {}
Kinkocalypse.ISReadABookOrigStart = ISReadABook.start;
Kinkocalypse.ISReadWorldMapOrigIsValid = ISReadWorldMap.isValid;

Kinkocalypse.SetBlindedOverlayOn = function(playerNum, isTrue)
	local sm = getSearchMode();
	local psm = sm:getSearchModeForPlayer(playerNum);
	sm:setEnabled(playerNum, true);

	if isTrue == true then
		local blur = psm:getBlur();
		blur:setExterior(0.5);
		blur:setInterior(0.5);
		local desat = psm:getDesat();
		desat:setExterior(1);
		desat:setInterior(1);
		local gradient = psm:getGradientWidth();
		gradient:setExterior(12);
		gradient:setInterior(12);
		local dark = psm:getDarkness();
		dark:setExterior(1);
		dark:setInterior(1);
	else
		local blur = psm:getBlur();
		blur:setExterior(0.1);
		blur:setInterior(0.1);
		local desat = psm:getDesat();
		desat:setExterior(0.1);
		desat:setInterior(0.1);
		local gradient = psm:getGradientWidth();
		gradient:setExterior(0.5);
		gradient:setInterior(0.5);
		local dark = psm:getDarkness();
		dark:setExterior(0.20);
		dark:setInterior(0.20);
	end
end

Kinkocalypse.SetBlindedOverlayOff = function(playerNum)
	local sm = getSearchMode();
    local psm = sm:getSearchModeForPlayer(playerNum);
    sm:setEnabled(playerNum, false);
	local blur = psm:getBlur();
	local desat = psm:getDesat();
	local gradient = psm:getGradientWidth();
	local dark = psm:getDarkness();
	blur:reset()
	desat:reset()
	gradient:reset()
	dark:reset()
end

Kinkocalypse.IsBlindFoldEquipped = function(player)
	local wornItem = player:getWornItem("RestraintsHead")
	return wornItem ~= nil and Kinkocalypse.IsHasTag(wornItem, "SteelBlindFold")
end

Kinkocalypse.IsCanRead = function()
	local player = getPlayer()
	if Kinkocalypse.IsBlindFoldEquipped(player) == true or Kinkocalypse.IsPunishmentActive("Blinded") == true then
		return false
	else return true end
end

ISReadABook.start = function(self)
	if Kinkocalypse.ControllCenterIsOn() == true and Kinkocalypse.IsPunishmentActive("NoRead") then
		Kinkocalypse.AddPunishmentScore(20, getText("UI_Kincalypse_ViolateNoReadRule"))
		Kinkocalypse.AddPunishment("NoRead", 60 + ZombRand(120))
		Kinkocalypse.AddPunishment("Blinded", 120)
	end

	if Kinkocalypse.IsCanRead() == false then
		Kinkocalypse.Say(getText("UI_Kincalypse_CantSeeAnthing"))
		self:forceStop()
	else
		Kinkocalypse.ISReadABookOrigStart(self)	
	end	
end

ISReadWorldMap.isValid = function()
	if Kinkocalypse.IsCanRead() == false then
		Kinkocalypse.Say(getText("UI_Kincalypse_CantSeeAnthing"))
		return false
	end	
	return Kinkocalypse.ISReadWorldMapOrigIsValid(self)
end

Kinkocalypse.CheckBlindFold = function()
	local player = getPlayer()
	local playerNum = player:getPlayerNum()
	local nightVision = false

	if Kinkocalypse.IsPunishmentActive("Blinded") == true or Kinkocalypse.IsBlindFoldEquipped(player) then
		nightVision = true
		Kinkocalypse.SetBlindedOverlayOn(playerNum, true)
	else
		nightVision = false
		Kinkocalypse.SetBlindedOverlayOff(playerNum)
	end

	player:setWearingNightVisionGoggles(nightVision)
end
