package com.ruoyi.app.pay;

import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/** Self-contained redirect page; no separately deployed server page is required. */
@Component
public class LinePayReturnPageRenderer {

    public String render(String ddId) {
        return render(ddId, "");
    }

    public String render(String ddId, String nonce) {
        String encoded = URLEncoder.encode(ddId == null ? "" : ddId, StandardCharsets.UTF_8)
                .replace("+", "%20");
        String deepLink = "com.twanmsdyh.app://payment/result?orderId=" + encoded;
        return "<!doctype html><html><head><meta charset=\"utf-8\">"
                + "<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">"
                + "<title>LINE Pay</title><style>body{font-family:system-ui,sans-serif;margin:0;"
                + "background:#f5f7fa;color:#202124}.card{max-width:420px;margin:12vh auto;padding:32px;"
                + "background:#fff;border-radius:16px;box-shadow:0 8px 30px #00000012;text-align:center}"
                + "a{display:inline-block;margin-top:18px;padding:12px 22px;border-radius:8px;"
                + "background:#06c755;color:#fff;text-decoration:none}</style></head><body>"
                + "<main class=\"card\"><h1>Payment is being confirmed</h1>"
                + "<p>请返回 App 查询最终支付结果，请勿重复付款。</p>"
                + "<a id=\"open-app\" href=\"" + deepLink + "\">Open app</a></main>"
                + "<script nonce=\"" + nonce + "\">setTimeout(function(){location.href=document.getElementById('open-app').href},80)</script>"
                + "</body></html>";
    }
}
