--
-- 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)
    -- side doors
    self.sideDoors = {}
    self.numSideDoors = 0;
    while true do
        local key = "vehicle.sideDoors.sideDoor("..tostring(self.numSideDoors)..")";
        if not hasXMLProperty(self.xmlFile, key) then
            break;
        end;
        
        self.numSideDoors = self.numSideDoors + 1;
        local sideDoor = {}
        sideDoor.animName = getXMLString(self.xmlFile, key.."#animName");
        sideDoor.index = self.numSideDoors;
        sideDoor.vehicle = self;
        sideDoor.triggerId = Utils.indexToObject(self.components, getXMLString(self.xmlFile, key.."#triggerIndex"));
        sideDoor.playerInTrigger = false;
        addTrigger(sideDoor.triggerId, "triggerCallback", sideDoor);
        
        function sideDoor:open()
            sideDoor.vehicle:playAnimation(sideDoor.animName, 1, 0, false);
        end;
        function sideDoor:close()
            sideDoor.vehicle:playAnimation(sideDoor.animName, -1, 1, false);
        end;
        
        function sideDoor:getIsClosed()
            return sideDoor.vehicle:getAnimationTime(sideDoor.animName) == 0;
        end;
        function sideDoor:getIsOpened()
            return sideDoor.vehicle:getAnimationTime(sideDoor.animName) == 1;
        end;
        
        function sideDoor:triggerCallback(triggerId, otherId, onEnter, onLeave, onStay, otherShapeId)
            if g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
                if onEnter then
                    sideDoor.playerInTrigger = true;
                elseif onLeave then
                    sideDoor.playerInTrigger = false;
                end;
            end;
        end;
        
        self.sideDoors[sideDoor.index] = sideDoor;
        self.sideDoors[sideDoor.animName] = sideDoor;
    end;    
    self.sideDoorOpenSound = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.sideDoorOpen", nil, self.baseDirectory);
    self.sideDoorCloseSound = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.sideDoorClose", nil, self.baseDirectory);
    -- Kapot
    self.Kapot = {}
    self.numKapot = 0;
    while true do
        local key = "vehicle.Kapot.Kapot744("..tostring(self.numKapot)..")";
        if not hasXMLProperty(self.xmlFile, key) then
            break;
        end;
        
        self.numKapot = self.numKapot + 1;
        local Kapot744 = {}
        Kapot744.animName = getXMLString(self.xmlFile, key.."#animName");
        Kapot744.index = self.numKapot;
        Kapot744.vehicle = self;
        Kapot744.triggerId = Utils.indexToObject(self.components, getXMLString(self.xmlFile, key.."#triggerIndex"));
        Kapot744.playerInTrigger = false;
        addTrigger(Kapot744.triggerId, "triggerCallback", Kapot744);
        
        function Kapot744:open()
            Kapot744.vehicle:playAnimation(Kapot744.animName, 1, 0, false);
        end;
        function Kapot744:close()
            Kapot744.vehicle:playAnimation(Kapot744.animName, -1, 1, false);
        end;
        
        function Kapot744:getIsClosed()
            return Kapot744.vehicle:getAnimationTime(Kapot744.animName) == 0;
        end;
        function Kapot744:getIsOpened()
            return Kapot744.vehicle:getAnimationTime(Kapot744.animName) == 1;
        end;
        
        function Kapot744:triggerCallback(triggerId, otherId, onEnter, onLeave, onStay, otherShapeId)
            if g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
                if onEnter then
                    Kapot744.playerInTrigger = true;
                elseif onLeave then
                    Kapot744.playerInTrigger = false;
                end;
            end;
        end;
        
        self.Kapot[Kapot744.index] = Kapot744;
        self.Kapot[Kapot744.animName] = Kapot744;
    end;    
    self.Kapot744OpenSound = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.Kapot744Open", nil, self.baseDirectory);
    self.Kapot744CloseSound = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.Kapot744Close", nil, self.baseDirectory);
end;

function DoorOpener:delete()
    for a=1, table.getn(self.sideDoors) do
        removeTrigger(self.sideDoors[a].triggerId);
    end;
    SoundUtil.deleteSample(self.sideDoorOpenSound);
    SoundUtil.deleteSample(self.sideDoorCloseSound);
    for a=1, table.getn(self.Kapot) do
        removeTrigger(self.Kapot[a].triggerId);
    end;
    SoundUtil.deleteSample(self.Kapot744OpenSound);
    SoundUtil.deleteSample(self.Kapot744CloseSound);
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 not (g_gui.currentGui ~= nil or g_currentMission.isPlayerFrozen) then
        -- side door control
        for a=1, self.numSideDoors do
            local sideDoor = self.sideDoors[a];
            if sideDoor.playerInTrigger then
                if g_currentMission.player.isEntered then
                    if sideDoor:getIsClosed() then
                        g_currentMission:addHelpButtonText(g_i18n:getText("LEFTDOOROPENER"), InputBinding.SET_LEFTDOOROPENER);
                        if InputBinding.hasEvent(InputBinding.SET_LEFTDOOROPENER) then
                            sideDoor:open();
                            SoundUtil.playSample(self.sideDoorOpenSound, 1, 0);
                        end;
                    elseif sideDoor:getIsOpened() then
                        if (sideDoor.animName == "leftDoor" ) or sideDoor.animName ~= "leftDoor" then
                            g_currentMission:addHelpButtonText(g_i18n:getText("LEFTDOOROPENER"), InputBinding.SET_LEFTDOOROPENER);
                            if InputBinding.hasEvent(InputBinding.SET_LEFTDOOROPENER) then
                                sideDoor:close();
                                SoundUtil.playSample(self.sideDoorCloseSound, 1, 0);
                            end;
                        end;
                    end;
                end;
            end;
        end;
        -- Kapot
        for a=1, self.numKapot do
            local Kapot744 = self.Kapot[a];
            if Kapot744.playerInTrigger then
                if g_currentMission.player.isEntered then
                    if Kapot744:getIsClosed() then
                        g_currentMission:addHelpButtonText(g_i18n:getText("KAPOTDOOROPENER"), InputBinding.SET_KAPOTDOOROPENER);
                        if InputBinding.hasEvent(InputBinding.SET_KAPOTDOOROPENER) then
                            Kapot744:open();
                            SoundUtil.playSample(self.Kapot744OpenSound, 1, 0);
                        end;
                    elseif Kapot744:getIsOpened() then
                        if (Kapot744.animName == "kapot" ) or Kapot744.animName ~= "kapot" then
                            g_currentMission:addHelpButtonText(g_i18n:getText("KAPOTDOOROPENER"), InputBinding.SET_KAPOTDOOROPENER);
                            if InputBinding.hasEvent(InputBinding.SET_KAPOTDOOROPENER) then
                                Kapot744:close();
                                SoundUtil.playSample(self.Kapot744CloseSound, 1, 0);
                            end;
                        end;
                    end;
                end;
            end;
        end;
    end;
end;

function DoorOpener:onLeave()
end;

function DoorOpener:draw()
end;