"""Vinyl-specific cards and hero, composed by gen_cinema.py."""


def image(width, height, texture, left=0, top=0, mask='', aspect='scale', visible='true'):
    """A picture; `mask`: the diffuse mask that cuts it to the card's rounded shape."""
    diffuse = ' diffuse="milionar/cinema/{}"'.format(mask) if mask else ''
    return '''<control type="image"><left>{left}</left><top>{top}</top><width>{width}</width><height>{height}</height>
<aspectratio>{aspect}</aspectratio><texture background="true"{diffuse}>{texture}</texture><visible>{visible}</visible></control>'''.format(**locals())


def shape(width, height, texture, border=0):
    """A texture of the skin stretched over the box; `border` keeps the corners of a small panel texture round."""
    border = ' border="{}"'.format(border) if border else ''
    return '''<control type="image"><width>{width}</width><height>{height}</height>
<texture{border}>milionar/cinema/{texture}</texture></control>'''.format(**locals())


def label(width, height, value, left=0, top=0, font='font20', color='white', visible='true', align='left'):
    return '''<control type="label"><left>{left}</left><top>{top}</top><width>{width}</width><height>{height}</height>
<font>{font}</font><textcolor>{color}</textcolor><align>{align}</align><aligny>center</aligny><label>{value}</label><visible>{visible}</visible></control>'''.format(**locals())


SIZES = {'Square': (216, 290), 'Record': (192, 268), 'Artist': (192, 268), 'MusicWide': (326, 258),
         'Banner': (490, 276), 'Track': (490, 108), 'Genre': (248, 168)}
ROUND = ('Record', 'Artist')              # a circle with its texts under it: no panel, the focus frame is a ring
# style -> (width, height, texture element) of the focus frame, drawn 16 px around the card's top left corner
CIRCLE_FRAME = '<texture colordiffuse="button_focus">milionar/cinema/focus_circle.png</texture>'   # the ring of a round card


