#!/usr/bin/env python3
"""Generate the single categorized Appearance settings dialog."""
from pathlib import Path
import xml.etree.ElementTree as ET

ROOT=Path(__file__).resolve().parents[2]
OUT=ROOT/'resources/skins/Default/1080i/skin/DialogMilionarAppearance.xml'


def node(parent,tag,text=None,**attrs):
    result=ET.SubElement(parent,tag,attrs);result.text=text;return result


def control(parent,kind,values,**attrs):
    result=node(parent,'control',type=kind,**attrs)
    for tag,value in values.items():node(result,tag,str(value))
    return result


def rows(parent,width,categories=False):
    for focused in (False,True):
        row=node(parent,'focusedlayout' if focused else 'itemlayout',width=str(width),height='72')
        if focused:
            focus=control(row,'image',{'left':0,'top':4,'width':width,'height':64})
            node(focus,'texture','lists/focus.png',colordiffuse='button_focus')
            if categories:
                node(focus,'visible','Control.HasFocus(50)')
                selected=control(row,'image',{'left':0,'top':4,'width':width,'height':64})
                node(selected,'texture','lists/focus.png',colordiffuse='button_alt_focus')
                node(selected,'visible','!Control.HasFocus(50)')
        control(row,'label',{'left':20,'top':4,'width':width-40 if categories else width-360,'height':64,
                            'font':'font25_title' if categories else 'font12','aligny':'center',
                            'textcolor':('$VAR[MilionarCategoryInk]' if categories else 'flat_ink') if focused else 'grey',
                            'selectedcolor':'flat_ink' if focused and not categories else 'white',
                            'label':'$INFO[ListItem.Label]'})
        if not categories:
            control(row,'label',{'left':width-340,'top':4,'width':316,'height':64,'font':'font12',
                                'align':'right','aligny':'center','textcolor':'flat_ink' if focused else 'cinema_dim',
                                'label':'$INFO[ListItem.Label2]'})


def build():
    root=ET.Element('window',type='dialog')
    node(root,'defaultcontrol','53',always='true');node(root,'include','Animation_DialogPopupOpenClose')
    node(root,'depth','DepthDialog+')
    group=control(node(root,'controls'),'group',{'centerleft':'50%','centertop':'50%','width':1800,'height':960})
    panel=node(group,'include',content='DialogBackgroundCommons')
    for key,value in (('width','1800'),('height','960'),('darken_bg','true'),
                      ('header_label','$ADDON[plugin.video.milionar 35616]'),('header_id','')):
        node(panel,'param',name=key,value=value)
    control(group,'label',{'left':48,'top':76,'width':1704,'height':36,'font':'font20','textcolor':'grey',
                           'label':'$ADDON[plugin.video.milionar 35714]',
                           'visible':'String.IsEqual(Window.Property(appearance.page),appearance)'})
    control(group,'label',{'left':48,'top':76,'width':1704,'height':36,'font':'font20','textcolor':'grey',
                           'label':'$ADDON[plugin.video.milionar 35722]',
                           'visible':'String.IsEqual(Window.Property(appearance.page),sections)'})
    categories=control(group,'list',{'left':48,'top':124,'width':280,'height':744,'orientation':'vertical',
                                    'onleft':50,'onright':51,'onup':50,'ondown':53,'scrolltime':100},id='50')
    rows(categories,280,True)
    close=control(group,'button',{'left':48,'top':868,'width':280,'height':64,'font':'font12',
                                 'align':'center','aligny':'center','label':'$ADDON[plugin.video.milionar 32777]',
                                 'onleft':50,'onright':50,'onup':50,'ondown':50},id='53')
    node(close,'texturefocus','lists/focus.png',colordiffuse='button_focus')
    node(close,'texturenofocus')
    categories.find('onright').set('condition','String.IsEqual(Window.Property(appearance.page),appearance)')
    node(categories,'onright','52',condition='!String.IsEqual(Window.Property(appearance.page),appearance)')
    for appearance in (True,False):
        listing=control(group,'list',{'left':368,'top':124,'width':1384,'height':288 if appearance else 744,
                                     'orientation':'vertical','onleft':50,'onright':51 if appearance else 52,'onup':51 if appearance else 52,'ondown':51 if appearance else 52,
                                     'scrolltime':100,'visible':('' if appearance else '!')+'String.IsEqual(Window.Property(appearance.page),appearance)'},id='51' if appearance else '52')
        rows(listing,1384)
    preview=control(group,'image',{'left':612,'top':428,'width':896,'height':504,'aspectratio':'keep',
                                  'visible':'String.IsEqual(Window.Property(appearance.page),appearance)'},id='60')
    node(preview,'texture','$INFO[Window.Property(appearance.preview)]',background='true')
    ET.indent(root,space='\t');return ET.tostring(root,encoding='utf-8',xml_declaration=True)


if __name__=='__main__':
    OUT.write_bytes(build());print('generated Appearance settings')
