--
-- V900.lua
--
-- @author  MRA-Modding(Alex2009,Repi)
-- @website www.alex2009.de, www.mra-modding.de
-- @date    21.05.2011
--
--Modifikation erst nach rücksprache mit dem Autor(Alex2009) erlaubt.
--Modification allowed only after consultation with the author(Alex2009).

V900 = {};

function V900.prerequisitesPresent(specializations)
    return true;
end;

function V900:load(xmlFile)
    self.setAIstate = SpecializationUtil.callSpecializationsFunction("setAIstate");
    self.camerathree = Utils.indexToObject(self.components, getXMLString(xmlFile,"vehicle.ReversingCameras#camthree"));
    self.camerafour = Utils.indexToObject(self.components, getXMLString(xmlFile,"vehicle.ReversingCameras#camfour"));
	self.oldvehicle = nil;
	self.MRACutterstartreel = false;
	self.MRACutterstopreel = false;
    self.AIState = false;
	self.isreelStartetoverStartReel = false;
    local messerNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.RapsMesser#index"));
    self.messerChar = 0;
    if messerNode ~= nil then
        self.messerChar = getAnimCharacterSet(messerNode);
        if self.messerChar ~= 0 then
            local clipname = getAnimClipIndex(self.messerChar, getXMLString(xmlFile, "vehicle.RapsMesser#clipname"));
            setAnimTrackLoopState(self.messerChar, 0, true);
            assignAnimTrackClip(self.messerChar, 0, clipname);
			self.messerClip = clipname;
        end;
    end;
    local schneidmesserNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.SchneidMesser#index"));
    self.schneidmesserChar = 0;
    if schneidmesserNode ~= nil then
        self.schneidmesserChar = getAnimCharacterSet(schneidmesserNode);
        if self.schneidmesserChar ~= 0 then
            local clipname = getAnimClipIndex(self.schneidmesserChar, getXMLString(xmlFile, "vehicle.SchneidMesser#clipname"));
            setAnimTrackLoopState(self.schneidmesserChar, 0, true);
            assignAnimTrackClip(self.schneidmesserChar, 0, clipname);
			self.schneidmesserClip = clipname;
        end;
    end;
    self.RollSpeed = 0.00001;
    self.reelSpeedMax = self.reelSpeed;
    self.reelSpeedEnd = (self.reelSpeed-0.00001);
	---------------Greeenstar------------------
    self.GreenStarLeft = {};
    self.GreenStarLeft.startOutside = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.GreenStar.GreenStarLeft#OutStart"));
    self.GreenStarLeft.widthOutside = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.GreenStar.GreenStarLeft#OutWidth"));
    self.GreenStarLeft.heightOutside = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.GreenStar.GreenStarLeft#OutHeight"));
    self.GreenStarLeft.startInside = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.GreenStar.GreenStarLeft#InsideStart"));
    self.GreenStarLeft.widthInside = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.GreenStar.GreenStarLeft#InsideWidth"));
    self.GreenStarLeft.heightInside = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.GreenStar.GreenStarLeft#InsideHeight"));

    self.GreenStarRight = {};
    self.GreenStarRight.startOutside = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.GreenStar.GreenStarRight#OutStart"));
    self.GreenStarRight.widthOutside = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.GreenStar.GreenStarRight#OutWidth"));
    self.GreenStarRight.heightOutside = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.GreenStar.GreenStarRight#OutHeight"));
    self.GreenStarRight.startInside = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.GreenStar.GreenStarRight#InsideStart"));
    self.GreenStarRight.widthInside = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.GreenStar.GreenStarRight#InsideWidth"));
	self.GreenStarRight.heightInside = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.GreenStar.GreenStarRight#InsideHeight"));	
    self.MRACutter = true;
end;

function V900:delete()
end;

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

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

function V900:update(dt)
    if self.attacherVehicle ~= nil and self:getIsActive() and self:getIsActiveForInput() then
        local CamState = false;
        if self.attacherVehicle.LEXK ~= nil and self.attacherVehicle.LEXM ~= nil and self.attacherVehicle.LEXM then
            if self.attacherVehicle.LEXK == 3 or self.attacherVehicle.LEXK == 4 then
                CamState = true;
            else
                CamState = false;
            end;
        end;
        if (self.AIState ~= CamState) or (self.animParts[1].toMax ~= self.AIState) or (self.animParts[2].toMax ~= self.AIState) then
            self:setAIstate(CamState);
        end;
    elseif (self.attacherVehicle == nil and self.AIState) or (self.animParts[1].toMax ~= self.AIState) or (self.animParts[2].toMax ~= self.AIState) then
        self:setAIstate(false);
    end;
