# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
from __python__ import hash_literals, bound_methods

from dom import clear, set_css, build_rule
from elementmaker import E
from gettext import gettext as _


COVER_GRID_CLASS = 'book-list-cover-grid'
THUMBNAIL_MAX_WIDTH = 3 * 100
THUMBNAIL_MAX_HEIGHT = 4 * 100
THUMBNAIL_MIN_WIDTH = 3 * 35
THUMBNAIL_MIN_HEIGHT = 4 * 35
BORDER_RADIUS = 10


def description():
    return _('A grid of book covers')


def cover_grid_css():
    sel = '.' + COVER_GRID_CLASS
    margin, margin_unit = 10, 'px'
    ans = build_rule(sel, display='flex', flex_wrap='wrap', justify_content='space-around', align_items='flex-end',
                     align_content='flex-start', user_select='none', overflow='hidden', margin_top=f'{margin / 2}{margin_unit}')

    # Container for an individual cover
    sel += ' > a'
    ans += build_rule(
        sel, margin=f'{margin}{margin_unit}', display='flex', align_content='flex-end', align_items='center', justify_content='space-around',
        max_width=THUMBNAIL_MAX_WIDTH+'px', max_height=THUMBNAIL_MAX_HEIGHT+'px', cursor='pointer',
        min_width=THUMBNAIL_MIN_WIDTH+'px', min_height=THUMBNAIL_MIN_HEIGHT + 'px')
    mq =  '@media all and (orientation: {orient}) {{ {sel} {{ width: 21{dim}; height: 28{dim} }} }}\n'
    for dim in 'vw', 'vh':
        ans += mq.format(sel=sel, dim=dim, orient='portrait' if dim is 'vw' else 'landscape')
    ans += build_rule(f'{sel}:hover', transform='scale(1.2)')
    ans += build_rule(f'{sel}:active', transform='scale(2)')
    ans += build_rule(sel + '.cover-grid-filler', height='0', max_height='0', min_height='0')

    # Container for cover failed to load message
    ans += build_rule(sel + ' > div', position='relative', top='-50%', transform='translateY(50%)', margin='0')

    # The actual cover
    sel += ' > img'
    ans += build_rule(sel, max_width='100%', max_height='100%', display='block', width='auto', height='auto', border_radius=BORDER_RADIUS+'px')
    return ans

def init(container):
    clear(container)
    container.appendChild(E.div(class_=COVER_GRID_CLASS))
    for i in range(12):
        container.lastChild.appendChild(E.div(class_='cover-grid-filler'))

def on_img_load(img, load_type):
    div = img.parentNode
    if not div:
        return
    if load_type is not 'load':
        clear(div)
        div.appendChild(E.div(
            E.h2(img.dataset.title, style='text-align:center; font-size:larger; font-weight: bold'),
            E.div(_('by'), style='text-align: center'),
            E.h2(img.dataset.authors, style='text-align:center; font-size:larger; font-weight: bold')
        ))
        set_css(div, border='dashed 1px currentColor', border_radius=BORDER_RADIUS+'px')

def create_item(book_id, metadata, create_image, show_book_details, href):
    authors = metadata.authors.join(' & ') if metadata.authors else _('Unknown')
    img = create_image(book_id, THUMBNAIL_MAX_WIDTH, THUMBNAIL_MAX_HEIGHT, on_img_load)
    tooltip = _('{} by {}').format(metadata.title, authors)
    img.setAttribute('alt', tooltip)
    img.dataset.title, img.dataset.authors = metadata.title, authors
    ans = E.a(img, onclick=show_book_details, title=tooltip, href=href)
    return ans

def append_item(container, item):
    first_filler = container.lastChild.querySelector('.cover-grid-filler')
    container.lastChild.insertBefore(item, first_filler)
