# -*- coding: utf-8 -*-
"""Builds every PayGo merchant + customer screen."""
from paygo_ui import *

NAV = {}            # active-state -> main component id


# ==========================================================================
# M-0 · Idle / standby
# ==========================================================================
def m0():
    s = screen(0, "M-0 · Idle", "Standby screen saver",
               "Customer-facing ambient state; tap anywhere to wake into amount entry.",
               fills=[{"type": "linearGradient",
                       "stops": [BLUE_DARK, BLUE_DEEP, BLUE],
                       "from": [0.1, 0], "to": [0.9, 1]}])
    # decorative rings, clipped by the artboard
    create("ellipse", parent=s, name="Ring A", x=255, y=-60, width=210, height=210,
           props={"fill": "none", "stroke": "#FFFFFF29", "strokeWeight": 22,
                  "absolute": True})
    create("ellipse", parent=s, name="Ring B", x=-70, y=640, width=190, height=190,
           props={"fill": "none", "stroke": "#FFFFFF24", "strokeWeight": 20,
                  "absolute": True})
    create("ellipse", parent=s, name="Ring C", x=300, y=700, width=150, height=150,
           props={"fill": "#FFFFFF14", "stroke": "none", "absolute": True})

    sb = status_bar(s, "12:00", right="100%", dark=True)
    nset(sb, absolute=True, x=0, y=0)

    core = col(s, "Standby", gap=0, fill_w=True, fill_v=True, hug_v=False,
               main="center", cross="center", padding=[0, 28, 0, 28])
    lcard = col(core, "Logo card", gap=0, padding=[22, 30, 22, 30], fill=PAPER,
                radius=20, main="center", cross="center",
                effects=[{"type": "dropShadow", "color": "#04507F40",
                          "offset": [0, 14], "radius": 34, "spread": 0,
                          "visible": True}])
    logo(lcard, 190)
    gap1 = col(core, "Caption", gap=8, fill_w=True, cross="center")
    nset(gap1, paddingTop=30)
    txt(gap1, "Pay & Go Your Way", 21, "SemiBold", PAPER, align="center",
        name="Tagline")
    txt(gap1, "Secure payments by PayWise", 12, "Regular", "#FFFFFFB8",
        align="center", name="Sub")

    hint = row(s, "Hint", gap=8, padding=[0, 0, 44, 0], fill_w=True, main="center",
               cross="center")
    nset(hint, absolute=True, x=0, y=760, width=SCREEN_W)
    icon(hint, "nfc", 15, "#FFFFFFB8")
    txt(hint, "Double-tap screen to wake up", 11.5, "SemiBold", "#FFFFFFB8",
        tracking="5%", name="Hint")
    return s


# ==========================================================================
# M-1 · Home / amount entry
# ==========================================================================
def m1():
    s = screen(1, "M-1 · Home", "Instant amount entry",
               "No login. Prominent keypad, then Tap & Pay, QR, or payment link.")
    status_bar(s, "9:41")
    app_header(s, title="Global Trade Co. · Kiosk 1",
               sub="Tree City Mall · Branch #3", right_pill="TTD")

    b = body(s, gap=14, padding=[14, PAD, 14, PAD])
    amount_display(b, "$150.00 TTD", "Enter payment amount")

    kp = col(b, "Keypad", gap=10, fill_w=True)
    nset(kp, layout="grid", gridColumns=3,
         columnTracks=["1fr", "1fr", "1fr"], gap=10, crossGap=10)
    keys = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "CLR", "0", "00"]
    for k in keys:
        soft = k in ("CLR", "00")
        key = row(kp, "Key · " + k, gap=0, height=56, fill=PAPER, radius=R_BTN,
                  stroke=LINE, main="center", cross="center", hug_v=False)
        nset(key, sizingH="fill", sizingV="fixed", height=56)
        txt(key, k, 15 if soft else 19, "SemiBold" if soft else "Bold",
            MUTED if soft else INK, name="Label")

    acts = col(b, "Actions", gap=10, fill_w=True)
    button(acts, "Tap & Pay", "green", "nfc")
    button(acts, "Generate QR Code", "blue", "qr")
    link_button(acts, "Send Payment Link")
    spacer(b)

    i = instance(NAV["Home"][0], NAV["Home"][1], parent=s)
    nset(i, sizingH="fill")
    return s


