-- Script for Placeable Fires
--
-- @Author: Alex2009, Sven777b. Grisu118
-- 
-- @date 03/02/2014
-- @version v0.3
-- @Descripion: Readme you can find there: https://github.com/Grisu118/Scripts
-- @web: http://www.ls-landtechnik.de / http://www.alex2009.de / http://grisu118.ch / http://vertexdezign.de
-- @description:
-- Basis für eigene "Placeables" by Alex2009 & Sven777b, Spezifikation "PlaceableTipTrigger" by rafftnix (February 2013) Erlaubnis dafür von Sven777b erhalten.
-- Anpassungen für Platzierbares Feuer by Grisu118
-- Copyright (C) Grisu118, Alex2009, Sven777b, rafftnix, All Rights Reserved.
-- free for noncommerical-usage




PlaceableFire = {};
InitObjectClass(PlaceableFire, "PlaceableFire");

function PlaceableFire:new(isServer, isClient, customMt)
	--print("new Fire");
--	self.particleSystems = {};
--	self.particleSystems_fire=nil;
--	self.particleSystems_smokeBlack=nil;
--	self.particleSystems_smokeWhite=nil;

	if PlaceableFire_mt == nil then
		PlaceableFire_mt = Class(PlaceableFire, Placeable);
	end;
	
	if mt == nil then
		mt = PlaceableFire_mt;
	end;

    self = Placeable:new(isServer, isClient, mt);
    registerObjectClassName(self, "PlaceableFire");
	
	self.isServer=isServer;
	self.isClient=isClient;
	self.mt=mt;
	
	self.update=PlaceableFire.update;

	self.setBurning=PlaceableFire.setBurning;
	self.setTriopan=PlaceableFire.setTriopan;
	self.setWhite=PlaceableFire.setWhite;

    
	
	self.nodeId=0;
	self.timediff=0;
	self.triggerId=nil;
	self.audioId=nil;
	self.TriopanId=nil;
	self.isBurning=true;
	self.isWhite=false;
	self.isVisible=true;
	self.playerInRange=false;
	--print("new Fire Created");
    return self;
end;

function PlaceableFire:delete()
	--print ("Delete")
	if self.particleSystems_fire ~= nil then
		Utils.deleteParticleSystem(self.particleSystems_fire);
	end;
	if self.particleSystems_smokeBlack ~= nil then
		Utils.deleteParticleSystem(self.particleSystems_smokeBlack);
	end;
	if self.particleSystems_smokeWhite ~= nil then
		Utils.deleteParticleSystem(self.particleSystems_smokeWhite);
	end;
--	Utils.deleteParticleSystem(self.particleSystems);
    removeTrigger(self.triggerId);

    unregisterObjectClassName(self);
    PlaceableFire:superClass().delete(self);
end;

function PlaceableFire:deleteFinal()
    PlaceableFire:superClass().deleteFinal(self);
end;

function PlaceableFire:load(xmlFilename, x,y,z, rx,ry,rz, moveMode, initRandom)

    if not PlaceableFire:superClass().load(self, xmlFilename, x,y,z, rx,ry,rz, moveMode, initRandom) then
        return false;
    end;

    if not moveMode then
		local xmlFile = loadXMLFile("TempXML", xmlFilename);
		--self.Hinweis = Utils.indexToObject(self.nodeId, getXMLString(xmlFile, "placeable.Hinweis#index"));
		--PS
--		self.particleSystems = {};
		self.particleSystems_fire = {};
		self.particleSystems_smokeWhite = {};
		self.particleSystems_smokeBlack = {};
		local i = 0;
		while true do
			local ps = string.format("placeable.particleSystems.system(%d)", i);
			local index = Utils.indexToObject(self.nodeId, getXMLString(xmlFile, ps .. "#index"));
			local t = getXMLFloat(xmlFile, ps .. "#type");
			if index == nil then
				break;
			end;
			--print(t);
			if t==0 then
				Utils.loadParticleSystem(xmlFile, self.particleSystems_fire, ps, index, false, nil, self.baseDirectory);
				--print("Fire");
			elseif t==1 then
				Utils.loadParticleSystem(xmlFile, self.particleSystems_smokeBlack, ps, index, false, nil, self.baseDirectory);
				--print("smokeBlack");
			elseif t==2 then
				Utils.loadParticleSystem(xmlFile, self.particleSystems_smokeWhite, ps, index, false, nil, self.baseDirectory);
				--print("smokeWhite");
			end;
			i = i +1;
		end;
		Utils.setEmittingState(self.particleSystems_fire, true);
		Utils.setEmittingState(self.particleSystems_smokeBlack, true);
		Utils.setEmittingState(self.particleSystems_smokeWhite, false);
		--setVisibility(self.Hinweis, false);
		delete(xmlFile);

		local triggerId=Utils.getNoNil(getChild(self.nodeId,"Trigger"),0);
			if triggerId ~= nil then
				addTrigger(triggerId, "triggerCallback", self);
				self.triggerId=triggerId;
				--print("Trigger  Added: "..triggerId.."");
			end;
		
		local audioId=Utils.getNoNil(getChild(self.nodeId,"audio"),0);
			if audioId ~= nil then
				self.audioId=audioId;
			end;
		local TriopanId=Utils.getNoNil(getChild(self.nodeId,"Triopan_by_Grisu118_70"),0);
			if TriopanId ~= nil then
				self.TriopanId=TriopanId;
			end;	
	end;
    return true;