end;

function V900:readStream(streamId, connection)
	self.AIState = streamReadBool(streamId);
end;

function V900:writeStream(streamId, connection)
	streamWriteBool(streamId, self.AIState);
end;

function V900:updateTick(dt)
    --[[if not g_currentMission.missionDynamicInfo.isMultiplayer then
        if self.attacherVehicle ~= nil then
            if (self.AIState ~= self.attacherVehicle.isAutopilotActivated) or (self.animParts[1].toMax ~= self.AIState) or (self.animParts[2].toMax ~= self.AIState) then
                self:setAIstate(self.attacherVehicle.isAutopilotActivated);
            end;
        elseif (self.attacherVehicle == nil and self.AIState) or (self.animParts[1].toMax ~= self.AIState) or (self.animParts[2].toMax ~= self.AIState) then
            self:setAIstate(false);
        end;
    end;]]--
    if self.reelStarted then
        if not g_currentMission.missionDynamicInfo.isMultiplayer then
            if self.attacherVehicle.isMotorStarted ~= nil and self.attacherVehicle.isAutopilotActivated ~= nil then
                if not self.attacherVehicle.isAutopilotActivated and self.attacherVehicle.isMotorStarted then
                    local x,y,z = getWorldTranslation(self.GreenStarRight.startOutside);--Rechts Start
                    local x1,y1,z1 = getWorldTranslation(self.GreenStarRight.widthOutside);
                    local x2,y2,z2 = getWorldTranslation(self.GreenStarRight.heightOutside);
                    if x ~= nil and z ~= nil and x1 ~= nil and z1 ~= nil and x2 ~= nil and x2 ~= nil then
                        self.GSRechtsOut = Utils.getFruitArea(self.currentInputFruitType, x, z, x1, z1, x2, z2);
                    end;
                    local x,y,z = getWorldTranslation(self.GreenStarRight.startInside);
                    local x1,y1,z1 = getWorldTranslation(self.GreenStarRight.widthInside);
                    local x2,y2,z2 = getWorldTranslation(self.GreenStarRight.heightInside);
                    if x ~= nil and z ~= nil and x1 ~= nil and z1 ~= nil and x2 ~= nil and x2 ~= nil then
                        self.GSRechtsIn = Utils.getFruitArea(self.currentInputFruitType, x, z, x1, z1, x2, z2);--Rechts Ende!
                    end;
                    local x,y,z = getWorldTranslation(self.GreenStarLeft.startOutside);--Links Start
                    local x1,y1,z1 = getWorldTranslation(self.GreenStarLeft.widthOutside);
                    local x2,y2,z2 = getWorldTranslation(self.GreenStarLeft.heightOutside);
                    if x ~= nil and z ~= nil and x1 ~= nil and z1 ~= nil and x2 ~= nil and x2 ~= nil then
                        self.GSLinksOut = Utils.getFruitArea(self.currentInputFruitType, x, z, x1, z1, x2, z2);
                    end;
                    local x,y,z = getWorldTranslation(self.GreenStarLeft.startInside);
                    local x1,y1,z1 = getWorldTranslation(self.GreenStarLeft.widthInside);
                    local x2,y2,z2 = getWorldTranslation(self.GreenStarLeft.heightInside);
                    if x ~= nil and z ~= nil and x1 ~= nil and z1 ~= nil and x2 ~= nil and x2 ~= nil then
                        self.GSLinksIn = Utils.getFruitArea(self.currentInputFruitType, x, z, x1, z1, x2, z2);--Links Ende
                    end;
                    if self.GSRechtsOut ~= nil and self.GSLinksOut ~= nil then 
                        if self.GSRechtsOut < self.GSLinksOut then
                            self.KornStandLinks = true;
                        else
                            self.KornStandLinks = false;
                        end;
                    end;
                    if self.attacherVehicle ~= nil then
                        self.attacherVehicle.GSLlights = 0;
                        self.attacherVehicle.GSRlights = 0;
                    end;
                    if self.GSLinksOut ~= nil and self.GSRechtsIn ~= nil then--Rechts
                        if self.GSLinksOut >= self.GSRechtsIn and (self.KornStandLinks and self.GSRechtsOut <= 10) or (not self.KornStandLinks and self.GSRechtsOut >= 10) then
                            local verhaeltnis = self.GSRechtsIn / self.GSLinksOut; 
                            local lichter = 11 - math.ceil(11 * verhaeltnis);	
                            if self.attacherVehicle ~= nil then
                                self.attacherVehicle.GSLlights = lichter;
                            end;
                        elseif (self.KornStandLinks and self.GSRechtsOut >= 5) or (not self.KornStandLinks and self.GSRechtsOut <= 10) then
                            local verhaeltnis = self.GSRechtsOut / self.GSLinksOut; 
                            local lichter = math.ceil(11 * verhaeltnis);	
                            if self.attacherVehicle ~= nil then
                                self.attacherVehicle.GSRlights = lichter;
                            end;				
                        end;
                    end;
                    if self.GSRechtsOut ~= nil and self.GSLinksIn ~= nil then--Links
                        if self.GSRechtsOut >= self.GSLinksIn and (self.KornStandLinks and self.GSLinksOut >= 10) or (not self.KornStandLinks and self.GSLinksOut <= 10) then
                            local verhaeltnis = self.GSLinksIn / self.GSRechtsOut; 
                            local lichter = 11 - math.ceil(11 * verhaeltnis);	
                            if self.attacherVehicle ~= nil then
                                self.attacherVehicle.GSRlights = lichter;
                            end;
                        elseif (self.KornStandLinks and self.GSLinksOut <= 5) or (not self.KornStandLinks and self.GSLinksOut >= 10) then
                            local verhaeltnis = self.GSLinksOut / self.GSRechtsOut; 
                            local lichter = math.ceil(11 * verhaeltnis);	
                            if self.attacherVehicle ~= nil then
                                self.attacherVehicle.GSLlights = lichter;
                            end;				
                        end;
                    end;
                else
                    self.attacherVehicle.GSRlights = 0;
                    self.attacherVehicle.GSLlights = 0;
                    self.attacherVehicle.GSRlights = nil;
                    self.attacherVehicle.GSLlights = nil;
                end;
            end;
        else
            if self.attacherVehicle.isMotorStarted ~= nil and self.attacherVehicle.isAIThreshing ~= nil then
                if not self.attacherVehicle.isAIThreshing and self.attacherVehicle.isMotorStarted then
                    local x,y,z = getWorldTranslation(self.GreenStarRight.startOutside);--Rechts Start
                    local x1,y1,z1 = getWorldTranslation(self.GreenStarRight.widthOutside);
                    local x2,y2,z2 = getWorldTranslation(self.GreenStarRight.heightOutside);
                    if x ~= nil and z ~= nil and x1 ~= nil and z1 ~= nil and x2 ~= nil and x2 ~= nil then
                        self.GSRechtsOut = Utils.getFruitArea(self.currentInputFruitType, x, z, x1, z1, x2, z2);
                    end;
                    local x,y,z = getWorldTranslation(self.GreenStarRight.startInside);
                    local x1,y1,z1 = getWorldTranslation(self.GreenStarRight.widthInside);
                    local x2,y2,z2 = getWorldTranslation(self.GreenStarRight.heightInside);
                    if x ~= nil and z ~= nil and x1 ~= nil and z1 ~= nil and x2 ~= nil and x2 ~= nil then
                        self.GSRechtsIn = Utils.getFruitArea(self.currentInputFruitType, x, z, x1, z1, x2, z2);--Rechts Ende!
                    end;
                    local x,y,z = getWorldTranslation(self.GreenStarLeft.startOutside);--Links Start
                    local x1,y1,z1 = getWorldTranslation(self.GreenStarLeft.widthOutside);
                    local x2,y2,z2 = getWorldTranslation(self.GreenStarLeft.heightOutside);
                    if x ~= nil and z ~= nil and x1 ~= nil and z1 ~= nil and x2 ~= nil and x2 ~= nil then
                        self.GSLinksOut = Utils.getFruitArea(self.currentInputFruitType, x, z, x1, z1, x2, z2);
                    end;
                    local x,y,z = getWorldTranslation(self.GreenStarLeft.startInside);
                    local x1,y1,z1 = getWorldTranslation(self.GreenStarLeft.widthInside);
                    local x2,y2,z2 = getWorldTranslation(self.GreenStarLeft.heightInside);
                    if x ~= nil and z ~= nil and x1 ~= nil and z1 ~= nil and x2 ~= nil and x2 ~= nil then
                        self.GSLinksIn = Utils.getFruitArea(self.currentInputFruitType, x, z, x1, z1, x2, z2);--Links Ende
                    end;
                    if self.GSRechtsOut ~= nil and self.GSLinksOut ~= nil then 
                        if self.GSRechtsOut < self.GSLinksOut then
                            self.KornStandLinks = true;
                        else
                            self.KornStandLinks = false;
                        end;
                    end;
                    if self.attacherVehicle ~= nil then
                        self.attacherVehicle.GSLlights = 0;
                        self.attacherVehicle.GSRlights = 0;
                    end;
                    if self.GSLinksOut ~= nil and self.GSRechtsIn ~= nil then--Rechts
                        if self.GSLinksOut >= self.GSRechtsIn and (self.KornStandLinks and self.GSRechtsOut <= 10) or (not self.KornStandLinks and self.GSRechtsOut >= 10) then
                            local verhaeltnis = self.GSRechtsIn / self.GSLinksOut; 
                            local lichter = 11 - math.ceil(11 * verhaeltnis);	
                            if self.attacherVehicle ~= nil then
                                self.attacherVehicle.GSLlights = lichter;
                            end;
                        elseif (self.KornStandLinks and self.GSRechtsOut >= 5) or (not self.KornStandLinks and self.GSRechtsOut <= 10) then
                            local verhaeltnis = self.GSRechtsOut / self.GSLinksOut; 
                            local lichter = math.ceil(11 * verhaeltnis);	
                            if self.attacherVehicle ~= nil then
                                self.attacherVehicle.GSRlights = lichter;
                            end;				
                        end;
                    end;
                    if self.GSRechtsOut ~= nil and self.GSLinksIn ~= nil then--Links
                        if self.GSRechtsOut >= self.GSLinksIn and (self.KornStandLinks and self.GSLinksOut >= 10) or (not self.KornStandLinks and self.GSLinksOut <= 10) then
                            local verhaeltnis = self.GSLinksIn / self.GSRechtsOut; 
                            local lichter = 11 - math.ceil(11 * verhaeltnis);	
                            if self.attacherVehicle ~= nil then
                                self.attacherVehicle.GSRlights = lichter;
                            end;
                        elseif (self.KornStandLinks and self.GSLinksOut <= 5) or (not self.KornStandLinks and self.GSLinksOut >= 10) then
                            local verhaeltnis = self.GSLinksOut / self.GSRechtsOut; 
                            local lichter = math.ceil(11 * verhaeltnis);	
                            if self.attacherVehicle ~= nil then
                                self.attacherVehicle.GSLlights = lichter;
                            end;				
                        end;
                    end;
                else
                    self.attacherVehicle.GSRlights = 0;
                    self.attacherVehicle.GSLlights = 0;
                    self.attacherVehicle.GSRlights = nil;
                    self.attacherVehicle.GSLlights = nil;
                end;
            end;
        end;
		-----------------------------GreenStar Ende------------------------
		if self.MRACutterstartreel then
            local haspel = false;
            local welle = false;
		    if self.reelSpeed >= self.reelSpeedMax then
                self.reelSpeed = self.reelSpeedMax;
				haspel = true;
            elseif self.reelSpeed <= self.reelSpeedMax and self.reelSpeed >= 0.00001 then
                self.reelSpeed = self.reelSpeed + 0.00003;
            end;
            if self.RollSpeed >= 0.01 then
                self.RollSpeed = 0.00001;
                welle = true;
            elseif self.RollSpeed <= 0.01 and self.RollSpeed >= 0.00001 then
                self.RollSpeed = self.RollSpeed + 0.00006;
                for _, rollNode in pairs(self.rollNodes) do
                    rollNode.speed = -self.RollSpeed;
                end;
            end;
            if welle and haspel then
                self.MRACutterstartreel = false;
            end;
        end;
	    if getAnimTrackTime(self.schneidmesserChar, self.schneidmesserClip) == 0 then
            setAnimTrackTime(self.schneidmesserChar, self.schneidmesserClip, 0.0);
		    setAnimTrackSpeedScale(self.schneidmesserChar, self.schneidmesserClip, 1);
		    enableAnimTrack(self.schneidmesserChar, self.schneidmesserClip);
        end;
        if self.lastArea > 0 then
            if self.currentFruitType ~= FruitUtil.FRUITTYPE_UNKNOWN then
                fruitType = FruitUtil.fruitIndexToDesc[self.currentFruitType].name;
    	    	if fruitType == "rape" then
			        if getAnimTrackTime(self.messerChar, self.messerClip) == 0 then
                        setAnimTrackTime(self.messerChar, self.messerClip, 0.0);
    		            setAnimTrackSpeedScale(self.messerChar, self.messerClip, 2);
			            enableAnimTrack(self.messerChar, self.messerClip);
                    end;
			    else
		            if getAnimTrackTime(self.messerChar, self.messerClip) > 0.0 then
                        setAnimTrackTime(self.messerChar, self.messerClip, 0.0);
    	                setAnimTrackSpeedScale(self.messerChar, self.messerClip, 0);
		                disableAnimTrack(self.messerChar, self.messerClip);
		            end;
			    end;
		    else
			    if getAnimTrackTime(self.messerChar, self.messerClip) > 0.0 then
                    setAnimTrackTime(self.messerChar, self.messerClip, 0.0);
		            setAnimTrackSpeedScale(self.messerChar, self.messerClip, 0);
		            disableAnimTrack(self.messerChar, self.messerClip);
		        end;
		    end;
        end;
	else
	    if getAnimTrackTime(self.schneidmesserChar, self.schneidmesserClip) > 0.0 then
            setAnimTrackTime(self.schneidmesserChar, self.schneidmesserClip, 0.0);
		    setAnimTrackSpeedScale(self.schneidmesserChar, self.schneidmesserClip, 0);
		    disableAnimTrack(self.schneidmesserChar, self.schneidmesserClip);
		end;
	    if getAnimTrackTime(self.messerChar, self.messerClip) > 0.0 then
            setAnimTrackTime(self.messerChar, self.messerClip, 0.0);
		    setAnimTrackSpeedScale(self.messerChar, self.messerClip, 0);
		    disableAnimTrack(self.messerChar, self.messerClip);
		end;
    end;
    if self.attacherVehicle ~= nil then
	    if self.attacherVehicle.attachedCutters ~= nil then
            for cutter,implement in pairs(self.attacherVehicle.attachedCutters) do
                local jointDesc = self.attacherVehicle.attacherJoints[implement.jointDescIndex];
			    if jointDesc ~= nil then
	                if jointDesc.moveDown == false then
			            if self.attacherVehicle.allowsThreshing then
				            self.attacherVehicle.allowsThreshing = false;
				        end;
			        else
				        if self.attacherVehicle.allowsThreshing == false then
				            self.attacherVehicle.allowsThreshing = true;
				        end;	
                    end;
			    end;
            end;
	    end;
	end;
	if self.MRACutterstopreel and self.isreelStartetoverStartReel then
	    if self.reelSpeed <= 0.00000 then
            self.reelSpeed = self.reelSpeedMax;
			self.MRACutterstopreel = false;
            self.isreelStartetoverStartReel = false;
        elseif self.reelSpeed <= self.reelSpeedEnd and self.reelSpeed >= 0.00000 then
            self.reelSpeed = self.reelSpeed - 0.000025;
        end;
		if self.MRACutterstopreel then
            for _, rollNode in pairs(self.rollNodes) do
                rotate(rollNode.node, dt*self.reelSpeed, 0, 0);
            end;
	        rotate(self.reelNode, -dt*self.reelSpeed, 0, 0);
            local atx, aty, atz = getRotation(self.reelNode);  
            for i=1, self.spikesCount do
                local spike = getChildAt(self.spikesRootNode, i-1);
                setRotation(spike, -atx, aty, atz);
            end;
		end;
	end;
