#!/usr/bin/env python3
"""Generate the Vinyl detail card (WindowXMLDialog; also works in Estuary)."""
from pathlib import Path
import xml.etree.ElementTree as ET

ROOT = Path(__file__).resolve().parents[2]
OUT = ROOT / 'resources/skins/Default/1080i/vinyl/DialogVinylDetail.xml'
ART = 'special://home/addons/plugin.video.milionar/resources/skin.milionar/media/milionar/cinema/'


def child(parent, name, value, **attrs):
    node = ET.SubElement(parent, name, attrs)
    node.text = str(value)
    return node


def control(parent, kind, x, y, width, height, identity=None, **fields):
    attrs = {'type': kind}
    if identity is not None:
        attrs['id'] = str(identity)
    node = ET.SubElement(parent, 'control', attrs)
    for name, value in dict(left=x, top=y, width=width, height=height, **fields).items():
        child(node, name, value)
    return node


def image(parent, x, y, width, height, texture, mask='', color='', aspect='scale', visible='true', border=''):
    node = control(parent, 'image', x, y, width, height, aspectratio=aspect, visible=visible)
    attrs = {'background': 'true'}
    if mask:
        attrs['diffuse'] = ART + mask
    if color:
        attrs['colordiffuse'] = color
    if border:
        attrs['border'] = border
    child(node, 'texture', texture, **attrs)
    return node


def label(parent, x, y, width, height, value, font='font25_title', color='FFFFFFFF', kind='label', visible='true'):
    return control(parent, kind, x, y, width, height, font=font, textcolor=color, aligny='center', label=value, visible=visible)


def button(parent, identity, width, label, visible='true'):
    node = control(parent, 'button', 0, 0, width, 64, identity, font='font12', label=label,
                   textcolor='FFFFFFFF', focusedcolor='FFFFFFFF', align='center', aligny='center',
                   visible=visible, onup='noop', ondown=500)
    child(node, 'texturefocus', ART + 'button.png', border='20', colordiffuse='button_focus')
    child(node, 'texturenofocus', ART + 'button_ghost.png', border='20')
    return node


def build():
    root = ET.Element('window', {'type': 'dialog'})
    child(root, 'defaultcontrol', 101)
    controls = ET.SubElement(root, 'controls')
    image(controls, 0, 0, 1920, 1080, ART + 'button.png', color='DD03060C')
    panel = control(controls, 'group', 160, 80, 1600, 920)
    image(panel, 0, 0, 1600, 920, ART + 'card.png', border='20')
    image(panel, 0, 0, 1600, 690, '$INFO[Window.Property(vinyl.fanart)]', 'hero_fade.png', '80FFFFFF')
    image(panel, 70, 78, 400, 400, '$INFO[Window.Property(vinyl.cover)]', 'mask_detail_cover.png', aspect='keep')
    label(panel, 70, 502, 400, 120, '$INFO[Window.Property(vinyl.title)]', 'font30_title', kind='textbox')
    label(panel, 70, 644, 400, 108, '$INFO[Window.Property(vinyl.left)]', 'font12', 'FFB5BFCE', kind='textbox')
    label(panel, 530, 48, 1000, 34, '$INFO[Window.Property(vinyl.kicker)]', 'font20', 'FFB5BFCE')
    has_title = '!String.IsEmpty(Window.Property(vinyl.title_image))'
    image(panel, 530, 86, 920, 126, '$INFO[Window.Property(vinyl.title_image)]', aspect='keep', visible=has_title)
    label(panel, 530, 86, 980, 120, '$INFO[Window.Property(vinyl.title)]', 'font45_title', kind='textbox', visible='!' + has_title)
    label(panel, 530, 220, 1000, 108, '$INFO[Window.Property(vinyl.description)]', 'font12', 'FFDCE3EC', kind='textbox')
    buttons = control(panel, 'grouplist', 530, 332, 1000, 64, 100, orientation='horizontal', itemgap=12,
                      onleft=106, onright=101, ondown=500)
    button(buttons, 101, 180, '$LOCALIZE[208]')
    button(buttons, 102, 140, '$ADDON[plugin.video.milionar 32523]', '!String.IsEmpty(Window.Property(vinyl.can_shuffle))')
    button(buttons, 103, 280, '$INFO[Window.Property(vinyl.favorite_label)]', '!String.IsEmpty(Window.Property(vinyl.can_favorite))')
    button(buttons, 104, 170, '$ADDON[plugin.video.milionar 32681]')
    button(buttons, 106, 110, '$ADDON[plugin.video.milionar 32682]')
    songs = control(panel, 'list', 530, 420, 1000, 384, 500, orientation='vertical', itemgap=4,
                    onup=101, ondown=108, onleft=101, onright=106)
    for focused in (False, True):
        layout = ET.SubElement(songs, 'focusedlayout' if focused else 'itemlayout', {'width': '1000', 'height': '96'})
        image(layout, 0, 0, 1000, 92, ART + ('button.png' if focused else 'button_ghost.png'),
              color='button_focus' if focused else '', border='20')
        label(layout, 14, 0, 46, 92, '$INFO[ListItem.Property(vinyl.number)]', 'font20')
        image(layout, 64, 10, 72, 72, '$INFO[ListItem.Art(thumb)]', 'mask_detail_track.png')
        label(layout, 156, 10, 710, 38, '$INFO[ListItem.Label]')
        label(layout, 156, 49, 710, 32, '$INFO[ListItem.Property(vinyl.artist)]', 'font20', 'FFDCE3EC')
        label(layout, 890, 0, 94, 92, '$INFO[ListItem.Property(vinyl.duration)]', 'font20')
    footer = control(panel, 'grouplist', 530, 834, 1000, 64, 109, orientation='horizontal', itemgap=16,
                     onup=500, onleft=107, onright=108)
    previous = button(footer, 107, 180, '$ADDON[plugin.video.milionar 31855]')
    child(previous, 'enable', '!String.IsEmpty(Window.Property(vinyl.previous))')
    previous.find('onup').text = '500'
    previous.find('ondown').text = '101'
    label(footer, 0, 0, 400, 64, '$INFO[Window.Property(vinyl.page)]', 'font20', 'FFB5BFCE')
    next_button = button(footer, 108, 180, '$ADDON[plugin.video.milionar 31856]')
    child(next_button, 'enable', '!String.IsEmpty(Window.Property(vinyl.next))')
    next_button.find('onup').text = '500'
    next_button.find('ondown').text = '101'
    ET.indent(root, space='\t')
    return '<?xml version="1.0" encoding="UTF-8"?>\n<!-- generated by gen_vinyl_detail.py: do not edit -->\n' + ET.tostring(root, encoding='unicode') + '\n'


if __name__ == '__main__':
    OUT.parent.mkdir(parents=True, exist_ok=True)
    OUT.write_text(build(), encoding='utf-8')
    print('wrote', OUT.relative_to(ROOT))
