# -*- coding: utf-8 -*-
"""gui_api tutorial 1: modal dialogs.

Every Ask* function opens a CLO-styled dialog, waits for the user and returns the answer.
Run it from the Python Script editor or drag and drop the file onto CLO.
"""
import ApiTypes
import gui_api

if not gui_api.IsGuiAvailable():
    raise SystemExit("gui_api needs the CLO window (it is not available in headless mode)")

# 1. Yes / no question. Returns True only when the accept button is pressed.
if not gui_api.AskConfirm("Dialog tour", "Walk through the five gui_api dialogs?", "Start", "Cancel"):
    raise SystemExit("cancelled")

# 2. One line of text. The result tells whether the user pressed OK.
name = gui_api.AskText("Dialog tour", "Style name", "Spring tee", "type a name")
print("AskText:", name.accepted, name.value)

# 3. Exactly one option.
size = gui_api.AskChoice("Dialog tour", "Base size", ["XS", "S", "M", "L", "XL"], 2)
print("AskChoice:", size.accepted, size.selectedIndex, size.selectedValue)

# 4. Any number of options. selectedIndices lists every checked option.
outputs = ["3D thumbnail", "2D thumbnail", "OBJ", "BOM"]
picked = gui_api.AskMultiChoice("Dialog tour", "Outputs to create", outputs, [0, 1])
print("AskMultiChoice:", picked.accepted, [outputs[i] for i in picked.selectedIndices])


# 5. A small form. Each field has a unique key; every value comes back as text.
def field(key, label, kind, default="", options=None):
    f = ApiTypes.CloApiGuiFormField()
    f.key = key
    f.label = label
    f.type = kind
    f.defaultValue = default
    f.options = options or []
    return f


form = gui_api.AskForm("Dialog tour - style details", [
    field("style", "Style number", ApiTypes.GUI_FIELD_TEXT, "ST-2401"),
    field("season", "Season", ApiTypes.GUI_FIELD_CHOICE, "FW", ["SS", "FW", "Resort"]),
    field("lined", "Lined", ApiTypes.GUI_FIELD_CHECK, "false"),
    field("price", "Target price", ApiTypes.GUI_FIELD_NUMBER, "29.9"),
])
if form.accepted:
    values = dict(form.values)
    print("AskForm:", values)
    gui_api.AskConfirm("Dialog tour", "Saved %s (%s, %s)" % (values["style"], values["season"], values["price"]), "OK", "Close")
