@startuml

title Plugin Service

participant "App" as app
participant "PluginMgr" as pluginMgr
participant "DisplayMgr" as displayMgr
participant "Plugin" as plugin
participant "TopicHandlerService" as topicHandlerService

autoactivate on

note over app,topicHandlerService
    Install plugin
end note

app -> pluginMgr: Install concrete plugin.
pluginMgr -> plugin: Create concrete plugin.
plugin --> pluginMgr: Concrete plugin
pluginMgr -> displayMgr: Install plugin in display slot.

alt Empty slot available
    displayMgr -> displayMgr: Add plugin to available slot.
    displayMgr <-- displayMgr
    displayMgr -> plugin: Set slot interface.
    plugin --> displayMgr
    displayMgr -> plugin: Start plugin and provide display width and height.
    plugin --> displayMgr
    displayMgr --> pluginMgr: slot id
    pluginMgr -> topicHandlerService: Register plugin topics
        topicHandlerService -> plugin: Get topics
        topicHandlerService <-- plugin: topics
    pluginMgr <-- topicHandlerService
    pluginMgr --> app: Concrete plugin

    alt Depends on application
        app -> plugin: Enable plugin.
        plugin --> app
    end alt
else No slot available
    displayMgr --> pluginMgr: invalid slot id
    pluginMgr --> app: no plugin
end alt

note over app,topicHandlerService
    Uninstall plugin
end note

app -> pluginMgr: Uninstall concrete plugin.
pluginMgr -> displayMgr: Uninstall plugin.
pluginMgr -> topicHandlerService: Unregister topics
topicHandlerService --> pluginMgr
displayMgr -> plugin: Stop plugin.
plugin --> displayMgr
displayMgr -> displayMgr: Remove plugin from slot.
displayMgr -> plugin: Set invalid slot id.
plugin --> displayMgr
displayMgr --> pluginMgr
pluginMgr -> pluginMgr: Destroy concrete plugin.
pluginMgr --> app

note over app,topicHandlerService
    Plugin scheduler
end note

alt Plugin selected to show?

    displayMgr -> displayMgr: Set selected slot, retrieved from selected plugin.
    displayMgr <-- displayMgr

    alt Selected plugin disabled in the meantime?
        displayMgr -> displayMgr: Stop plugin duration timer.
        displayMgr <-- displayMgr
        displayMgr -> displayMgr: Clear selected plugin.
        displayMgr <-- displayMgr
    end alt

end alt

alt No plugin selected?

    displayMgr -> displayMgr: Find next slot with installed and enabled plugin.
    displayMgr <-- displayMgr

    alt Installed and enabled plugin found?
        displayMgr -> displayMgr: Select plugin.
        displayMgr <-- displayMgr
        displayMgr -> plugin: Get how long slot shall be active.
        plugin --> displayMgr: duration

        alt Is duration not infinite?
            displayMgr -> displayMgr: Start plugin duration timer.
            displayMgr <-- displayMgr
        end alt
    end alt

end alt

loop For all installed plugins

    alt Is plugin is enabled?

        displayMgr -> plugin: Process plugin.
        plugin --> displayMgr

    end alt

end loop

displayMgr -> displayMgr: Clear display
displayMgr <-- displayMgr

alt Is a plugin selected?
    displayMgr -> plugin: Call selected plugin to update framebuffer.
    plugin --> displayMgr
end alt

displayMgr -> displayMgr: Update display with framebuffer.
displayMgr <-- displayMgr

alt Is plugin duration timer active?
    alt Is plugin duration timer timeout?
        displayMgr -> displayMgr: Stop plugin run duration timer.
        displayMgr <-- displayMgr
        displayMgr -> displayMgr: Clear selected plugin.
        displayMgr <-- displayMgr
    end alt
end alt

@enduml