--
-- InteractiveButtons
-- Specialization for an interactive control button
--
-- @author  	Manuel Leithner (SFM-Modding)
-- @version 	v2.1
-- @date  		29/08/11
-- @history:	v1.0 - Initial version
--				v2.0 - converted to ls2011
--				v2.1 - improvements
--
-- free for noncommerical-usage
--

InteractiveButtons = {};

function InteractiveButtons.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(InteractiveControl, specializations);
end;

function InteractiveButtons:load(xmlFile)

	local i=0;
	while true do
		local buttonName = string.format("vehicle.interactiveComponents.buttons.button(%d)", i);	
		if not hasXMLProperty(xmlFile, buttonName) then
			break;
		end;
		local name = Utils.getNoNil(g_i18n:getText(getXMLString(xmlFile, buttonName .. "#name")), "ERROR");
		local mark = Utils.indexToObject(self.components, getXMLString(xmlFile, buttonName .. "#mark"));
		local highlight = getChildAt(mark, 0);
		local size = Utils.getNoNil(getXMLFloat(xmlFile, buttonName .. "#size"), 0.1);
		local event = getXMLString(xmlFile, buttonName .. "#event");
		local onMessage = g_i18n:getText(Utils.getNoNil(getXMLString(xmlFile, buttonName .. "#onMessage"), "ic_button_on"));
		local offMessage =  g_i18n:getText(Utils.getNoNil(getXMLString(xmlFile, buttonName .. "#offMessage") , "ic_button_off"));
	
		local button = Button:new(nil, highlight, name, mark, size, event, self, onMessage, offMessage, self.infoBar);
		
		button.synch = Utils.getNoNil(getXMLBool(xmlFile, buttonName .. "#synch"), true);

		table.insert(self.interactiveObjects, button);
		i = i + 1;
	end;
end;

function InteractiveButtons:delete()
end;

function InteractiveButtons:mouseEvent(posX, posY, isDown, isUp, button)
end;

function InteractiveButtons:keyEvent(unicode, sym, modifier, isDown)
end;

function InteractiveButtons:update(dt)	
end;

function InteractiveButtons:draw()
end;



--
-- Button Class
-- Specifies an interactive Button
--
-- SFM-Modding
-- @author  Manuel Leithner
-- @date  29/08/11
--

Button = {};

function Button:new(node, highlight, name, mark, size, event, vehicle, onMessage, offMessage, infobar)

	local Button_mt = Class(Button, InteractiveComponentInterface);	
    local instance = InteractiveComponentInterface:new(node, highlight, name, mark, size, onMessage, offMessage, infobar, Button_mt);

	instance.vehicle = vehicle;
	instance.event = event;
	
	return instance;	
end;

function Button:delete()
	InteractiveComponentInterface.delete(self);
end;

function Button:mouseEvent(posX, posY, isDown, isUp, button)
	InteractiveComponentInterface.mouseEvent(self, posX, posY, isDown, isUp, button);
end;

function Button:keyEvent(unicode, sym, modifier, isDown)
	InteractiveComponentInterface.keyEvent(self, unicode, sym, modifier, isDown);
end;

function Button:update(dt)
	if self.vehicle ~= nil then
		if self.event == "togglePipe" then
			self.isOpen = self.vehicle.targetPipeState ~= 1;
		elseif self.event == "toggleChopper" then
			self.isOpen = not self.vehicle.isStrawActive;
                elseif self.event == "toggleMolotilkaEvent" then
			self.isOpen = self.vehicle.turnThresherActive;
	        elseif self.event == "toggleBeaconLight" then
	                self.isOpen = self.vehicle.beaconLightsActive == true;  
		elseif self.event == "toggleMotorIgnition" then
			self.isOpen = self.vehicle.ignitionMode ~= 0;
	        elseif self.event == "toggleWorkFront" then
	                self.isOpen = (self.vehicle.lightsTypesMask == 2) or (self.vehicle.lightsTypesMask == 3) or (self.vehicle.lightsTypesMask == 6) or (self.vehicle.lightsTypesMask == 7);
	        elseif self.event == "toggleWorkRear" then
	                self.isOpen = (self.vehicle.lightsTypesMask == 4) or (self.vehicle.lightsTypesMask == 5) or (self.vehicle.lightsTypesMask == 6) or (self.vehicle.lightsTypesMask == 7);
	        elseif self.event == "toggleFrontLight" then
	                self.isOpen = (self.vehicle.lightsTypesMask == 1) or (self.vehicle.lightsTypesMask == 3);
		elseif self.event == "cablight" then
			self.isOpen = self.vehicle.cl.turnOn;
		elseif self.event == "pipelight" then
			self.isOpen = self.vehicle.pl.turnOn;
		end;
	end;
	InteractiveComponentInterface.update(self, dt);
end;

function Button:draw()
	InteractiveComponentInterface.draw(self);
end;

