--
-- DoorOpener
--
-- written by fruktor, visit: www.eifok-team.de
-- Edit by }I{EKA special for MTZ-1025

DoorOpener = {};

DoorOpener.modDir = g_currentModDirectory;

function DoorOpener.prerequisitesPresent(specializations)
    return true; 
end;

function DoorOpener:load(savegame)
--  left door
    self.playerCallbackLeftDoor = SpecializationUtil.callSpecializationsFunction("playerCallbackLeftDoor"); 
    
    self.doorO = {};
    
    local trigger = Utils.indexToObject( self.components, getXMLString(self.xmlFile, string.format("vehicle.leftDoorOpener#trigger")) );
    if trigger == nil then
        print("[Error::LeftDoorOpener] node is OK, but trigger could not be located. Check line "..tostring(i+1).." in your vehicle.xml");
    end;
    
    local icIndex = getXMLInt(self.xmlFile, string.format("vehicle.leftDoorOpener#icIndex", i));
    
    self.doorO.icIndex = icIndex;
    self.doorO.trigger = trigger;
    self.doorO.plIR = false;
    
    addTrigger( trigger, "playerCallbackLeftDoor", self );
    
--  right door
    self.playerCallbackRightDoor = SpecializationUtil.callSpecializationsFunction("playerCallbackRightDoor"); 
    
    self.door1 = {};
    
    local trigger = Utils.indexToObject( self.components, getXMLString(self.xmlFile, string.format("vehicle.rightDoorOpener#trigger")) );
    if trigger == nil then
        print("[Error::RightDoorOpener] node is OK, but trigger could not be located. Check line "..tostring(i+1).." in your vehicle.xml");
    end;
    
    local icIndex = getXMLInt(self.xmlFile, string.format("vehicle.rightDoorOpener#icIndex", i));
    
    self.door1.icIndex = icIndex;
    self.door1.trigger = trigger;
    self.door1.plIR = false;
    
    addTrigger( trigger, "playerCallbackRightDoor", self );
    
--    rightN
    self.playerCallbackRightN = SpecializationUtil.callSpecializationsFunction("playerCallbackRightN"); 
    
    self.door2 = {};
    
    local trigger = Utils.indexToObject( self.components, getXMLString(self.xmlFile, string.format("vehicle.rightNOpener#trigger")) );
    if trigger == nil then
        print("[Error::RightNOpener] node is OK, but trigger could not be located. Check line "..tostring(i+1).." in your vehicle.xml");
    end;
    
    local icIndex = getXMLInt(self.xmlFile, string.format("vehicle.rightNOpener#icIndex", i));
    
    self.door2.icIndex = icIndex;
    self.door2.trigger = trigger;
    self.door2.plIR = false;
    
    addTrigger( trigger, "playerCallbackRightN", self );    
    
--    leftN
    self.playerCallbackLeftN = SpecializationUtil.callSpecializationsFunction("playerCallbackLeftN"); 
    
    self.door3 = {};
    
    local trigger = Utils.indexToObject( self.components, getXMLString(self.xmlFile, string.format("vehicle.leftNOpener#trigger")) );
    if trigger == nil then
        print("[Error::LeftNOpener] node is OK, but trigger could not be located. Check line "..tostring(i+1).." in your vehicle.xml");
    end;
    
    local icIndex = getXMLInt(self.xmlFile, string.format("vehicle.leftNOpener#icIndex", i));
    
    self.door3.icIndex = icIndex;
    self.door3.trigger = trigger;
    self.door3.plIR = false;
    
    addTrigger( trigger, "playerCallbackLeftN", self );        
end;

function DoorOpener:delete()
-- left door
    if self.doorO.trigger ~= nil then
        removeTrigger(self.doorO.trigger);
    end;
--  right door
    if self.door1.trigger ~= nil then
        removeTrigger(self.door1.trigger);
    end;
--  RightN
    if self.door2.trigger ~= nil then
        removeTrigger(self.door2.trigger);
    end;    
--  LeftN
    if self.door3.trigger ~= nil then
        removeTrigger(self.door3.trigger);
    end;
end;

function DoorOpener:readStream(streamId, connection)
end;

function DoorOpener:writeStream(streamId, connection)
end;

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

function DoorOpener:keyEvent(unicode, sym, modifier, isDown)
end;

function DoorOpener:update(dt)
    if self.doorO.plIR then
        g_currentMission:addHelpButtonText( g_i18n:getText("SET_LEFTDOOROPENER"), InputBinding.SET_DOOROPENER );    
        setTextColor(1.0, 1.0, 1.0, 1.0);
        setTextAlignment(RenderText.ALIGN_LEFT);                 
        renderText( 0.5, 0.06, 0.02, g_i18n:getText("SET_LEFTDOOROPENER") );
        if InputBinding.hasEvent(InputBinding.SET_DOOROPENER)then
            local obj = self.interactiveObjects[self.doorO.icIndex];
            if obj ~= nil then
                self:doActionOnObject(self.doorO.icIndex);
            end;
        end
    end;
    if self.door1.plIR then
        g_currentMission:addHelpButtonText( g_i18n:getText("SET_RIGHTDOOROPENER"), InputBinding.SET_DOOROPENER );    
        setTextColor(1.0, 1.0, 1.0, 1.0);
        setTextAlignment(RenderText.ALIGN_LEFT);                 
        renderText( 0.5, 0.06, 0.02, g_i18n:getText("SET_RIGHTDOOROPENER") );
        if InputBinding.hasEvent(InputBinding.SET_DOOROPENER)then
            local obj = self.interactiveObjects[self.door1.icIndex];
            if obj ~= nil then
                self:doActionOnObject(self.door1.icIndex);
            end;
        end
    end;
    if self.door2.plIR then
        g_currentMission:addHelpButtonText( g_i18n:getText("SET_RIGHTNOPENER"), InputBinding.SET_DOOROPENER );    
        setTextColor(1.0, 1.0, 1.0, 1.0);
        setTextAlignment(RenderText.ALIGN_LEFT);                 
        renderText( 0.5, 0.06, 0.02, g_i18n:getText("SET_RIGHTNOPENER") );
        if InputBinding.hasEvent(InputBinding.SET_DOOROPENER)then
            local obj = self.interactiveObjects[self.door2.icIndex];
            if obj ~= nil then
                self:doActionOnObject(self.door2.icIndex);
            end;
        end
    end;
    if self.door3.plIR then
        g_currentMission:addHelpButtonText( g_i18n:getText("SET_LEFTNOPENER"), InputBinding.SET_DOOROPENER );    
        setTextColor(1.0, 1.0, 1.0, 1.0);
        setTextAlignment(RenderText.ALIGN_LEFT);                 
        renderText( 0.5, 0.06, 0.02, g_i18n:getText("SET_LEFTNOPENER") );
        if InputBinding.hasEvent(InputBinding.SET_DOOROPENER)then
            local obj = self.interactiveObjects[self.door3.icIndex];
            if obj ~= nil then
                self:doActionOnObject(self.door3.icIndex);
            end;
        end
    end;        
end;

function DoorOpener:onLeave()
end;

function DoorOpener:draw()
end;

function DoorOpener:playerCallbackLeftDoor(triggerId, otherId, onEnter, onLeave, onStay)
    if onEnter and g_currentMission.controlPlayer and g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
        self.doorO.plIR = true;
    elseif onLeave and g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
        self.doorO.plIR = false;
    end;
end;

function DoorOpener:playerCallbackRightDoor(triggerId, otherId, onEnter, onLeave, onStay)
    if onEnter and g_currentMission.controlPlayer and g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
        self.door1.plIR = true;
    elseif onLeave and g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
        self.door1.plIR = false;
    end;
end;

function DoorOpener:playerCallbackRightN(triggerId, otherId, onEnter, onLeave, onStay)
    if onEnter and g_currentMission.controlPlayer and g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
        self.door2.plIR = true;
    elseif onLeave and g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
        self.door2.plIR = false;
    end;
end;

function DoorOpener:playerCallbackLeftN(triggerId, otherId, onEnter, onLeave, onStay)
    if onEnter and g_currentMission.controlPlayer and g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
        self.door3.plIR = true;
    elseif onLeave and g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
        self.door3.plIR = false;
    end;
end;


