#!/bin/python3

import asyncio

from i3ipc import Event
from i3ipc.aio import Connection


async def main():
    sway = await Connection().connect()

    @sway.on(Event.WINDOW_NEW)
    @sway.on(Event.WINDOW_CLOSE)
    @sway.on(Event.WINDOW_MOVE)
    @sway.on(Event.WINDOW_TITLE)
    async def on_window_change(sway, _):
        for w in (await sway.get_tree()).workspaces():
            new_name = f'{w.num}:'

            first_descendant = True
            for d in w.descendants():
                app_name = d.app_id
                if not app_name:
                    app_name = d.window_instance

                if app_name:
                    if not first_descendant:
                        new_name += ' '
                    first_descendant = False

                    if 'kitty' in app_name:
                        if 'vim' in d.name:
                            new_name += '<span font=\'12\' rise=\'-2500\'>\ue6c5</span>'
                        else:
                            new_name += ''
                    elif app_name in ['Chromium', 'google-chrome', 'google-chrome-unstable']:
                        if 'YouTube' in d.name:
                            new_name += ''
                        else:
                            new_name += ''
                    elif app_name == 'Navigator':
                        new_name += ''
                    elif app_name == 'discord':
                        new_name += ''
                    elif app_name == 'spotify':
                        new_name += '<span font=\'11\' rise=\'-400\'></span>'
                    elif app_name in ['GEMOC Studio', 'code-oss']:
                        new_name += ''
                    elif 'okular' in app_name or 'zathura' in app_name:
                        new_name += '<span font=\'FontAwesome 5 Free Solid\'></span>'
                    elif app_name == 'pavucontrol':
                        new_name += ''
                    else:
                        new_name += app_name

            await sway.command(f'rename workspace "{w.name}" to "{new_name}"')


    await sway.main()

asyncio.run(main())
