# -*- coding: utf-8 -*-

import wx

from outwiker.api.app.tree import testPageTitle
from outwiker.api.core.events import PAGE_UPDATE_CONTENT
from outwiker.api.core.exceptions import ReadonlyException
from outwiker.api.core.tree import testreadonly

from .gui.textentrydialog import TextEntryDialog
from .gui.datetimedialog import DateTimeDialog
from hackpage.i18n import get_
from hackpage.validators import ChangeUidValidator


@testreadonly
def changeUidWithDialog(page, application):
    """
    Show dialog to change page UID
    """
    global _
    _ = get_()

    if page is None or page.parent is None:
        return

    if page.readonly:
        raise ReadonlyException

    title = _("Change page identifier")
    message = _('Enter new identifier for page "{}"').format(page.display_title)
    prefix = "page://"
    defaultValue = page.getUid(generate=True)
    validator = ChangeUidValidator(application, page)

    with TextEntryDialog(
        application.mainWindow,
        title=title,
        message=message,
        prefix=prefix,
        value=defaultValue,
        validator=validator,
    ) as dlg:
        if dlg.ShowModal() == wx.ID_OK:
            # Не отлавливаем исключения, поскольку считаем,
            # что правильность идентификатора проверил validator
            page.setUid(dlg.Value.strip())


@testreadonly
def setAliasWithDialog(page, application):
    """
    Show dialog to set page alias
    """
    global _
    _ = get_()

    if page is None or page.parent is None:
        return

    if page.readonly:
        raise ReadonlyException

    title = _("Set page alias")
    message = _("Enter alias for page").format(page.display_title)
    defaultValue = page.alias if page.alias is not None else ""

    with TextEntryDialog(
        application.mainWindow, title=title, message=message, value=defaultValue
    ) as dlg:
        if dlg.ShowModal() == wx.ID_OK:
            page.alias = dlg.GetValue()


@testreadonly
def setPageFolderWithDialog(page, application):
    """
    Change page folder and set alias which matches old display_title
    """
    global _
    _ = get_()

    if page is None or page.parent is None:
        return

    if page.readonly:
        raise ReadonlyException

    oldTitle = page.title
    oldDisplayTitle = page.display_title

    title = _("Set page folder")
    message = _("Enter the page folder name")
    defaultValue = oldTitle

    with TextEntryDialog(
        application.mainWindow,
        title=title,
        message=message,
        value=defaultValue,
        validator=testPageTitle,
    ) as dlg:
        if dlg.ShowModal() == wx.ID_OK:
            if page.title != dlg.GetValue():
                page.title = dlg.GetValue()
                page.alias = oldDisplayTitle


@testreadonly
def setPageCreationDate(page, application):
    global _
    _ = get_()

    title = _("Page creation date and time")
    selectedDateTime = page.creationdatetime

    with DateTimeDialog(
        application.mainWindow, title=title, selectedDateTime=selectedDateTime
    ) as dlg:
        if dlg.ShowModal() == wx.ID_OK:
            page.creationdatetime = dlg.getDateTime()
            application.onPageUpdate(page, change=PAGE_UPDATE_CONTENT)


@testreadonly
def setPageChangeDate(page, application):
    global _
    _ = get_()

    title = _("Date and time of change of the page")
    selectedDateTime = page.datetime

    with DateTimeDialog(
        application.mainWindow, title=title, selectedDateTime=selectedDateTime
    ) as dlg:
        if dlg.ShowModal() == wx.ID_OK:
            page.datetime = dlg.getDateTime()
            application.onPageUpdate(page, change=PAGE_UPDATE_CONTENT)