end;

function PlaceableFire:readStream(streamId, connection)
	PlaceableFire:superClass().readStream(self, streamId, connection);

end;

function PlaceableFire:writeStream(streamId, connection)
	PlaceableFire:superClass().writeStream(self, streamId, connection)

end;

function PlaceableFire:readUpdateStream(streamId, timestamp, connection)
	PlaceableFire:superClass().readUpdateStream(self, streamId, timestamp, connection);
end;

function PlaceableFire:writeUpdateStream(streamId, connection, dirtyMask)
	PlaceableFire:superClass().writeUpdateStream(self, streamId, connection, dirtyMask);

end;

function PlaceableFire:update(dt)
	if(self.timediff<1000)then
		self.timediff=self.timediff+dt;
	end;

--  if g_currentMission.controlPlayer and self.playerInRange then
	if self.isBurning then
		Utils.setEmittingState(self.particleSystems_fire, true);
		if self.isWhite then
			Utils.setEmittingState(self.particleSystems_smokeBlack, false);
			Utils.setEmittingState(self.particleSystems_smokeWhite, true);
		else
			Utils.setEmittingState(self.particleSystems_smokeBlack, true);
			Utils.setEmittingState(self.particleSystems_smokeWhite, false);
		end;
		setVisibility(self.audioId, true);
	else
		Utils.setEmittingState(self.particleSystems_fire, false);
		Utils.setEmittingState(self.particleSystems_smokeBlack, false);
		Utils.setEmittingState(self.particleSystems_smokeWhite, false);
		setVisibility(self.audioId, false);
	end;
	if self.TriopanId ~= 0 then
		if self.isVisible then
			setVisibility(self.TriopanId, true);
		else
			setVisibility(self.TriopanId, false);
		end;
	end;
--  end;

	if(self.timediff>1000 and g_currentMission:getIsServer())then
		if g_currentMission.controlPlayer and self.playerInRange then
			
			g_currentMission:addHelpButtonText(g_i18n:getText("PlaceableFireDelete"), InputBinding.PLACEABLEFIRE_DEL);
			
			if self.isBurning then
				g_currentMission:addHelpButtonText(g_i18n:getText("PlaceableFireExtinguish"), InputBinding.PLACEABLEFIRE_Extinguish);
			else
				g_currentMission:addHelpButtonText(g_i18n:getText("PlaceableFirekindle"), InputBinding.PLACEABLEFIRE_Extinguish);
			end;
			if not self.isWhite then
				g_currentMission:addHelpButtonText(g_i18n:getText("PlaceableFirewhite"), InputBinding.PLACEABLEFIRE_SMOKE);
			else
				g_currentMission:addHelpButtonText(g_i18n:getText("PlaceableFireblack"), InputBinding.PLACEABLEFIRE_SMOKE);
			end;
			if self.TriopanId ~= 0 then
				if self.isVisible then
					g_currentMission:addHelpButtonText(g_i18n:getText("PlaceableFireTrio1"), InputBinding.PLACEABLEFIRE_TRIO);
				else
					g_currentMission:addHelpButtonText(g_i18n:getText("PlaceableFireTrio2"), InputBinding.PLACEABLEFIRE_TRIO);
				end;
			end;
			if InputBinding.hasEvent(InputBinding.PLACEABLEFIRE_DEL) then
				g_client:getServerConnection():sendEvent(SellPlaceableEvent:new(self));
				
			end;
			if InputBinding.hasEvent(InputBinding.PLACEABLEFIRE_Extinguish) then
				self:setBurning(not self.isBurning);
				--print(self.isBurning);
			end;
			if InputBinding.hasEvent(InputBinding.PLACEABLEFIRE_SMOKE) then
				self:setWhite(not self.isWhite);
				--print(self.isWhite);
			end;
			if InputBinding.hasEvent(InputBinding.PLACEABLEFIRE_TRIO) then
				self:setTriopan(not self.isVisible);
				--print(self.isVisible);
			end;
		end;
	end;
	
end;

function PlaceableFire:setBurning(state)
	--print(state);
	self.isBurning=state;
	--print(self.isBurning);
end;

function PlaceableFire:setWhite(state)
	--print(state);
	self.isWhite=state;
	--print(self.isWhite);
end;

function PlaceableFire:setTriopan(state)
	--print(state);
	self.isVisible=state;
	--print(self.isVisible);
end;

function PlaceableFire:updateMoving(delta)

end;

function PlaceableFire:updateTrailerTipping(trailer, fillDelta, fillType)

end;

function PlaceableFire:getTipInfoForTrailer(trailer, tipReferencePointIndex)

end;

function PlaceableFire:getTipDistanceFromTrailer(trailer, tipReferencePointIndex)

end;

function PlaceableFire:getNoAllowedText(trailer)

end;

function PlaceableFire:triggerCallback(triggerId, otherId, onEnter, onLeave, onStay)
	if onEnter then
		--print("Trigger entered");
		self.playerInRange=true;
	else
		self.playerInRange=false;
	end;
end;

registerPlaceableType("PlaceableFire", PlaceableFire);
