--[[
CoverFix

Specialization to allow use 'openOnBuy' for an cover, GIANTS has not finished their job there!

Author:		Ifko[nator]
Datum:		04.01.2020

Version:	v1.0

History:	v1.0 @ 04.01.2020 - initial implementation in FS 19
			---------------------------------------------------
]]

CoverFix = {};

function CoverFix.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Cover, specializations); 
end;

function CoverFix.registerEventListeners(vehicleType)
	local functionNames = {
		"onLoad",
		"onUpdate"
	};

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

function CoverFix:onLoad(savegame)
	self.spec_cover.hasFinishedFirstLoad = false;
end;

function CoverFix:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
	if not self.spec_cover.hasFinishedFirstLoad then
		for coverIndex, cover in pairs(self.spec_cover.covers) do
			if cover.startOpenState then
				self:setCoverState(coverIndex);
				AnimatedVehicle.updateAnimationByName(self, cover.openAnimation, 9999999);
			end;
		end;

		self.spec_cover.hasFinishedFirstLoad = true;
	end;
end;