--[[
    Script to handle removing tokens from map, or map and CT with mouse click and keyboard combination.
]]--

function onInit()	
	Token.onClickDown = onClickDown;			
end


-- Delete token / and CT entry on mouse click.
-- Use: Mouse Left-click Token on combat map while holding down either 'Alt' or 'Alt+Ctrl'
-- Pre: Token on combat map
-- Post: Token removed from combat map (Alt) or combat map and combat tracker (Atl+Ctrl)
-- NOTE: button (number), Returns a numerical value indicating the button pressed (1 = left, 2 = middle, 4 = button 4, 5 = button 5). Right button is used for radial menus.
function onClickDown( token, button, image ) 

	local bFlankingRules = OptionsManager.getOption("CE_TRBC"); 

	-- Deletes token from combat map, if Alt held on left mouse click.
	if (bFlankingRules == 'option_val_alt') then	
		if (Input.isAltPressed() == true) and (User.isHost() == true) and (button==1) then
			local nodeCT = CombatManager.getCTFromToken(token);
			token.delete();		
	
			-- Deletes token from combat tracker if Ctrl was also held on click.
			if (Input.isControlPressed() == true) then
				if nodeCT then -- only attempt delete if there is a CT entry
					nodeCT.delete();					
				end				
			end
		end	
	end

	-- Deletes token from combat map, if Alt + Shift held on left mouse click.
	if (bFlankingRules == 'option_val_alt_shift') then	
		if (Input.isAltPressed() == true) and (Input.isShiftPressed() == true) and (User.isHost() == true) and (button==1) then
			local nodeCT = CombatManager.getCTFromToken(token);
			token.delete();		
	
			-- Deletes token from combat tracker if Ctrl was also held on click.
			if (Input.isControlPressed() == true) then
				if nodeCT then -- only attempt delete if there is a CT entry
					nodeCT.delete();					
				end				
			end
		end	
	end


end




-- Open record when token is shift-left clicked (direct copy from CoreRPG.pak FG v3.3.7, function onDoubleClick(tokenMap, vImage))
function openTokenInformationWindow(tokenMap, vImage)			
	local nodeCT = CombatManager.getCTFromToken(tokenMap);
	if nodeCT then
		if User.isHost() then
			local sClass, sRecord = DB.getValue(nodeCT, "link", "", "");
			if sRecord ~= "" then
				Interface.openWindow(sClass, sRecord);				
			else
				Interface.openWindow(sClass, nodeCT);				
			end
		else
			if (DB.getValue(nodeCT, "friendfoe", "") == "friend") then
				local sClass, sRecord = DB.getValue(nodeCT, "link", "", "");
				if sClass == "charsheet" then
					if sRecord ~= "" and DB.isOwner(sRecord) then
						Interface.openWindow(sClass, sRecord);						
					else
						ChatManager.SystemMessage(Interface.getString("ct_error_openpclinkedtokenwithoutaccess"));
					end
				else
					local nodeActor;
					if sRecord ~= "" then
						nodeActor = DB.findNode(sRecord);
					else
						nodeActor = nodeCT;
					end
					if nodeActor then
						local w = Interface.openWindow(sClass, nodeActor);						
						w.bringToFront();
					else
						ChatManager.SystemMessage(Interface.getString("ct_error_openotherlinkedtokenwithoutaccess"));
					end
				end
				vImage.clearSelectedTokens();
			end
		end
	end
end
