--
-- InteractiveWindows with Save!
-- Specialization for InteractiveWindows
--

InteractiveWindows = {};

function InteractiveWindows.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(InteractiveControl, specializations) and SpecializationUtil.hasSpecialization(AnimatedVehicle, specializations);
end;

function InteractiveWindows:load(xmlFile)

    local i=0;
    while true do
        local windowName = string.format("vehicle.interactiveComponents.windows.window(%d)", i);
        if not hasXMLProperty(xmlFile, windowName) then
            break;
        end;
        local animation = getXMLString(xmlFile, windowName .. "#animName");
        local name = Utils.getNoNil(g_i18n:getText(getXMLString(xmlFile, windowName .. "#name")), "ERROR");
        local mark = Utils.indexToObject(self.components, getXMLString(xmlFile, windowName .. "#mark"));
        local highlight = getChildAt(mark, 0);
        local size = Utils.getNoNil(getXMLFloat(xmlFile, windowName .. "#size"), 0.1);
        local onMessage = g_i18n:getText(Utils.getNoNil(getXMLString(xmlFile, windowName .. "#onMessage"), "ic_button_on"));
        local offMessage =  g_i18n:getText(Utils.getNoNil(getXMLString(xmlFile, windowName .. "#offMessage") , "ic_button_off"));
        local window = Window:new(highlight, name, animation, mark, size, self, onMessage, offMessage, self.infoBar);
        window.synch = Utils.getNoNil(getXMLBool(xmlFile, windowName .. "#synch"), true);
        table.insert(self.interactiveObjects, window);
        i = i + 1;
    end;
end;

function InteractiveWindows:delete()
end;

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

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

function InteractiveWindows:update(dt)
end;

function InteractiveWindows:draw()
end;

Window = {};

function Window:new(highlight, name, animation, mark, size, vehicle, onMessage, offMessage, infobar)

    local Window_mt = Class(Window, InteractiveComponentInterface);
    local instance = InteractiveComponentInterface:new(nil, highlight, name, mark, size, onMessage, offMessage, infobar, Window_mt);
    instance.vehicle = vehicle;
    instance.animation = animation;

    return instance;
end;

function Window:delete()
    InteractiveComponentInterface.delete(self);
end;

function Window:mouseEvent(posX, posY, isDown, isUp, button)
    InteractiveComponentInterface.mouseEvent(self, posX, posY, isDown, isUp, button);
end;

function Window:keyEvent(unicode, sym, modifier, isDown)
    InteractiveComponentInterface.keyEvent(self, unicode, sym, modifier, isDown);
end;

function Window:update(dt)
    InteractiveComponentInterface.update(self, dt);
end;

function Window:draw()
    InteractiveComponentInterface.draw(self);
end;

function Window:doAction(noEventSend, forceAction)
    InteractiveComponentInterface.doAction(self, forceAction);
    local dir = 1;
    if not self.isOpen  then
        dir = -1;
    end;
    self.vehicle:playAnimation(self.animation, dir, Utils.clamp(self.vehicle:getAnimationTime(self.animation), 0, 1), true);
end;

function Window:onEnter()
    InteractiveComponentInterface.onEnter(self);
end;

function Window:onExit()
    InteractiveComponentInterface.onExit(self);
end;

function Window:setActive()
    InteractiveComponentInterface.setActive(self, isActive);
end;

function Window:setVisible(isVisible)
    InteractiveComponentInterface.setVisible(self, isVisible);
end;