--
-- Specialization Acros
--
-- @author  	Aluha74rus
-- @version 	v1.0
-- @date  		23/05/19

Acros = {};
local Acros_directory = g_currentModDirectory;

function Acros.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Combine, specializations) and SpecializationUtil.hasSpecialization(Pipe, specializations);
end;

function Acros.registerEventListeners(vehicleType)	
	SpecializationUtil.registerEventListener(vehicleType, "onLoad", Acros);
	SpecializationUtil.registerEventListener(vehicleType, "onUpdate", Acros)
	SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", Acros)
	SpecializationUtil.registerEventListener(vehicleType, "onDelete", Acros)
end

function Acros.initSpecialization()
   g_configurationManager:addConfigurationType("flags", g_i18n:getText("configuration_flags"), nil, nil, nil, nil, ConfigurationUtil.SELECTOR_MULTIOPTION)
end

function Acros:onLoad(savegame)
	
    local configurations = Utils.getNoNil(self.configurations["flags"], 1, i)
	
    local key = string.format("vehicle.flagsConfigurations.flagsConfiguration(%d)", self.configurations["flags"]-1)
    ObjectChangeUtil.updateObjectChanges(self.xmlFile, "vehicle.flagsConfigurations.flagsConfiguration", self.configurations["flags"], self.components, self)
	
	if self.isClient then
	    self.sampleWarningGST = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.Acros.sounds", "WarningGST", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleWarningBunker = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.Acros.sounds", "WarningBunker", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleWarningBunkerBip = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.Acros.sounds", "WarningBunkerBip", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleWarningShnek = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.Acros.sounds", "WarningShnek", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
        self.wsCleaningTimer = 1000
        self.wsotCleaningTime = 12000
	end
    self.KlawishaMassa =  Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.motorized.KlawishaMassa#name"), "KlawishaMassa")
    self.KlawishaStart =  Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.motorized.KlawishaStart#name"), "KlawishaStart")
	
    local klawishaPoworotNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.enterable.klawishaPoworot#node"));
    if klawishaPoworotNode ~= nil then
        self.klawishaPoworot = {};
        self.klawishaPoworot.node = klawishaPoworotNode;
        local x, y, z = StringUtil.getVectorFromString(getXMLString(self.xmlFile, "vehicle.enterable.klawishaPoworot#minRot"));
        self.klawishaPoworot.minRot = {};
        self.klawishaPoworot.minRot[1] = MathUtil.degToRad(Utils.getNoNil(x, 0));
        self.klawishaPoworot.minRot[2] = MathUtil.degToRad(Utils.getNoNil(y, 0));
        self.klawishaPoworot.minRot[3] = MathUtil.degToRad(Utils.getNoNil(z, 0));

        local x, y, z = StringUtil.getVectorFromString(getXMLString(self.xmlFile, "vehicle.enterable.klawishaPoworot#rotLeft"));
        self.klawishaPoworot.leftRot = {};
        self.klawishaPoworot.leftRot[1] = MathUtil.degToRad(Utils.getNoNil(x, 0));
        self.klawishaPoworot.leftRot[2] = MathUtil.degToRad(Utils.getNoNil(y, 0));
        self.klawishaPoworot.leftRot[3] = MathUtil.degToRad(Utils.getNoNil(z, 0));

        local x, y, z = StringUtil.getVectorFromString(getXMLString(self.xmlFile, "vehicle.enterable.klawishaPoworot#rotRight"));
        self.klawishaPoworot.rightRot = {};
        self.klawishaPoworot.rightRot[1] = MathUtil.degToRad(Utils.getNoNil(x, 0));
        self.klawishaPoworot.rightRot[2] = MathUtil.degToRad(Utils.getNoNil(y, 0));
        self.klawishaPoworot.rightRot[3] = MathUtil.degToRad(Utils.getNoNil(z, 0));

        self.klawishaPoworot.rotTime = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.enterable.klawishaPoworot#rotTime"), 1)*1000;
		self.klawishaPoworot.lastDir = 1;
    end;
    self.KlawishaSvet =  Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.enterable.KlawishaSvet#name"), "KlawishaSvet")
    self.KlawishaSvetWork =  Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.enterable.KlawishaSvetWork#name"), "KlawishaSvetWork") 

	
end
function Acros:onDelete()
    if self.isClient then
		g_soundManager.deleteSample(self.sampleWarningGST);
		g_soundManager.deleteSample(self.sampleWarningBunker);
		g_soundManager.deleteSample(self.sampleWarningBunkerBip);
		g_soundManager.deleteSample(self.sampleWarningShnek);
    end
