--
-- Specialization Motor
--
-- @author  	Aluha74rus
-- @version 	v1.1
-- @date  		25/01/19

AnimParts = {};
AnimParts.ModDirectory = g_currentModDirectory

function AnimParts.initSpecialization()
   g_configurationManager:addConfigurationType("tuning", g_i18n:getText("configuration_tuning"), nil, nil, nil, nil, ConfigurationUtil.SELECTOR_MULTIOPTION)
end

function AnimParts.prerequisitesPresent(specializations)
	return true
end;

function AnimParts.registerEventListeners(vehicleType)
	for _,n in pairs( { "onLoad", "onPostLoad", "onUpdate", "onUpdateTick", "onDelete", "onDraw" , "onRegisterActionEvents" } ) do
		SpecializationUtil.registerEventListener(vehicleType, n, AnimParts)
	end
end

function AnimParts:onRegisterActionEvents(isActiveForInput, isActiveForInputIgnoreSelection)
	if isActiveForInputIgnoreSelection then
		if self.ActionEvents == nil then 
			self.ActionEvents = {}
		else	
			self:clearActionEventsTable( self.ActionEvents )
		end 
		for _,actionName in pairs({ "DwerLewaya", "DwerPrawaya", "FortockaPrawaya", "FortockaLewaya" } ) do
			local _, eventName = self:addActionEvent(self.ActionEvents, InputAction[actionName], self, AnimParts.actionCallback, true, true, false, true, nil);
			if g_inputBinding ~= nil and g_inputBinding.events ~= nil and g_inputBinding.events[eventName] ~= nil then
				if isActiveForInputIgnoreSelection then
					g_inputBinding.events[eventName].displayPriority = 1
				elseif  isActiveForInputIgnoreSelection then
					g_inputBinding.events[eventName].displayPriority = 3
				end
				g_inputBinding.events[eventName].displayIsVisible = true
			end
		end
	end
end

