--
-- smerovky
-- 
-- 
-- 
--
-- @author  DJ Lukes
-- @date  9/12/09
-- @version 1.0 CZ
--
--
--

smerovky = {};

function smerovky.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Attachable, specializations);
end;

function smerovky:load(xmlFile)

    -- Revers lights
    self.reverseLightObject = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.utilLights.reverse#index"));
    setVisibility(self.reverseLightObject, false);
    self.reverseLightActive = false;
    
    -- Brake lights
    self.brakeLightObject = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.utilLights.brake#index"));
    setVisibility(self.brakeLightObject, false);
    self.brakeLightActive = false;
    
    -- Turn lights
    self.leftTurnObject = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.utilLights.leftTurn#index"))
    setVisibility(self.leftTurnObject, false);
    self.leftTurnActive = false;
    self.flashLeft = false;
    
    self.rightTurnObject = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.utilLights.rightTurn#index"))
    setVisibility(self.rightTurnObject, false);
    self.rightTurnActive = false;
    self.flashRight = false;
    
    self.flash = false;
    self.flashWarn = false;
    self.timer = 10;
end;

function smerovky:delete()
end;

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

function smerovky:keyEvent(unicode, sym, modifier, isDown)
    -- Turn signals
    if InputBinding.isPressed(InputBinding.LEFTTURN) then
        if not self.flashLeft or self.flashWarn then
            self.flashLeft = false;
        end;
        self.flashLeft = not self.flashLeft;
        self.flashRight = false;
        self.flashWarn = false;
        self.timer = 10;
    end;
    if InputBinding.isPressed(InputBinding.RIGHTTURN) then
        if not self.flashRight or self.flashWarn then
            self.flashRight = false;
        end;
        self.flashRight = not self.flashRight;
        self.flashLeft = false;
        self.flashWarn = false;
        self.timer = 10;
    end;
    -- Warn lights
    if InputBinding.isPressed(InputBinding.WARNLIGHTS) then
        if self.flashWarn then
            self.flashLeft = false;
            self.flashRight = false;
        else
            self.rightTurnActive = false;
            self.leftTurnActive = false;
            self.flashLeft = true;
            self.flashRight = true;
        end;
        self.flashWarn = not self.flashWarn;
        self.timer = 10;
    end;
end;

function smerovky:update(dt)
    -- Reverse lights
    if self.movingDirection < 0 then
        self.reverseLightActive = true;
    else
        self.reverseLightActive = false;
    end;
    setVisibility(self.reverseLightObject, self.reverseLightActive);
    
    -- Define input
    self.input = InputBinding.getAnalogInputAxis(InputBinding.AXIS_FORWARD);
    if InputBinding.isAxisZero(self.input) then
        self.input = InputBinding.getDigitalInputAxis(InputBinding.AXIS_FORWARD);
    end;
    
    -- Brake lights
    setVisibility(self.brakeLightObject, self.brakeLightActive);
    
    if (self.movingDirection*self.lastSpeed*(-self.input)) < -0.001 then
        self.brakeLightActive = true;
    else
        self.brakeLightActive = false;
    end;
    
	-- Turn lights
    if self.flashLeft or self.flashRight then
        self.timer = self.timer - dt;
        if self.flashRight or self.flashWarn then
            if self.timer < 0 then
                self.rightTurnActive = not self.rightTurnActive;
            end;
        else
            self.rightTurnActive = false;
        end;
    
        if self.flashLeft or self.flashWarn then
            if self.timer < 0 then
                self.leftTurnActive = not self.leftTurnActive;
            end;
        else
            self.leftTurnActive = false;
        end;
        if self.timer < 0 then
            self.timer = 500;
        end;
    else
        self.rightTurnActive = false;
        self.leftTurnActive = false;
    end;
    setVisibility(self.rightTurnObject, self.rightTurnActive);
    setVisibility(self.leftTurnObject, self.leftTurnActive);
end;

function smerovky:draw()

end;

