-- 
-- Fredzaza
--

Godet = {
	prerequisitesPresent = function (specializations)
		return true
	end
}

source("dataS/scripts/vehicles/specializations/SetTurnedOnEvent.lua");
source("dataS/scripts/vehicles/specializations/PloughAreaEvent.lua");
 
function Godet.prerequisitesPresent(specializations)
      return true;
end;

function Godet:load(xmlFile)
  		assert(self.setIsTurnedOn == nil, "Sprayer needs to be the first specialization which implements setIsTurnedOn");
        self.setIsTurnedOn = Godet.setIsTurnedOn;
        self.isTurnedOn = false;
		self.activation = false;

		self.terrassement = false;
        self.terrassementNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassement#index"));

        self.terrassementNodeA = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementa#index"));
        self.terrassementNodeB = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementb#index"));
        self.terrassementNodeC = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementc#index"));
        self.terrassementNodeD = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementd#index"));
        self.terrassementNodeE = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassemente#index"));
        self.terrassementNodeF = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementf#index"));
        self.terrassementNodeG = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementg#index"));
        self.terrassementNodeH = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementh#index"));
        self.terrassementNodeI = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementi#index"));
        self.terrassementNodeJ = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementj#index"));
        self.terrassementNodeK = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementk#index"));
        self.terrassementNodeL = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementl#index"));
        self.terrassementNodeM = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementm#index"));
        self.terrassementNodeN = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementn#index"));
        self.terrassementNodeO = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassemento#index"));

        self.aiTerrainDetailChannel1 = g_currentMission.ploughChannel;
        self.aiTerrainDetailChannel2 = g_currentMission.sowingChannel;
    
        self.fillremplissage = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.fillremplissage"), 500);
        self.fillvidage = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.fillvidage"), 5);
        
        self.godet = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.godet#index"));

		self.terralargeur = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.terralargeur"), 500);
		self.terrahauteur = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.terrahauteur"), 500);
		
		self.tera1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.tera1#index"));
		self.tera2 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.tera2#index"));	

		self.CategorieGodet = getXMLString(xmlFile, "vehicle.CategorieGodet");
end;
  
function Godet:delete()
end;
  
function Godet:readStream(streamId, connection)
    local turnedOn = streamReadBool(streamId);
	self:setIsTurnedOn(turnedOn, true);
end;
 
function Godet:writeStream(streamId, connection)
	streamWriteBool(streamId, self.isTurnedOn);
end;
 
function Godet:mouseEvent(posX, posY, isDown, isUp, button)
end;
 
function Godet:keyEvent(unicode, sym, modifier, isDown)
end;
 
function Godet:update(dt)
    if self.isClient then
        if self:getIsActiveForInput() then
            if InputBinding.hasEvent(InputBinding.Godet) then
                self:setIsTurnedOn(not self.isTurnedOn);
            end;
        end;
    end;     
    if self.isTurnedOn then
		self.activation = true ;
	else	
		self.activation = false;	 
	end;
end;
 
function Godet:updateTick(dt)
    if self:getIsActiveForInput() then
        if InputBinding.hasEvent(InputBinding.Godet) then
        	self.activation = not self.activation;
        end;
    end;

    local x,y,z = getWorldTranslation(self.terrassementNode);
    local terrainHeight = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x, 0, z);
		if y<= terrainHeight then
			self.terrassement = true;
		else
			self.terrassement = false;
		end;
	
    if self.terrassement then
            if self.activation then
                if self.fillLevel ~= self.capacity then
                        local workAreasSend = {};
                        for k, workArea in pairs(self.workAreas) do
                            if self:getIsAreaActive(workArea) then
                                local x,y,z = getWorldTranslation(workArea.start);
                                local x1,y1,z1 = getWorldTranslation(workArea.width);
                                local x2,y2,z2 = getWorldTranslation(workArea.height);
                                --Utils.updatePloughArea(x, z, x1, z1, x2, z2, not self.PloughLimitToField);
                                table.insert(workAreasSend, {x,z,x1,z1,x2,z2});
                            end;
                        end;
                        if (table.getn(workAreasSend) > 0) then
                            local realArea = PloughAreaEvent.runLocally(workAreasSend, limitToField, limitGrassDestructionToField, angle);
                            local usage = self.fillremplissage * dt*0.001;
                            local usedFruitType = FruitUtil.FRUITTYPE_DIRT;
                            local usedFillType = FruitUtil.fruitTypeToFillType[usedFruitType];                                                                             
                            self:setFillLevel(self.fillLevel + usage, usedFillType);
						end;
                end;
			end;
    end;        
end;
 
function Godet:draw()
    if self.activation then
		g_currentMission:addHelpButtonText(g_i18n:getText("TerrOff"), InputBinding.Godet);
	else
		g_currentMission:addHelpButtonText(g_i18n:getText("TerrOn"), InputBinding.Godet);
    end;   
end;

function Godet:onAttach(attacherVehicle)
end;
  
function Godet:onDetach()
end;

function Godet:onActivate()
end;
 
function Godet:onDeactivate()       
end;
 
function Godet:onDeactivateSounds()
end;
 
function Godet:aiRotateLeft()
end;
 
function Godet:aiRotateRight()
end;
 
function Godet:aiTurnOn()
end;
 
function Godet:aiTurnOff()
end;
 
function Godet:aiInvertsMarkerOnTurn(turnLeft)
end;
 
function Godet:setRotationMax(rotationMax, noEventSend)
end;

function Godet:setIsTurnedOn(isTurnedOn, noEventSend)
    SetTurnedOnEvent.sendEvent(self, isTurnedOn, noEventSend)
    self.isTurnedOn = isTurnedOn;
end;
   
function Godet:onDeactivate()
    self.isTurnedOn = false;
end;
   
function Godet:onLeave()
    self.isTurnedOn = false;
end;