function Button:doAction(noEventSend, forceAction)
	if self.vehicle ~= nil then
		if self.event == "togglePipe" then
			if forceAction == nil then
				local nextState = self.vehicle.targetPipeState+1;
				if nextState > self.vehicle.numPipeStates then
					nextState = 1;
				end;
				self.vehicle:setPipeState(nextState, true);
			end;
		elseif self.event == "toggleChopper" then
			local newValue = not self.vehicle.isStrawActive;
			if forceAction ~= nil then
				newValue = forceAction;
			end;
			if self.vehicle.lastValidInputFruitType ~= FruitUtil.FRUITTYPE_UNKNOWN then
				local fruitDesc = FruitUtil.fruitIndexToDesc[self.vehicle.lastValidInputFruitType];
				if fruitDesc.hasWindrow then
					self.vehicle:setIsStrawActive(newValue, true);
				else
					self.vehicle.toggleChopperWarningTime = self.vehicle.time + 2000;
				end;
			else
				self.vehicle:setIsStrawActive(newValue, true);				
			end;
		elseif self.event == "toggleBeaconLight" then
            if forceAction == nil then
                if self.vehicle.beaconLightsActive then
				    self.vehicle:setBeaconLightsVisibility(false, true);
			    else
                    self.vehicle:setBeaconLightsVisibility(true, true);
			    end;
			end;
		elseif self.event == "toggleMotorIgnition" then
		    if forceAction == nil then
			    if self.vehicle.ignitionMode ~= 1 or (self.vehicle.ignitionMode == 1 and (self.vehicle.preHeatT == 0 and not self.vehicle.preHeatS) or (self.vehicle.preHeatS and self.vehicle.preHeatT >= 0)) then
                    self.vehicle:setManualIgnitionMode(math.abs(self.vehicle.ignitionMode + 1));
                end;

                if self.lastIgnitionMode == nil then
					self.lastIgnitionMode = 0;
				end;

				if self.lastIgnitionMode ~= self.vehicle.ignitionMode then
					if self.vehicle.ignitionMode == 2 then
						self.vehicle.key.mode = 2;
					end;

					self.lastIgnitionMode = self.vehicle.ignitionMode;
				end;
	        end;
		elseif self.event == "toggleWorkFront" then
		    if forceAction == nil then
			    if self.vehicle.lightsTypesMask == 0 then
				    self.vehicle:setLightsTypesMask(2, true);
			    elseif self.vehicle.lightsTypesMask == 1  then
				    self.vehicle:setLightsTypesMask(3, true);
			    elseif self.vehicle.lightsTypesMask == 2 then
				    self.vehicle:setLightsTypesMask(0, true);
			    elseif self.vehicle.lightsTypesMask == 3 then
				    self.vehicle:setLightsTypesMask(1, true);
			    elseif self.vehicle.lightsTypesMask == 4 then
				    self.vehicle:setLightsTypesMask(6, true);
			    elseif self.vehicle.lightsTypesMask == 5 then
				    self.vehicle:setLightsTypesMask(7, true);
			    elseif self.vehicle.lightsTypesMask == 6 then
				    self.vehicle:setLightsTypesMask(4, true);
			    elseif self.vehicle.lightsTypesMask == 7 then
				    self.vehicle:setLightsTypesMask(5, true);
				end;
			end;
		elseif self.event == "toggleWorkRear" then
            if forceAction == nil then
			    if self.vehicle.lightsTypesMask == 0 then
				    self.vehicle:setLightsTypesMask(4, true);
			    elseif self.vehicle.lightsTypesMask == 1  then
				    self.vehicle:setLightsTypesMask(5, true);
			    elseif self.vehicle.lightsTypesMask == 2 then
				    self.vehicle:setLightsTypesMask(6, true);
			    elseif self.vehicle.lightsTypesMask == 3 then
				    self.vehicle:setLightsTypesMask(7, true);
			    elseif self.vehicle.lightsTypesMask == 4 then
				    self.vehicle:setLightsTypesMask(0, true);
			    elseif self.vehicle.lightsTypesMask == 5 then
				    self.vehicle:setLightsTypesMask(1, true);
			    elseif self.vehicle.lightsTypesMask == 6 then
				    self.vehicle:setLightsTypesMask(2, true);
			    elseif self.vehicle.lightsTypesMask == 7 then
				    self.vehicle:setLightsTypesMask(3, true);
				end;
			end;
		elseif self.event == "toggleFrontLight" then
            if forceAction == nil then
			    if self.vehicle.lightsTypesMask == 0 then
				    self.vehicle:setLightsTypesMask(1, true);
			    elseif self.vehicle.lightsTypesMask == 1  then 
				    self.vehicle:setLightsTypesMask(0, true);
			    elseif self.vehicle.lightsTypesMask == 2 then
				    self.vehicle:setLightsTypesMask(3, true);
			    elseif self.vehicle.lightsTypesMask == 3 then
				    self.vehicle:setLightsTypesMask(2, true);
			    end;
			end;
		elseif self.event == "cablight" then
			if forceAction == nil then
				local state = not self.vehicle.cl.turnOn;
				self.vehicle:setCablight(state, true);
			end;
		elseif self.event == "pipelight" then
			if forceAction == nil then
				local state = not self.vehicle.pl.turnOn;
				self.vehicle:setPipelight(state, true);
			end;
		elseif self.event == "toggleMolotilkaEvent" then
			if forceAction == nil then
				if self.vehicle.isThreshing or self.vehicle:getIsStartThreshingAllowed() then
					self.vehicle:setIsThreshing(not self.vehicle.isThreshing);
				end;
			end;
		end;
	end;
end;

function Button:onEnter(dt)
	InteractiveComponentInterface.onEnter(self, dt);
end;

function Button:onExit(dt)
	InteractiveComponentInterface.onExit(self, dt);
end;

function Button:setActive()
	InteractiveComponentInterface.setActive(self, isActive);
end;

function Button:setVisible(isVisible)
	InteractiveComponentInterface.setVisible(self, isVisible);
end;