# -*- coding: utf-8 -*-
"""Common Kodi Dialog wrappers for CloudDrive2 plugin."""

try:
    import xbmcgui
    import xbmc
    _IN_KODI = True
except ImportError:
    _IN_KODI = False

from . import cd2_utils
from .cd2_l10n import _


def ok(heading, message):
    """Show an OK dialog."""
    dialog = xbmcgui.Dialog()
    dialog.ok(str(heading), str(message))


def yesno(heading, message, yeslabel=None, nolabel=None):
    """Show a Yes/No dialog. Returns True for Yes."""
    dialog = xbmcgui.Dialog()
    return dialog.yesno(str(heading), str(message), yeslabel, nolabel)


def select(heading, options):
    """Show a select list dialog. Returns index or -1."""
    dialog = xbmcgui.Dialog()
    return dialog.select(str(heading), list(options))


def input_text(heading, default=""):
    """Show a text input dialog. Returns entered string or None."""
    dialog = xbmcgui.Dialog()
    result = dialog.input(str(heading), default)
    return result if result else None


def notification(title, message, time_ms=3000):
    """Show a Kodi notification."""
    dialog = xbmcgui.Dialog()
    dialog.notification(str(title), str(message), xbmcgui.NOTIFICATION_INFO, time_ms)


def progress(heading, message="", percent=0):
    """Create a progress dialog. Use as context manager."""
    return _ProgressDialog(heading, message, percent)


class _ProgressDialog:
    def __init__(self, heading, message, percent=0):
        self._dialog = xbmcgui.DialogProgress()
        self._heading = heading
        self._message = message
        self._dialog.create(heading, message)

    def update(self, percent, message=None):
        if message:
            self._message = message
        self._dialog.update(percent, self._message)

    def close(self):
        self._dialog.close()
        self._dialog = None

    def is_canceled(self):
        return self._dialog.iscanceled()

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.close()


def show_busy(message="Working..."):
    """Show a busy indicator."""
    xbmc.executebuiltin(f"ActivateWindow(busydialognocancel)")


def hide_busy():
    """Hide the busy indicator."""
    xbmc.executebuiltin("Dialog.Close(busydialognocancel,true)")


def input_credentials(title="CloudDrive2 Account"):
    """Show a dialog to input username and password. Returns (user, pass) or (None, None)."""
    dialog = xbmcgui.Dialog()
    user = dialog.input(_(32122, title), "")
    if not user:
        return None, None
    pw = dialog.input(_(32123, title), "", xbmcgui.INPUT_ALPHANUM, xbmcgui.ALPHANUM_HIDE_INPUT)
    if pw is None:
        pw = ""
    return user, pw


def input_register():
    """Show register form: username + password. Validates min 6-char password."""
    dialog = xbmcgui.Dialog()
    user = dialog.input(_(32120), "")
    if not user:
        return None, None

    pw = dialog.input(_(32121), "", xbmcgui.INPUT_ALPHANUM, xbmcgui.ALPHANUM_HIDE_INPUT)
    if not pw:
        return None, None

    if len(pw) < 6:
        dialog.ok(_(32030), _(32046))
        return None, None

    return user, pw
