# -*- coding: utf-8 -*-
"""The looks of the Millionaire game window and how each one is operated (addon setting `milionar_game_design`).

Both designs are driven by the same adapter and controller; a design is an XML file plus a map from its controls to
intents (what the player asked for). Intents: answer0-3, life5050, lifetip, lifeaudience, primary / secondary (the
first / second menu button of the classic card), start, rules, back, next, again, results, retry (menu buttons of the
modern card), title (choose another title), home, end (leave the game), exit_stay (stay after "leave the game?"),
settings (open the settings of the title card), set_<key> (a row of the settings: OK takes its next value).
"""
from typing import Dict, NamedTuple

import xbmcaddon

from wcs.games import game_settings

SETTING = 'milionar_game_design'
ANSWER_IDS = (9500, 9501, 9502, 9503)
LIFE_IDS = {'life5050': 9510, 'lifetip': 9511, 'lifeaudience': 9512}


class Design(NamedTuple):
    key: str
    xml: str
    modern: bool                       # reads the structured properties of game_view (WCS.Game.Screen ...)
    clicks: Dict[int, str]             # control id -> intent
    primary: Dict[str, int]            # screen -> control that gets the focus first (modern card)
    menu_primary: int                  # control focused in menu states (classic card)
    confirm_exit: bool                 # Back during a game asks first


def _answers(life_ids):
    clicks = {control: 'answer{}'.format(index) for index, control in enumerate(ANSWER_IDS)}
    clicks.update({control: intent for intent, control in life_ids.items()})
    return clicks


CLASSIC = Design(
    key='classic', xml='games/DialogMilionarGame.xml', modern=False,
    clicks={**_answers(LIFE_IDS), 9514: 'primary', 9515: 'secondary', 9513: 'end'},
    primary={}, menu_primary=9514, confirm_exit=False)

MODERN_LIFE_IDS = {'life5050': 9631, 'lifetip': 9632, 'lifeaudience': 9633}
MODERN = Design(
    key='modern', xml='games/DialogMilionarGameModern.xml', modern=True,
    clicks={**_answers(MODERN_LIFE_IDS),
        9601: 'start', 9602: 'rules', 9603: 'title',
        9604: 'settings', 9605: 'mode_millionaire', 9606: 'mode_story',
        **{control: 'set_' + key for key, control in game_settings.CONTROLS.items()},
        9611: 'start', 9612: 'back',
        9621: 'start', 9622: 'back', 9623: 'back',
        9641: 'next', 9642: 'back',
        9651: 'again', 9652: 'title', 9653: 'home',
        9661: 'results', 9662: 'again',
        9671: 'title', 9672: 'again', 9673: 'home',
        9681: 'retry', 9682: 'back',
        9691: 'exit_stay', 9692: 'exit_confirm',
        9781: 'start', 9782: 'back', 9783: 'back', 9784: 'retry', 9785: 'back',
        **{9790 + index: 'answer{}'.format(index) for index in range(4)},
        9794: 'story_primary', 9796: 'title', 9797: 'home'},
    primary={'title': 9601, 'rules': 9611, 'intro': 9621, 'loading': 9623, 'question': 9500, 'feedback': 9641,
             'gameover': 9651, 'win': 9661, 'results': 9671, 'error': 9681},
    menu_primary=9601, confirm_exit=True)

DESIGNS = {'0': CLASSIC, '1': MODERN}
DEFAULT = MODERN


def current():
    """The design chosen in the addon settings (the modern card unless the classic one is selected)."""
    try:
        return DESIGNS.get(xbmcaddon.Addon('plugin.video.milionar').getSetting(SETTING), DEFAULT)
    except Exception:
        return DEFAULT