function AnimParts:onLoad(savegame)
    self.playerCallbackKapot = AnimParts.playerCallbackKapot; 
    self.RotCamera = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.enterable.RotCamera#name"), "RotCamera")
    self.RotCamera1 = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.enterable.RotCamera1#name"), "RotCamera1")
	
    local configurations = Utils.getNoNil(self.configurations["tuning"], 1, i)
	
    local key = string.format("vehicle.tuningConfigurations.tuningConfiguration(%d)", self.configurations["tuning"]-1)
    ObjectChangeUtil.updateObjectChanges(self.xmlFile, "vehicle.tuningConfigurations.tuningConfiguration", self.configurations["tuning"], self.components, self)
    if not hasXMLProperty(self.xmlFile, key) then
        key = "vehicle.tuningConfigurations"
    end
    self.tuningI3d = {}
    local i = 1
    while true do
        local key = string.format("vehicle.tuningConfigurations.tuningConfiguration(%d)", self.configurations["tuning"]-i)
        if not hasXMLProperty(self.xmlFile, key) then
            break
        end
        local node = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, key .. ".tuning#node"), self.i3dMappings)
        if node ~= nil then
            local tuningXmlFilename = getXMLString(self.xmlFile, key .. ".tuning#filename")
            if tuningXmlFilename ~= nil then
                tuningXmlFilename = Utils.getFilename(tuningXmlFilename, self.baseDirectory)
                local tuningXmlFile = loadXMLFile("tuningI3dXML", tuningXmlFilename)
                if tuningXmlFile ~= nil and tuningXmlFile ~= 0 then
                    local i3dFilename = getXMLString(tuningXmlFile, "I3dDetal.filename")
                    if i3dFilename ~= nil then
                        local i3dNode = g_i3DManager:loadSharedI3DFile(i3dFilename, self.baseDirectory, false, false, false)
                        if i3dNode ~= nil and i3dNode ~= 0 then
                            local rootNode = I3DUtil.indexToObject(i3dNode, getXMLString(tuningXmlFile, "I3dDetal.Detal#node"))
                            if rootNode ~= nil then
                                link(node, rootNode)
                                setTranslation(rootNode, 0,0,0)
                                local tuning = {}
                                tuning.filename = i3dFilename
                                tuning.rootNode = rootNode
                                table.insert(self.tuningI3d, tuning)
                            end
                            delete(i3dNode)
                        end
                    end
                    delete(tuningXmlFile)
                end
            end
        end
        i = i + 1
    end
	
    self.LDver = {};
    self.LDver.name = getXMLString(self.xmlFile, key .. ".LDver#name")
    self.LDver.speedScale = Utils.getNoNil(getXMLFloat(self.xmlFile, key .. ".LDver#speedScale"), 1);
    self.PDver = {};
    self.PDver.name = getXMLString(self.xmlFile, key .. ".PDver#name")
    self.PDver.speedScale = Utils.getNoNil(getXMLFloat(self.xmlFile, key .. ".PDver#speedScale"), 1);
    self.LOkno = {};
    self.LOkno.name = getXMLString(self.xmlFile, key .. ".LOkno#name")
    self.LOkno.speedScale = Utils.getNoNil(getXMLFloat(self.xmlFile, key .. ".LOkno#speedScale"), 1);
    self.POkno = {};
    self.POkno.name = getXMLString(self.xmlFile, key .. ".POkno#name")
    self.POkno.speedScale = Utils.getNoNil(getXMLFloat(self.xmlFile, key .. ".POkno#speedScale"), 1);
	
	
    self.LDverNew = {};
    self.LDverNew.name = getXMLString(self.xmlFile, key .. ".LDverNew#name")
    self.LDverNew.speedScale = Utils.getNoNil(getXMLFloat(self.xmlFile, key .. ".LDverNew#speedScale"), 1);
    self.PDverNew = {};
    self.PDverNew.name = getXMLString(self.xmlFile, key .. ".PDverNew#name")
    self.PDverNew.speedScale = Utils.getNoNil(getXMLFloat(self.xmlFile, key .. ".PDverNew#speedScale"), 1);
    self.LOknoNew = {};
    self.LOknoNew.name = getXMLString(self.xmlFile, key .. ".LOknoNew#name")
    self.LOknoNew.speedScale = Utils.getNoNil(getXMLFloat(self.xmlFile, key .. ".LOknoNew#speedScale"), 1);
    self.POknoNew = {};
    self.POknoNew.name = getXMLString(self.xmlFile, key .. ".POknoNew#name")
    self.POknoNew.speedScale = Utils.getNoNil(getXMLFloat(self.xmlFile, key .. ".POknoNew#speedScale"), 1);
	
    self.Kapot = {};
	    local trigger = I3DUtil.indexToObject( self.components, getXMLString(self.xmlFile, string.format("vehicle.Triggers.Kapot#trigger")) );
        self.Kapot.name = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.AnimDoors.Kapot#name"))
        self.Kapot.speedScale = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.AnimDoors.Kapot#speedScale"), 1);
        if self.Kapot.name ~= nil then
            local speedScale = -self.Kapot.speedScale;
            local stopTime = 0;
            if self.Kapot.speedScale < 0 then
                stopTime = 1;
            end
            self:playAnimation(self.Kapot.name, speedScale, nil, true);
            self:setAnimationStopTime(self.Kapot.name, stopTime);
            AnimatedVehicle.updateAnimationByName(self, self.Kapot.name, 99999999);
	    if trigger == nil then
	    	print("[Error::Kapot] node is OK, but trigger could not be located. Check line "..tostring(i+1).." in your vehicle.xml");
	    end;
	    self.Kapot.trigger = trigger;
        self.Kapot.plIR = false
	    addTrigger( trigger, "playerCallbackKapot", self );
    end
    self.animationNodes = g_animationManager:loadAnimations(self.xmlFile, "vehicle.powerTakeOffs.animationNodes", self.components, self, self.i3dMappings)
end
function AnimParts:onPostLoad(savegame)
end

