# ruff: noqa: F821

class Module:
    config = {
        "title": {
            "type": "string",
            "label": {"ru": "Название", "en": "Title"},
            "default": "Витрина",
            "min": 1,
            "max": 64,
        },
        "enabled": {
            "type": "boolean",
            "label": {"ru": "Включено", "en": "Enabled"},
            "default": True,
        },
        "step": {
            "type": "integer",
            "label": {"ru": "Шаг", "en": "Step"},
            "min": 1,
            "max": 20,
            "default": 3,
        },
        "scale": {
            "type": "number",
            "label": {"ru": "Множитель", "en": "Scale"},
            "min": 0,
            "max": 10,
            "default": 1.5,
        },
        "mode": {
            "type": "select",
            "label": {"ru": "Режим", "en": "Mode"},
            "choices": ["short", "full"],
            "default": "short",
        },
        "detail": {
            "type": "string",
            "label": {"ru": "Подробность", "en": "Detail"},
            "default": "видно только в режиме full",
            "when": {"field": "mode", "equals": "full"},
        },
        "tags": {
            "type": "list",
            "label": {"ru": "Метки", "en": "Tags"},
            "items": "string",
            "default": ["альфа", "бета"],
            "max": 8,
        },
        "chat": {
            "type": "chat",
            "label": {"ru": "Чат", "en": "Chat"},
        },
        "user": {
            "type": "user",
            "label": {"ru": "Пользователь", "en": "User"},
        },
        "note": {
            "type": "json",
            "label": {"ru": "JSON", "en": "JSON"},
            "default": {"limit": 3},
        },
        "token": {
            "type": "secret",
            "label": {"ru": "Секрет", "en": "Secret"},
        },
        "attachment": {
            "type": "file",
            "label": {"ru": "Файл", "en": "File"},
        },
    }
    pages = [
        {
            "id": "board",
            "title": {"ru": "Доска", "en": "Board"},
            "elements": [
                {"id": "about", "type": "text", "label": {"ru": "Текст", "en": "Text"}},
                {"id": "level", "type": "metric", "label": {"ru": "Уровень", "en": "Level"}},
                {
                    "id": "rows",
                    "type": "table",
                    "label": {"ru": "Таблица", "en": "Table"},
                    "columns": ["name", "value"],
                },
                {"id": "jobs", "type": "tasks", "label": {"ru": "Расписание", "en": "Schedule"}},
            ],
        },
        {
            "id": "actions",
            "title": {"ru": "Действия", "en": "Actions"},
            "elements": [
                {"id": "ping", "type": "button", "action": "ping", "label": {"ru": "Проверка", "en": "Ping"}},
                {"id": "save", "type": "form", "action": "save", "label": {"ru": "Записать текст", "en": "Save text"}},
            ],
        },
    ]

    async def init(self):
        await self.publish()

    async def publish(self):
        title = await app.config.get("title", "Витрина")
        step = await app.config.get("step", 3)
        await app.ui.set("board", {
            "about": title,
            "level": step,
            "rows": [{"name": "режим", "value": await app.config.get("mode", "short")}],
        })

    @loader.command("status", source="both")
    async def status(self, ctx):
        await self.publish()
        await ctx.reply(await app.config.get("title", "Витрина"))

    @loader.action()
    async def ping(self, ctx):
        return {"ok": True}

    @loader.action(fields={"text": {"type": "string", "label": {"ru": "Текст", "en": "Text"}, "default": "проверка"}})
    async def save(self, ctx):
        await app.db.set("text", ctx.values["text"])
        await self.publish()
        return {"text": ctx.values["text"]}

    @loader.handler(event="config")
    async def changed(self, ctx):
        await self.publish()