# ==========================================================================
# M-2 · Dynamic QR
# ==========================================================================
def m2():
    s = screen(2, "M-2 · QR", "Dynamic QR presentation",
               "Business-account QR for open camera scanning; supports a secondary customer screen.")
    status_bar(s, "9:42")
    app_header(s, back="Back to Entry", right_pill="Merchant")

    b = body(s, gap=12, padding=[14, PAD, 18, PAD])
    amount_display(b, "$150.00 TTD", "Amount to be paid", size=24)

    ring = col(b, "QR · pulse ring", gap=0, padding=7, radius=22,
               stroke=GREEN, stroke_weight=3, fill_w=True, cross="center")
    qcard = col(ring, "QR card", gap=12, padding=[16, 16, 14, 16], fill=PAPER,
                radius=16, fill_w=True, cross="center", stroke=LINE)
    qr_code(qcard, 212)
    sl = row(qcard, "Status line", gap=8, cross="center")
    create("ellipse", parent=sl, name="Dot", width=9, height=9,
           props={"fill": GREEN})
    txt(sl, "Gateway listening for payment", 11.5, "Bold", GREEN_DEEP,
        name="Status")

    cap = txt(b, "Business account QR code", 11, "Bold", MUTED, tracking="10%",
              case="upper", align="center", name="Caption")
    nset(cap, sizingH="fill")
    c = card(b, "Secondary screen", padding=[11, 14, 11, 14], gap=0)
    meta(c, "Secondary customer screen:", "Connected · displaying QR", size=12)

    spacer(b)
    acts = col(b, "Actions", gap=10, fill_w=True)
    button(acts, "Share Payment Link", "blue", "share")
    button(acts, "Cancel Transaction", "danger", "x")
    return s


# ==========================================================================
# M-3a · Payment successful
# ==========================================================================
def outcome(index, code, title, desc, kind):
    ok = kind == "success"
    s = screen(index, code, title, desc)
    status_bar(s, "14:32" if ok else "11:08")

    ban = col(s, "Banner · " + ("Success" if ok else "Declined"), gap=10,
              padding=[30, 20, 30, 20], fill=GREEN_DEEP if ok else DANGER,
              fill_w=True, cross="center")
    circ = row(ban, "Icon badge", gap=0, width=64, height=64, radius=999,
               fill="#FFFFFF38", main="center", cross="center")
    nset(circ, sizingH="fixed", sizingV="fixed")
    icon(circ, "check" if ok else "x", 34, PAPER)
    txt(ban, "PAYMENT SUCCESSFUL" if ok else "PAYMENT DECLINED", 19,
        "ExtraBold", PAPER, align="center", tracking="2%", name="Headline")
    if not ok:
        txt(ban, "Declined by card issuer", 12.5, "Regular", "#FFFFFFE0",
            align="center", name="Reason")

    b = body(s, gap=14, padding=[18, PAD, 18, PAD])
    if ok:
        amount_display(b, "$150.00 TTD", "Received from VISA ···· 4242",
                       caption_case="original", caption_size=12)
    else:
        amount_display(b, "$220.00 TTD", "Not charged · ask for another card",
                       caption_case="original", caption_size=12)

    c = card(b, "Transaction detail", gap=4)
    if ok:
        meta(c, "Ref #:", "TXN-998204112")
        meta(c, "Time:", "14:32:05 TST")
        meta(c, "Fee allocation:", "Merchant pays")
    else:
        meta(c, "Ref #:", "TXN-9980")
        meta(c, "Reason:", "3DS gateway auth timeout")

    if ok:
        chime = card(b, "Audio feedback", padding=[12, 14, 12, 14], gap=10,
                     horizontal=True, cross="center")
        icon(chime, "mic", 21, GREEN_DEEP)
        t = txt(chime, "Chime played: “Payment of $150.00 TTD received.”",
                12, "Regular", INK, name="Chime")
        nset(t, sizingH="fill", textAutoResize="height")

    spacer(b)
    acts = col(b, "Actions", gap=10, fill_w=True)
    if ok:
        button(acts, "Show / Share Receipt", "green", "share")
    else:
        button(acts, "Try Again", "blue")
    button(acts, "Return to Home Screen", "ghost")
    return s


