#!/usr/bin/env python3

from i3dstatus.block import Block

from shutil import which
import os
import sys
from subprocess import Popen, PIPE
import asyncio
from asyncio.subprocess import create_subprocess_exec, PIPE


async def main():
    block = await Block(os.path.basename(__file__)).connect()

    player_format = '{{artist}} - {{title}} {{duration(position)}}|{{duration(mpris:length)}}'
    playerctl_path = which('playerctl')

    if 'format' in block.config:
        player_format = block.config['format']

    if 'path' in block.config:
        playerctl_path = block.config['path']

    if not playerctl_path:
        block.error('could not find playerctl in the path')
        sys.exit(1)

    if not os.path.exists(playerctl_path):
        block.error('could not find playerctl path at {}'.format(playerctl_path))
        sys.exit(1)

    process = await create_subprocess_exec(playerctl_path,
                                           'metadata',
                                           '--format',
                                           player_format,
                                           '--follow',
                                           stdout=PIPE,
                                           stderr=PIPE)

    while True:
        line = await process.stdout.readline()
        line = str.strip(line.decode())
        await block.show(line)

        if process.stdout.at_eof():
            break


loop = asyncio.get_event_loop()
loop.run_until_complete(main())