end
function Acros:onUpdate(dt)
	
    local WarningGSTEnableTimeStartDelay = 17800;
	local WarningGSTEnableTime = (self.spec_motorized.motorStartTime - self.spec_motorized.motorStartDuration) + WarningGSTEnableTimeStartDelay;
	
    local KlawishaMassaEnableTimeStartDelay = 500;
	local KlawishaMassaEnableTime = (self.spec_motorized.motorStartTime - self.spec_motorized.motorStartDuration) + KlawishaMassaEnableTimeStartDelay;
	
    local KlawishaStartEnableTimeStartDelay = 1500;
	local KlawishaStartEnableTime = (self.spec_motorized.motorStartTime - self.spec_motorized.motorStartDuration) + KlawishaStartEnableTimeStartDelay;
	local Duration = 2600;
	local DisableTime = KlawishaStartEnableTime + Duration;
	
    if self.isClient then
		if self.KlawishaMassa ~= nil and self.playAnimation ~= nil then
		    if (g_currentMission.time <= KlawishaMassaEnableTime  and self.spec_motorized.isMotorStarted) then
                self:playAnimation(self.KlawishaMassa, 1, nil, true);
		    else
                self:playAnimation(self.KlawishaMassa, -1, nil, true);
		    end;
		end;	
		if self.KlawishaStart ~= nil and self.playAnimation ~= nil then
		    if (g_currentMission.time <= DisableTime and g_currentMission.time >= KlawishaStartEnableTime  and self.spec_motorized.isMotorStarted) then
                self:playAnimation(self.KlawishaStart, 1, nil, true);
		    else
                self:playAnimation(self.KlawishaStart, -1, nil, true);
		    end;
		end;
        local speed = self.lastSpeedReal*3600;
	    if (g_currentMission.time <= WarningGSTEnableTime  and speed <= 22 and self.spec_motorized.isMotorStarted) then
	        g_soundManager:playSample(self.sampleWarningGST,1.5*dt);
        end;
	    if self:getFillUnitFillLevel(self.spec_combine.fillUnitIndex) < (self:getFillUnitCapacity(self.spec_combine.fillUnitIndex) * 0.95) then
	        g_soundManager:playSample(self.sampleWarningBunker,1.5*dt);
	    end;
	    if self:getFillUnitFillLevel(self.spec_combine.fillUnitIndex) < (self:getFillUnitCapacity(self.spec_combine.fillUnitIndex) * 0.50) then
	        g_soundManager:playSample(self.sampleWarningBunkerBip,1.5*dt);
	    end;
	end;
end;
function Acros:onUpdateTick(dt)
    if self.isClient then
        local Warningspeed = self.lastSpeedReal*3600;
        if self.wsCleaningTimer <= g_currentMission.time and self.spec_pipe.targetState == 2 and Warningspeed <= 22 then
            g_soundManager:playSample(self.sampleWarningShnek,1.5*dt);
            self.wsCleaningTimer = g_currentMission.time + self.wsotCleaningTime
        end
	end;
    if self:getIsActive() then
        if self.spec_lights.turnLightState ~= nil then
			if self.klawishaPoworot ~= nil then
				local dir = 0;
				local hasRotChanged = false;
				local destRot = self.klawishaPoworot.minRot;
				
				if self.spec_lights.turnLightState == 1 then
					destRot = self.klawishaPoworot.leftRot;
					dir = 1;
				elseif self.spec_lights.turnLightState == 2 then
					destRot = self.klawishaPoworot.rightRot;
					dir = -1;
				end
				if self.klawishaPoworot.lastDir ~= dir then
					self.klawishaPoworot.lastDir = dir;
					self.klawishaPoworot.rotatingTime = 0;
				end
				
				if self.klawishaPoworot.rotatingTime ~= nil then
					self.klawishaPoworot.rotatingTime = self.klawishaPoworot.rotatingTime + dt;
					local t = math.min(1.0, self.klawishaPoworot.rotatingTime / self.klawishaPoworot.rotTime);
					if t == 1 then
						self.klawishaPoworot.rotatingTime = nil;
					end
					local rot = {getRotation(self.klawishaPoworot.node)};
					
					local rx1,ry1,rz1,rw1 = mathEulerToQuaternion(unpack(rot));
					local rx2,ry2,rz2,rw2 = mathEulerToQuaternion(unpack(destRot));
					
					local qx,qy,qz,qw = MathUtil.nlerpQuaternionShortestPath(rx1,ry1,rz1,rw1, rx2,ry2,rz2,rw2, t);
					setQuaternion(self.klawishaPoworot.node, qx,qy,qz,qw);
				end
			end;
		end;
        if lightsTypesMask ~= self.spec_lights.lightsTypesMask or force then
            if (self.spec_lights.lightsTypesMask == 1)then
                self:playAnimation(self.KlawishaSvet, 1, nil, true);
            else
                self:playAnimation(self.KlawishaSvet, -1, nil, true);
            end;
            if (self.spec_lights.lightsTypesMask > 2)then
                self:playAnimation(self.KlawishaSvet, 1, nil, true);
                self:playAnimation(self.KlawishaSvetWork, 1, nil, true);
            else
                self:playAnimation(self.KlawishaSvetWork, -1, nil, true);
            end;
	    end;
	end;
end