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

from aes import random_bytes
from encodings import hexlify
from gettext import gettext as _, register_callback, gettext as gt

_boss = None
dark_link_color = '__DARK_LINK_COLOR__'


def set_boss(b):
    nonlocal _boss
    _boss = b

def get_boss():
    return _boss

def current_book():
    return current_book.book
current_book.book = None

def rtl_page_progression():
    # Other options are "ltr" and "default." For Calibre, "default" is LTR.
    return current_book().manifest.page_progression_direction == 'rtl'

def ltr_page_progression():
    # Only RTL and LTR are supported, so it must be LTR if not RTL.
    return not rtl_page_progression()

uid = 'calibre-' + hexlify(random_bytes(12))

def viewport_mode_changer(val):
    if val:
        viewport_mode_changer.val = val
    return viewport_mode_changer.val

def current_layout_mode():
    return current_layout_mode.value
current_layout_mode.value = 'flow'

def set_layout_mode(val):
    current_layout_mode.value = val
    viewport_mode_changer()(val)

def current_spine_item():
    return current_spine_item.value
current_spine_item.value = None

def set_current_spine_item(val):
    current_spine_item.value = val

def toc_anchor_map():
    return toc_anchor_map.value

def set_toc_anchor_map(val):
    toc_anchor_map.value = val

default_color_schemes = {
    'system':{'foreground':'#000000', 'background':'#ffffff', 'name':_('System')},
    'white':{'foreground':'#000000', 'background':'#ffffff', 'name':_('White')},
    'black':{'foreground':'#ffffff', 'background':'#000000', 'link': '#4f81bd', 'name':_('Black')},
    'sepia-light':{'foreground':'#39322B', 'background':'#F6F3E9', 'name':_('Sepia light')},
    'sepia-dark': {'background':'#39322B', 'foreground':'#F6F3E9', 'link': '#4f81bd', 'name':_('Sepia dark')},
}


def set_system_colors(spec):
    c = default_color_schemes.system
    c.foreground = spec.foreground
    c.background = spec.background
    v'delete c.link'
    if spec.link:
        c.link = spec.link

register_callback(def():
    # Ensure the color scheme names are translated
    for key in default_color_schemes:
        scheme = default_color_schemes[key]
        scheme.name = gt(scheme.name)
    # set the system colors if in dark mode
    if window.matchMedia and window.matchMedia('(prefers-color-scheme: dark)').matches:
        set_system_colors({'background': '#111', 'foreground': '#ddd', 'link': dark_link_color})
)

IN_DEVELOP_MODE = '__IN_DEVELOP_MODE__'
runtime = {
    'is_standalone_viewer': False,
    'viewer_in_full_screen': False,
    'in_develop_mode': IN_DEVELOP_MODE is '1',
    'QT_VERSION': 0,
}


ui_operations = {
    'get_file': None,
    'get_mathjax_files': None,
    'update_url_state': None,
    'update_last_read_time': None,
    'show_error': None,
    'redisplay_book': None,
    'reload_book': None,
    'forward_gesture': None,
    'update_color_scheme': None,
    'update_font_size': None,
    'goto_cfi': None,
    'delete_book': None,
    'focus_iframe': None,
    'toggle_toc': None,
    'toggle_full_screen': None,
}



annot_id_uuid_map = {}


def clear_annot_id_uuid_map():
    for key in Object.keys(annot_id_uuid_map):
        v'delete annot_id_uuid_map[key]'


def is_dark_theme(set_val):
    if set_val?:
        is_dark_theme.ans = set_val
    return v'!!is_dark_theme.ans'