# ==========================================================================
# M-4 · Transaction history
# ==========================================================================
def m4():
    s = screen(5, "M-4 · History", "Transaction history",
               "Searchable log, status badges, and re-issuable receipts.")
    status_bar(s, "15:00")
    app_header(s, title="Transaction History", right_icon="search")

    b = col(s, "Content", gap=0, padding=0, fill_w=True, fill_v=True, hug_v=False)

    def group(label):
        g = row(b, "Section · " + label, gap=0, padding=[16, PAD, 8, PAD],
                fill_w=True)
        txt(g, label, 11, "Bold", BLUE_DEEP, tracking="12%", case="upper",
            name="Label")

    group("Today · Nov 9, 2026")
    txn_row(b, "TXN-998204112", "+ $150.00 TTD",
            "14:32 · Tap & Pay · VISA 4242", "SUCCESS")
    txn_row(b, "TXN-998204111", "+ $45.50 TTD",
            "13:15 · QR Scan · Anonymous", "SUCCESS")
    txn_row(b, "TXN-9980", "$220.00 TTD", "11:05 · 3DS timeout",
            "DECLINED", "declined")
    group("Yesterday · Nov 8, 2026")
    txn_row(b, "TXN-9979", "+ $89.00 TTD", "16:45 · QR Scan", "SUCCESS")
    txn_row(b, "TXN-9978", "+ $12.00 TTD", "09:20 · Payment link", "SUCCESS",
            last=True)
    spacer(b)

    i = instance(NAV["Transactions"][0], NAV["Transactions"][1], parent=s)
    nset(i, sizingH="fill")
    return s


# ==========================================================================
# M-5 · Settings
# ==========================================================================
def m5():
    s = screen(6, "M-5 · Settings", "Device profile & settings",
               "Read-only business info, audio toggle, fee allocation and key pairing.")
    status_bar(s, "10:12")
    app_header(s, title="Merchant Settings", right_pill="Read-only")

    b = body(s, gap=10, padding=[16, PAD, 16, PAD])
    section_label(b, "Merchant profile")
    c = card(b, "Profile", gap=3)
    t = txt(c, "Global Trade Co. (GTC)", 14, "SemiBold", INK, name="Business")
    nset(t, sizingH="fill")
    for line in ("Branch 3 · Kiosk 1", "Tree City Mall", "register.paywise.co"):
        t = txt(c, line, 13, "Regular", MUTED, name=line)
        nset(t, sizingH="fill")

    section_label(b, "App configuration")
    tr = card(b, "Audio chime", padding=[11, 15, 11, 15], horizontal=True,
              main="spaceBetween")
    txt(tr, "Audio notification chime", 13.5, "Regular", INK, name="Label")
    toggle(tr, True)
    c2 = card(b, "Fee allocation", padding=[12, 15, 12, 15], gap=0)
    meta(c2, "Fee allocation:", "Merchant pays")

    section_label(b, "Device provisioning")
    c3 = card(b, "Device", gap=4)
    meta(c3, "Device ID:", "DEV-8849-GTC")
    meta(c3, "Status:", "BOUND & ACTIVE")

    spacer(b)
    button(b, "Pair New Device Key", "blue")

    i = instance(NAV["Settings"][0], NAV["Settings"][1], parent=s)
    nset(i, sizingH="fill")
    return s


