--[[
	ChangeEnterText
	Specialization text to enter a vehicle
	
	@author:  	Ifko[nator]
	@date:  	21.08.2019
    @version:	2.0
    
    @history:   v1.0 @15.03.2019 - initial implemention in FS 19
                v2.0 @21.08.2019 - added support to change start and stop fill text for an vehicle with an fillTriggerVehicle
]]

ChangeEnterText = {};

function ChangeEnterText.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Enterable, specializations);
end;

function ChangeEnterText.registerEventListeners(vehicleType)
	local functionNames = {
        "onLoad"
	};

	for _, functionName in ipairs(functionNames) do
		SpecializationUtil.registerEventListener(vehicleType, functionName, ChangeEnterText);
	end;
end;

function ChangeEnterText:onLoad(savegame) 
    local specEnterable = self.spec_enterable;
     
    specEnterable.text = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.enterable#enterText"), "button_enterVehicle");

    Player.updateActionEvents = Utils.appendedFunction(Player.updateActionEvents, ChangeEnterText.updateActionEvents);

    ObjectIds.objectIdToClass[14].update = Utils.overwrittenFunction(ObjectIds.objectIdToClass[14].update, ChangeEnterText.updateFix);
    ObjectIds.objectIdToClass[14].startLoading = Utils.overwrittenFunction(ObjectIds.objectIdToClass[14].startLoading, ChangeEnterText.startLoadingFix);
    ObjectIds.objectIdToClass[14].stopLoading = Utils.overwrittenFunction(ObjectIds.objectIdToClass[14].stopLoading, ChangeEnterText.stopLoadingFix);
end;

function ChangeEnterText.startLoadingFix(self, superFunc, fillType, fillableObject, fillUnitIndex)
    if not self.isLoading then
        self:raiseActive();
        self.isLoading = true;
        self.selectedFillType = fillType;
        self.currentFillableObject = fillableObject;
        self.fillUnitIndex = fillUnitIndex;

        self.objectXML = fillableObject.xmlFile;

        local activateText = Utils.getNoNil(getXMLString(self.objectXML, "vehicle.fillTriggerVehicle#stopFillText"), "action_siloStopFilling");

        if string.sub(activateText, 1, 6) == "$l10n_" then
            activateText = g_i18n:getText(string.sub(activateText, 7));
        elseif g_i18n:hasText(activateText) then
            activateText = g_i18n:getText(activateText);
        end;

        self.activateText = activateText;

        if self.isClient then
            g_effectManager:setFillType(self.effects, self.selectedFillType);
            g_effectManager:startEffects(self.effects);
            g_soundManager:playSample(self.samples.load);

            if self.scroller ~= nil then
                setShaderParameter(self.scroller, self.scrollerShaderParameterName, self.scrollerSpeedX, self.scrollerSpeedY, 0, 0, false);
            end;
        end;
    end;
end;

function ChangeEnterText.stopLoadingFix(self, superFunc)
    if self.isLoading then
        self:raiseActive();

        self.isLoading = false;
        self.selectedFillType = FillType.UNKNOWN;

        local activateText = Utils.getNoNil(getXMLString(self.objectXML, "vehicle.fillTriggerVehicle#startFillText"), "action_siloStartFilling");

        if string.sub(activateText, 1, 6) == "$l10n_" then
            activateText = g_i18n:getText(string.sub(activateText, 7));
        elseif g_i18n:hasText(activateText) then
            activateText = g_i18n:getText(activateText);
        end;

        self.activateText = activateText;
        self.objectXML = nil;
        
        for i, fillableObject in pairs(self.fillableObjects) do
            fillableObject.lastWasFilled = fillableObject.object == self.validFillableObject;
        end;
        
        if self.isClient then
            g_effectManager:stopEffects(self.effects);
            g_soundManager:stopSample(self.samples.load);

            if self.scroller ~= nil then
                setShaderParameter(self.scroller, self.scrollerShaderParameterName, 0, 0, 0, 0, false);
            end;
        end;
    end;
end;

function ChangeEnterText.updateFix(self, superFunc, dt)
    if self.isServer then
        if self.validFillableObject ~= nil then
            if self.firstRun == nil then    
                local activateText = Utils.getNoNil(getXMLString(self.validFillableObject.xmlFile, "vehicle.fillTriggerVehicle#startFillText"), "action_siloStartFilling");

                if string.sub(activateText, 1, 6) == "$l10n_" then
                    activateText = g_i18n:getText(string.sub(activateText, 7));
                elseif g_i18n:hasText(activateText) then
                    activateText = g_i18n:getText(activateText);
                end;

                self.activateText = activateText;

                self.firstRun = true;
            end;
        end;
        
        if self.isLoading then
            if self.currentFillableObject ~= nil then
                local fillDelta = self.source:addFillLevelToFillableObject(self.currentFillableObject, self.fillUnitIndex, self.selectedFillType, self.fillLitersPerMS*dt, self.dischargeInfo, ToolType.TRIGGER);

                if fillDelta == nil or fillDelta < 0.001 then
                    self:setIsLoading(false);
                end;
            elseif self.isLoading then
                self:setIsLoading(false);
            end;
        end;

        self:raiseActive();
    end;
end;

function ChangeEnterText:updateActionEvents()
    self.canEnterVehicle = g_currentMission.interactiveVehicleInRange and g_currentMission.interactiveVehicleInRange:getIsEnterable();
	
	local vehicleIsRideable = self.canEnterVehicle and SpecializationUtil.hasSpecialization(Rideable, g_currentMission.interactiveVehicleInRange.specializations);
	local vehicleNeedChangeEnterText = self.canEnterVehicle and SpecializationUtil.hasSpecialization(ChangeEnterText, g_currentMission.interactiveVehicleInRange.specializations);
	local eventIdEnter = self.inputInformation.registrationList[InputAction.ENTER].eventId;
	
    if self.canEnterVehicle and not vehicleIsRideable then
		if vehicleNeedChangeEnterText then
			local specEnterable = g_currentMission.interactiveVehicleInRange.spec_enterable;
			local text = specEnterable.text;

			if text:sub(1, 6) == "$l10n_" then
				text = g_i18n:getText(text:sub(7));
			elseif g_i18n:hasText(text) then
				text = g_i18n:getText(text);
			end;

			g_inputBinding:setActionEventText(eventIdEnter, text);
		else
			g_inputBinding:setActionEventText(eventIdEnter, g_i18n:getText("button_enterVehicle"))
        end;
    end;
end;