end;

function V900:draw()
end;

function V900:onAttach(attacherVehicle)
	attacherVehicle.GSRlights = 0;
	attacherVehicle.GSLlights = 0;
    attacherVehicle.camerathree = self.camerathree;
    attacherVehicle.camerafour = self.camerafour;
	self.oldvehicle = attacherVehicle;
end;

function V900:onDetach()
    if self.oldvehicle ~= nil then
	    self.oldvehicle.GSRlights = 0;
		self.oldvehicle.GSLlights = 0;
		self.oldvehicle.GSRlights = nil;
		self.oldvehicle.GSLlights = nil;
        self.oldvehicle.camerathree = nil;
        self.oldvehicle.camerafour = nil;	
	    self.oldvehicle = nil;
	end;
end;

function V900:onStartReel()
    self.MRACutterstopreel = false;
    self.MRACutterstartreel = true;
    self.reelSpeed = 0.00001;
    self.RollSpeed = 0.00001;
	self.isreelStartetoverStartReel = true;
end;

function V900:onStopReel()
    if self.oldvehicle ~= nil then
        self.oldvehicle.GSRlights = 0;
        self.oldvehicle.GSLlights = 0;
        self.oldvehicle.GSRlights = nil;
        self.oldvehicle.GSLlights = nil;
    end;
    if self.isreelStartetoverStartReel then
        self.MRACutterstopreel = true;
        self.reelSpeed = self.reelSpeed - 0.00001;
        self.RollSpeed = self.RollSpeed - 0.00001;
	end;
end;

function V900:setAIstate(state,noEventSend)
    if (self.attacherVehicle ~= nil) and (g_settingsNickname == self.attacherVehicle.controllerName) then
        self.AIState = state;
        self:toggle(1,state);
        self:toggle(2,state);
    end;
end;