# ==========================================================================
# M-6 · Pair device
# ==========================================================================
def m6():
    s = screen(7, "M-6 · Provisioning", "Pair new device key",
               "One-time binding generated from the PayWise portal; daily use stays login-free.")
    status_bar(s, "10:13")
    app_header(s, back="Settings")

    b = body(s, gap=14, padding=[18, PAD, 20, PAD])
    t = txt(b, "Pair this terminal to a business account. Keys are generated in "
               "the PayWise web portal.", 13.5, "Regular", MUTED,
            line_height="150%", name="Intro")
    nset(t, sizingH="fill", textAutoResize="height")

    field(b, "Device ID", "DEV-8849-GTC")
    field(b, "Device Key", "· · · · · · · · · ·", muted_value=True)

    c = card(b, "Security note", padding=[12, 14, 12, 14], gap=0, fill=TINT,
             border=TINT_LINE)
    meta(c, "Security:", "staff devices stay read-only. Bank details are "
                         "managed in the portal.", size=12,
         label_color=BLUE_DARK)

    spacer(b)
    button(b, "Bind This Device", "green")
    return s


# ==========================================================================
# C-1 · Express registration
# ==========================================================================
def c1():
    s = screen(0, "C-1 · Onboarding", "Express registration",
               "Minimal setup — enough for receipts and future one-tap checkout.")
    status_bar(s, "16:20", right="100%")
    app_header(s, title="PayGo Customer Setup")

    b = body(s, gap=14, padding=[18, PAD, 20, PAD])
    head = col(b, "Welcome", gap=4, fill_w=True, cross="center")
    logo(head, 132)
    sp = col(head, "Spacer", gap=4, fill_w=True, cross="center")
    nset(sp, paddingTop=10)
    txt(sp, "Welcome to PayGo", 19, "ExtraBold", INK, align="center",
        name="Welcome")
    txt(sp, "Fast & frictionless payments", 12.5, "Regular", MUTED,
        align="center", name="Sub")

    field(b, "Full name", "Jane Doe")
    field(b, "Email address", "jane.doe@example.com")
    field(b, "Phone number", "+1 (868) 555-0199")

    joined = col(b, "Joined", gap=0, fill_w=True)
    meta(joined, "Date joined:", "Nov 9, 2026 · auto-set", size=12.5,
         color=MUTED)

    chk = row(b, "Biometric consent", gap=11, fill_w=True, cross="min")
    checkbox(chk, True)
    t = txt(chk, "Enable biometric authorization for faster checkout", 13,
            "Regular", "#243447", name="Consent")
    nset(t, sizingH="fill", textAutoResize="height")

    spacer(b)
    button(b, "Complete Registration", "green")
    return s


# ==========================================================================
# C-2 · Checkout + biometric
# ==========================================================================
def c2():
    s = screen(1, "C-2 · Checkout", "Biometric authorization",
               "Saved tokenized cards plus Touch ID — no PIN, no OTP at the terminal.")
    status_bar(s, "14:31", right="100%")
    app_header(s, title="PayGo Checkout")

    b = body(s, gap=11, padding=[14, PAD, 16, PAD])
    mc = card(b, "Merchant", padding=[11, 14, 11, 14], gap=0)
    meta(mc, "Global Trade Co.", "· Tree City Mall", size=12.5)
    amount_display(b, "$150.00 TTD", "Total amount due", size=28)

    section_label(b, "Saved tokenized card")
    sel = card(b, "Card · VISA (default)", padding=[12, 14, 12, 14],
               horizontal=True, main="spaceBetween", fill=TINT, border=TINT_LINE)
    left = row(sel, "Label", gap=6, cross="center")
    txt(left, "VISA ···· 4242", 13.5, "SemiBold", INK, name="Card")
    txt(left, "· Default", 11.5, "Regular", MUTED, name="Default")
    icon(sel, "check", 18, BLUE_DEEP)

    alt = card(b, "Card · Mastercard", padding=[12, 14, 12, 14],
               horizontal=True, main="spaceBetween")
    txt(alt, "Mastercard ···· 8819", 13.5, "Regular", MUTED, name="Card")
    icon(alt, "plus", 18, MUTED)

    button(b, "Add New Payment Card", "ghost", "card", align="min")

    rails = card(b, "Rails", padding=[11, 14, 11, 14], horizontal=True,
                 main="spaceAround")
    txt(rails, "Tap & Pay", 11.5, "Bold", BLUE_DEEP, name="Rail 1")
    txt(rails, "Scan QR", 11.5, "Regular", MUTED, name="Rail 2")
    txt(rails, "Payment Link", 11.5, "Regular", MUTED, name="Rail 3")

    bio = col(b, "Biometric prompt", gap=9, padding=[18, 16, 18, 16],
              fill=TINT_2, radius=R_CARD, stroke=TINT_LINE, fill_w=True,
              cross="center")
    icon(bio, "finger", 40, BLUE_DEEP)
    txt(bio, "Touch ID required", 13.5, "SemiBold", INK, align="center",
        name="Prompt")
    txt(bio, "Place finger on sensor to authorize", 12, "Regular", MUTED,
        align="center", name="Hint")

    spacer(b)
    button(b, "Authorize $150.00 TTD", "blue")
    return s