def card(style):
    """Every card is one rounded shape: the panel (card.png, radius kept by its border), and pictures cut by a mask of
    the same radius where they touch the panel's corners. The focus frame follows that shape."""
    circle = style in ROUND
    width, height = SIZES[style]
    art = '$INFO[ListItem.Art(thumb)]'              # the square cover (the hero has the original)
    body = '' if circle else shape(width, height, 'card.png', 20)
    if style == 'Track':
        body += image(84, 84, art, 12, 12, 'mask_track.png')
        body += label(292, 38, '$INFO[ListItem.Label]', 110, 16, 'font25_title')
        body += label(290, 34, '$INFO[ListItem.Property(milionar.hero.note)]', 110, 57, color='cinema_dim')
        body += label(64, 34, '$INFO[ListItem.Property(vinyl.duration)]', 410, 37, align='right')
    elif style in ('Banner', 'Genre'):
        # The square cover, uncropped, behind a mask with the card's window (see gen_cinema_textures.band): a mask
        # of the card's size would be cropped with the picture and leave square corners.
        body += image(width, width, art, top=-(width - height) // 2, mask='mask_banner.png' if style == 'Banner' else 'mask_genre.png')
        body += shape(width, height, 'shade_music.png' if style == 'Banner' else 'shade_genre.png')
        if style == 'Banner':
            has_image = '!String.IsEmpty(ListItem.Property(milionar.hero.title_image))'
            body += image(width - 40, 92, '$INFO[ListItem.Property(milionar.hero.title_image)]', 20, height - 152,
                          aspect='keep', visible=has_image)
            body += label(width - 40, 44, '$INFO[ListItem.Label]', 20, height - 102, 'font30_title', visible='!' + has_image)
            body += label(width - 40, 38, '$INFO[ListItem.Property(vinyl.subtitle)]', 20, height - 50, color='cinema_text')
        else:
            body += label(width - 32, 42, '$INFO[ListItem.Label]', 16, height - 52, 'font25_title')
    else:
        cover_h = width if style != 'MusicWide' else 184
        mask = 'mask_circle.png' if circle else 'mask_square.png' if style == 'Square' else 'mask_music_wide.png'
        # every cover is drawn as the uncropped square; the wide card's mask opens its 184 px window (see Banner)
        body += image(width, width, art, top=-(width - cover_h) // 2, mask=mask)
        if style == 'Record':
            body += shape(width, cover_h, 'record_grooves.png')
        inset, align = (0, 'center') if circle else (12, 'left')
        body += label(width - 2 * inset, 36, '$INFO[ListItem.Label]', inset, cover_h + 8, 'font25_title', align=align)
        body += label(width - 2 * inset, 30, '$INFO[ListItem.Property(milionar.hero.note)]', inset, cover_h + 42,
                      color='cinema_dim', align=align)
    return body.replace('<control ', '{t}<control '), width, height


ROWS = {name: card(name) for name in SIZES}


# The hero: one text block that ends right above the buttons (a bottom-aligned group list, so a short description or
# a missing line leaves no hole): title image or text title, genres, description (up to three lines), facts. The
# buttons are the managed ones every section has (gen_cinema.managed_buttons).
HERO_BODY = """\t\t<control type="group">
\t\t\t<left>70</left>
\t\t\t<top>0</top>
\t\t\t<visible>$EXP[MilCineAny_{sec}]</visible>
\t\t\t<control type="grouplist">
\t\t\t\t<top>20</top>
\t\t\t\t<width>1000</width>
\t\t\t\t<height>354</height>
\t\t\t\t<orientation>vertical</orientation>
\t\t\t\t<align>bottom</align>
\t\t\t\t<itemgap>8</itemgap>
\t\t\t\t<control type="image">
\t\t\t\t\t<width>760</width>
\t\t\t\t\t<height>150</height>
\t\t\t\t\t<aspectratio align="left" aligny="bottom">keep</aspectratio>
\t\t\t\t\t<texture background="true">$VAR[MilCineTitleImage_{sec}]</texture>
\t\t\t\t\t<visible>$EXP[MilCineHasTitleImage_{sec}]</visible>
\t\t\t\t</control>
{titles}
\t\t\t\t<control type="label">
\t\t\t\t\t<width>1000</width>
\t\t\t\t\t<height>34</height>
\t\t\t\t\t<font>font12</font>
\t\t\t\t\t<textcolor>cinema_dim</textcolor>
\t\t\t\t\t<shadowcolor>cinema_shadow</shadowcolor>
\t\t\t\t\t<aligny>center</aligny>
\t\t\t\t\t<label>$VAR[MilCineKicker_{sec}]</label>
\t\t\t\t</control>
\t\t\t\t<control type="textbox">
\t\t\t\t\t<width>900</width>
\t\t\t\t\t<height min="34" max="102">auto</height>
\t\t\t\t\t<font>font12</font>
\t\t\t\t\t<textcolor>cinema_text</textcolor>
\t\t\t\t\t<shadowcolor>cinema_shadow</shadowcolor>
\t\t\t\t\t<label>$VAR[MilCinePlot_{sec}]</label>
\t\t\t\t\t<visible>$EXP[MilCineHasPlot_{sec}]</visible>
\t\t\t\t</control>
\t\t\t\t<control type="label">
\t\t\t\t\t<width>1000</width>
\t\t\t\t\t<height>28</height>
\t\t\t\t\t<font>font20</font>
\t\t\t\t\t<textcolor>cinema_dim</textcolor>
\t\t\t\t\t<shadowcolor>cinema_shadow</shadowcolor>
\t\t\t\t\t<aligny>center</aligny>
\t\t\t\t\t<label>$VAR[MilCineMeta_{sec}]</label>
\t\t\t\t\t<visible>$EXP[MilCineHasMeta_{sec}]</visible>
\t\t\t\t</control>
\t\t\t</control>
{buttons}
\t\t</control>"""

HERO_TITLE = """\t\t\t\t<control type="label">
\t\t\t\t\t<width>1000</width>
\t\t\t\t\t<height>{height}</height>
\t\t\t\t\t<font>{font}</font>
\t\t\t\t\t<aligny>center</aligny>
\t\t\t\t\t<shadowcolor>cinema_shadow</shadowcolor>
\t\t\t\t\t<label>$VAR[MilCineTitle_{sec}]</label>
\t\t\t\t\t<visible>$EXP[MilCineSize_{step}_{sec}]</visible>
\t\t\t\t</control>"""
TITLE_HEIGHT = {'l': 104, 'm': 84, 's': 64}

def hero_controls(title_fonts, ids, buttons):
    """`buttons`: the section's managed hero buttons (gen_cinema.managed_buttons)."""
    titles = '\n'.join(HERO_TITLE.format(step=step, font=font, height=TITLE_HEIGHT[step], sec=ids['sec'])
                       for step, font, _left in title_fonts)
    return [HERO_BODY.format(titles=titles, buttons=buttons, sec=ids['sec'])]