function AnimParts:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
    if self.isClient then

	if Input.isMouseButtonPressed(Input.MOUSE_BUTTON_MIDDLE)then
		if self.RotCamera ~= nil and self.playAnimation ~= nil then
            self:playAnimation(self.RotCamera, 1, nil, true);
		end;
	else
		if not self.RotCamera ~= nil and self.playAnimation ~= nil then
            self:playAnimation(self.RotCamera, -1, nil, true);
		end;
	end;
	if Input.isMouseButtonPressed(Input.MOUSE_BUTTON_RIGHT) and Input.isKeyPressed(Input.KEY_lshift)then
		if self.RotCamera1 ~= nil and self.playAnimation ~= nil then
            self:playAnimation(self.RotCamera1, 1, nil, true);
		end;
	else
		if not self.RotCamera1 ~= nil and self.playAnimation ~= nil then
            self:playAnimation(self.RotCamera1, -1, nil, true);
		end;
	end
	if self.inputRotLDwer then
        if self.LDver ~= nil and self.LDverNew ~= nil and self.playAnimation ~= nil then
            self:playAnimation(self.LDver.name, self.LDver.speedScale, self:getAnimationTime(self.LDver.name), true);
            self:playAnimation(self.LDverNew.name, self.LDverNew.speedScale, self:getAnimationTime(self.LDverNew.name), true);
        else
            self:playAnimation(self.LDver.name, -self.LDver.speedScale, self:getAnimationTime(self.LDver.name), true);
            self:playAnimation(self.LDverNew.name, -self.LDverNew.speedScale, self:getAnimationTime(self.LDverNew.name), true);
        end;
    end;
		
	if self.inputRotPDver then
        if self.PDver ~= nil and self.PDverNew ~= nil and self.playAnimation ~= nil then
        	self:playAnimation(self.PDver.name, self.PDver.speedScale, self:getAnimationTime(self.PDver.name), true);
        	self:playAnimation(self.PDverNew.name, self.PDverNew.speedScale, self:getAnimationTime(self.PDverNew.name), true);
    	else
        	self:playAnimation(self.PDver.name, -self.PDver.speedScale, self:getAnimationTime(self.PDver.name), true);
        	self:playAnimation(self.PDverNew.name, -self.PDverNew.speedScale, self:getAnimationTime(self.PDverNew.name), true);
    	end;
    end;
		
	if self.inputLOkno then
        if self.LOkno ~= nil and self.LOknoNew ~= nil and self.playAnimation ~= nil then
        	self:playAnimation(self.LOkno.name, self.LOkno.speedScale, self:getAnimationTime(self.LOkno.name), true);
        	self:playAnimation(self.LOknoNew.name, self.LOknoNew.speedScale, self:getAnimationTime(self.LOknoNew.name), true);
    	else
        	self:playAnimation(self.LOkno.name, -self.LOkno.speedScale, self:getAnimationTime(self.LOkno.name), true);
        	self:playAnimation(self.LOknoNew.name, -self.LOknoNew.speedScale, self:getAnimationTime(self.LOknoNew.name), true);
    	end;
    end;
		
	if self.inputPOkno then
        if self.POkno ~= nil and self.POknoNew ~= nil and self.playAnimation ~= nil then
        	self:playAnimation(self.POkno.name, self.POkno.speedScale, self:getAnimationTime(self.POkno.name), true);
        	self:playAnimation(self.POknoNew.name, self.POknoNew.speedScale, self:getAnimationTime(self.POknoNew.name), true);
    	else
        	self:playAnimation(self.POkno.name, -self.POkno.speedScale, self:getAnimationTime(self.POkno.name), true);
        	self:playAnimation(self.POknoNew.name, -self.POknoNew.speedScale, self:getAnimationTime(self.POknoNew.name), true);
    	end;
    end;
		
    if self.Kapot.plIR then
        g_currentMission:addExtraPrintText(g_i18n:getText("Input_Kapot_Close"), 2000)
        g_currentMission:addExtraPrintText(g_i18n:getText("Input_Kapot_Open"), 2000)
    end
	
	if self.Kapot.plIR and  Input.isMouseButtonPressed(Input.MOUSE_BUTTON_LEFT)then
        if self.Kapot ~= nil and self.playAnimation ~= nil then
            self:playAnimation(self.Kapot.name, self.Kapot.speedScale, self:getAnimationTime(self.Kapot.name), true);
        end;
    end;
	
    if self.Kapot.plIR and Input.isMouseButtonPressed(Input.MOUSE_BUTTON_RIGHT)then
        if self.Kapot ~= nil and self.playAnimation ~= nil then
            self:playAnimation(self.Kapot.name, -self.Kapot.speedScale, self:getAnimationTime(self.Kapot.name), true);
        end;
    end;
    end;
end;

function AnimParts:onUpdateTick(dt, isActiveForInput, isSelected)
end

function AnimParts:onDraw(isActiveForInput, isSelected)
end

function AnimParts:onDelete()
	if self.Kapot.trigger ~= nil then
		removeTrigger(self.Kapot.trigger);
	end;
    g_animationManager:deleteAnimations(self.animationNodes)
end;

function AnimParts:actionCallback(actionName, keyStatus, arg4, arg5, arg6)
    if self.isClient then
		if keyStatus > 0 then
			if actionName == 'DwerLewaya' then
			    self.inputRotLDwer = not self.inputRotLDwer
			elseif actionName == 'DwerPrawaya' then
			    self.inputRotPDver = not self.inputRotPDver
			elseif actionName == 'FortockaPrawaya' then
			    self.inputLOkno = not self.inputLOkno
			elseif actionName == 'FortockaLewaya' then
			    self.inputPOkno = not self.inputPOkno
			end
		end
    end
end

function AnimParts:playerCallbackKapot(triggerId, otherActorId, onEnter, onLeave, onStay, otherShapeId)
	if onEnter and g_currentMission.controlPlayer and g_currentMission.player ~= nil and otherActorId == g_currentMission.player.rootNode then
		self.Kapot.plIR = true;
	elseif onLeave and g_currentMission.player ~= nil and otherActorId == g_currentMission.player.rootNode then
		self.Kapot.plIR = false;
	end;
end;