# ==========================================================================
# C-3 · Receipt & notes
# ==========================================================================
def c3():
    s = screen(2, "C-3 · Receipt", "Receipt & notes",
               "Automatic receipt capture plus optional text or voice notes.")
    status_bar(s, "14:33", right="100%")
    ban = col(s, "Banner · Completed", gap=0, padding=[16, 20, 16, 20],
              fill=GREEN_DEEP, fill_w=True, cross="center")
    txt(ban, "PAYMENT COMPLETED", 15, "ExtraBold", PAPER, tracking="4%",
        align="center", name="Headline")

    b = body(s, gap=12, padding=[16, PAD, 18, PAD])
    c = card(b, "Receipt", gap=4)
    meta(c, "Merchant:", "Global Trade Co.")
    meta(c, "Amount:", "$150.00 TTD")
    meta(c, "Date:", "Nov 9, 2026 · 14:32 TST")
    meta(c, "Card:", "VISA ···· 4242")

    section_label(b, "Expense tracking & notes")
    note = card(b, "Note input", padding=[13, 14, 13, 14], gap=0)
    txt(note, "Groceries for the week", 13, "Regular", INK, name="Note")

    button(b, "Record Voice Note Receipt", "ghost", "mic", align="min")

    tot = card(b, "Month total", padding=[12, 14, 12, 14], gap=0, fill=TINT,
               border=TINT_LINE)
    m = meta(tot, "November spending:", "$425.50 TTD", size=13,
             label_color=BLUE_DARK)
    nset(m, textAlign="center")

    spacer(b)
    button(b, "Save Receipt to Log", "green")
    return s


# ==========================================================================
# C-4 · Customer payment log
# ==========================================================================
def c4():
    s = screen(3, "C-4 · Log", "Customer payment log",
               "Every PayGo transaction across merchants, with receipts in one place.")
    status_bar(s, "17:05", right="100%")
    app_header(s, title="Payment History")

    b = col(s, "Content", gap=0, padding=0, fill_w=True, fill_v=True, hug_v=False)
    g = row(b, "Section · November 2026", gap=0, padding=[16, PAD, 8, PAD],
            fill_w=True)
    txt(g, "November 2026", 11, "Bold", BLUE_DEEP, tracking="12%",
        case="upper", name="Label")

    txn_row(b, "Global Trade Co.", "$150.00", "Nov 9 · VISA 4242", "COMPLETE")
    txn_row(b, "Belle's Bake Shop", "$48.75", "Nov 7 · VISA 4242", "COMPLETE")
    txn_row(b, "S&S Pharmacy", "$112.50", "Nov 3 · VISA 4242", "COMPLETE")
    txn_row(b, "KFC Independence Sq.", "$114.25", "Nov 1 · VISA 4242",
            "COMPLETE", last=True)

    spacer(b)
    tot = row(b, "Month total", gap=10, padding=[14, PAD, 10, PAD],
              fill_w=True, main="spaceBetween", cross="center")
    txt(tot, "November spending", 12.5, "Regular", MUTED, name="Label")
    txt(tot, "$425.50 TTD", 15, "ExtraBold", INK, name="Total")
    foot = col(b, "Actions", gap=0, padding=[0, PAD, 20, PAD], fill_w=True)
    button(foot, "View Receipt", "ghost", "receipt")
    return s
