using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using UnityEngine.UI;
namespace TKS.OutGame
{
///
/// 기기 우편함(2026-11-20 · 임무 메뉴 요청서 §0-6 「우편 최소 기능」 · 목업 07_pop_mail) — 로비 「우편」(기기 세이브 모드) → 목록 · 받기.
/// 디자인(PD 2026-11-20 「너무 단순 · 글자가 작아 불편 · 우편 보상은 무조건 받기만 · 삼국지 세계관 팝업」 · Fable 기획 `mission_ui/design/디자인기획_r2.md` §A)
/// = 「본영에서 내려온 포상 조서 두루마리」: 두루마리 판(mail_scroll 9 분할) · 축 위 제목 · 왼쪽 위 봉랍 서신 장식(+ 2통부터 통 수 배지) ·
/// 통마다 블록(제목 38 · 붉은 괘선 · 본문 30 · 보상 받침 + 등급 틀 칸 112) · 아래 「받기」 하나(= 보이는 우편 전부) + 본영 인장 · 닫기 X = 틀 그대로.
/// 열 때 두루마리 펼침(PD 2026-11-21 · UiUnroll · 표 mail_unroll) = 두 축만 보이는 닫힘 → 위아래로 벌어지며 종이가 가운데부터 드러남.
/// 우편 = SaveData.mail(임무 초기화 때 받지 않은 구간 보상) · 글 = mission_text 의 mail_<kind>_title/body · 좌표 · 색 · 그림 = mission_screen(C45).
/// 서버 모드는 기존 서버 우편함(Net/MailPanel).
///
public static class MailBox
{
public static string Title(MailEntry m)
{
return MissionScreen.T("mail_" + m.kind + "_title");
}
public static string Body(MailEntry m)
{
string date = m.args.Count > 0 ? m.args[0] : "";
DateTime_Parts(date, out var mo, out var dd);
var needs = new List();
if (m.args.Count > 1)
foreach (var n in m.args[1].Split(';'))
if (n.Length > 0) needs.Add(MissionScreen.F("mail_need", n));
return MissionScreen.F("mail_" + m.kind + "_body", mo, dd, string.Join(" · ", needs.ToArray()));
}
static void DateTime_Parts(string yyyymmdd, out string month, out string day)
{
month = day = "";
if (yyyymmdd == null || yyyymmdd.Length != 8) return;
int mo, dd;
if (int.TryParse(yyyymmdd.Substring(4, 2), NumberStyles.Integer, CultureInfo.InvariantCulture, out mo)) month = mo.ToString(CultureInfo.InvariantCulture);
if (int.TryParse(yyyymmdd.Substring(6, 2), NumberStyles.Integer, CultureInfo.InvariantCulture, out dd)) day = dd.ToString(CultureInfo.InvariantCulture);
}
static float[] V(string key, params float[] def)
{
return MissionScreen.V(key, def);
}
static Color C(string key, Color def)
{
return MissionScreen.Col(key, def);
}
public static void Open(OutGameRoot root)
{
Open(root, true);
}
/// unroll = 두루마리 펼침 연출(처음 열 때 · 받은 뒤 다시 열 때는 펼친 채).
public static void Open(OutGameRoot root, bool unroll)
{
UiText.Use(MissionScreen.Texts);
var s = root.Session;
var items = s.Save.mail != null ? s.Save.mail.items : new List();
var ids = new List();
foreach (var m in items) ids.Add(m.id);
int n = items.Count;
var mp = V("mail_panel", 1000f, 920f, 1500f);
var blk = V("mail_block", 434f, 18f);
int shown = Mathf.Max(1, n);
float H = Mathf.Clamp(V("mail_fixed_h", 476f)[0] + shown * blk[0] + (shown - 1) * blk[1], mp[1], mp[2]);
// 버튼 0개 = 팝업 틀이 아래 버튼 줄을 만들지 않음(「받기」 하나는 아래에서 직접 · PD 「우편 보상은 무조건 받기만」)
var content = root.ModalCustom(MissionScreen.T("mail_title"), H);
var panel = (RectTransform)content.parent;
panel.sizeDelta = new Vector2(mp[0], H);
Scroll(panel);
// 종이 = 판 가운데 고정 크기(H) · Reveal(RectMask2D) = 두 축 안쪽만 보임 → 펼칠 때 글이 움직이지 않고 가운데부터 드러남
float rodT, rodB;
Rods(panel, out rodT, out rodB);
var paper = Paper(panel, mp[0], H, rodT, rodB);
if (unroll) Unroll(panel, H, rodT, rodB);
// 서신 장식(로비 우편 아이콘) + 통 수 배지(2통부터)
var dc = V("mail_deco", 34f, -40f, 136f);
var deco = UiKit.Icon(panel, MissionScreen.Cfg("mail_deco_icon"), new Vector2(0f, 1f), new Vector2(0.5f, 0.5f), new Vector2(dc[0] + dc[2] * 0.5f, -(dc[1] + dc[2] * 0.5f)), dc[2]);
if (deco != null)
{
deco.name = "Deco";
UiKit.DropShadow(deco, 3f);
if (n >= 2)
{
var bg = V("mail_badge", 104f, -6f, 44f, 26f);
var b = UiKit.Skin(UiKit.Rect("Badge", deco.transform, new Vector2(0f, 1f), new Vector2(0.5f, 0.5f), new Vector2(bg[0], -bg[1]), new Vector2(bg[2], bg[2])), "badge_circle", C("badge", UiKit.Red));
b.preserveAspect = true;
UiKit.Title(UiKit.FillLabel(b.transform, n.ToString(CultureInfo.InvariantCulture), (int)bg[3], UiKit.White), true);
}
}
var pa = V("mail_paper", 140f, 190f, 720f, 306f);
if (n == 0)
{
var e = UiKit.Label(paper, MissionScreen.T("mail_empty"), 32, C("mail_sub", UiKit.InkSoft), TextAnchor.MiddleCenter, new Vector2(0f, 1f), new Vector2(0f, 1f), new Vector2(pa[0], -pa[1]), new Vector2(pa[2], H - pa[1] - pa[3]), false);
e.name = "Empty";
return;
}
RectTransform list = paper;
bool scroll = n >= 3;
if (scroll)
{
ScrollRect sr;
list = UiKit.ScrollGrid("Mails", paper, pa[0], mp[0] - pa[0] - pa[2], pa[1], pa[3], new Vector2(pa[2], blk[0]), new Vector2(0f, blk[1]), 1, out sr);
}
for (int i = 0; i < n; i++)
{
RectTransform cell;
if (scroll)
{
var go = new GameObject("Mail_" + items[i].id, typeof(RectTransform));
go.transform.SetParent(list, false);
cell = (RectTransform)go.transform;
}
else cell = UiKit.At(paper, "Mail_" + items[i].id, pa[0], pa[1] + i * (blk[0] + blk[1]), pa[2], blk[0]);
Block(cell, items[i], pa[2], i > 0);
}
// 「받기」 하나 = 보이는 우편 전부(mail_claim_mode all) · 팩 노랑 버튼 그대로
var bt = V("mail_btn", 420f, 112f, 262f, 44f);
var claim = UiKit.SkinBtn(paper, "MailClaim", UISkin.Has(MissionScreen.Cfg("mail_btn_skin", "btn_l_yellow")) ? MissionScreen.Cfg("mail_btn_skin", "btn_l_yellow") : "button", UiKit.White, MissionScreen.T("mail_claim"), (int)bt[3],
UiKit.Col("text_on_yellow", UiKit.Ink), (mp[0] - bt[0]) * 0.5f, H - bt[2], bt[0], bt[1], () => Claim(root, MissionScreen.Cfg("mail_claim_mode", "all") == "one" ? new List { ids[0] } : ids));
claim.name = "MailClaim";
Seal(paper, H);
}
/// 9 분할 판의 윗 축 · 아랫 축 높이(UI 단위 = 테두리 px ÷ 판 pixelsPerUnit) · 판 그림이 없으면 0.
static void Rods(RectTransform panel, out float top, out float bottom)
{
top = bottom = 0f;
var img = panel.GetComponent();
if (img == null || img.sprite == null || img.type != Image.Type.Sliced) return;
float ppu = Mathf.Max(0.0001f, img.pixelsPerUnit);
top = img.sprite.border.w / ppu;
bottom = img.sprite.border.y / ppu;
}
/// Reveal(두 축 안쪽 · RectMask2D) 안에 판 가운데 기준 고정 크기 종이 — 좌표계는 판과 같음(왼쪽 위 기준 At).
static RectTransform Paper(RectTransform panel, float w, float h, float top, float bottom)
{
var reveal = UiKit.Fill("Reveal", panel, 0f, 0f, top, bottom);
reveal.gameObject.AddComponent();
var go = new GameObject("Paper", typeof(RectTransform));
go.transform.SetParent(reveal, false);
var rt = (RectTransform)go.transform;
rt.anchorMin = rt.anchorMax = rt.pivot = new Vector2(0.5f, 0.5f);
rt.sizeDelta = new Vector2(w, h);
rt.anchoredPosition = new Vector2(0f, (top - bottom) * 0.5f); // Reveal 가운데 ≠ 판 가운데(두 축 높이 차의 반)
return rt;
}
/// 두루마리 펼침(표 mail_unroll = 켬 | 닫힌 채 초 | 펼침 초 · 닫힘 높이 = 두 축 · 판 그림이 없으면 열림 높이의 0.2).
static void Unroll(RectTransform panel, float H, float top, float bottom)
{
var u = V("mail_unroll", 1f, 0.06f, 0.42f);
if (u[0] <= 0f) return;
float closed = top + bottom > 0f ? top + bottom : H * 0.2f;
UiUnroll.Play(panel, closed, H, u[1], u[2]);
}
/// 두루마리 판(테마 아이콘 mail_scroll · 9 분할) · 제목 띠 = 투명(글자만 축 위) · 닫기 X 자리.
static void Scroll(RectTransform panel)
{
var sp = UISkin.Icon(MissionScreen.Cfg("mail_skin"));
var img = panel.GetComponent();
if (sp != null && img != null)
{
img.sprite = sp;
img.type = sp.border.sqrMagnitude > 0f ? Image.Type.Sliced : Image.Type.Simple;
img.color = Color.white;
}
var mt = V("mail_title", 84f, 520f, 44f);
var head = panel.Find("Head") as RectTransform;
if (head != null)
{
var hi = head.GetComponent();
if (hi != null && sp != null) hi.color = Color.clear; // 파란 제목 띠 폐지(축 위 글자만)
else if (hi != null) hi.color = C("pop_mail", UiKit.Header);
head.anchoredPosition = new Vector2(head.anchoredPosition.x, head.anchoredPosition.y - (mt[0] - (6f + head.rect.height * 0.5f)));
var t = head.GetComponentInChildren();
if (t != null)
{
t.resizeTextMaxSize = (int)mt[2];
foreach (var o in t.GetComponents()) o.effectColor = C("mail_title_line", o.effectColor);
}
}
var x = panel.Find("Close") as RectTransform;
var xp = V("mail_x", 1f, -58f, -84f);
if (x != null) x.anchoredPosition = new Vector2(xp[1], xp[2]);
}
/// 우편 한 통(제목 · 붉은 괘선 · 본문 · 보상 받침 + 등급 틀 칸 · 2통째부터 위 구분 선).
static void Block(RectTransform cell, MailEntry mail, float w, bool divider)
{
var tc = C("text", UiKit.Ink);
var hd = V("mail_head", 52f, 38f);
if (divider) Solid(cell, "Div", C("mail_div", Color.gray), 0f, -V("mail_block", 434f, 18f)[1] * 0.5f - 1f, w, 2f);
var t = UiKit.Title(UiKit.Label(cell, Title(mail), (int)hd[1], tc, TextAnchor.MiddleLeft, new Vector2(0f, 1f), new Vector2(0f, 1f), Vector2.zero, new Vector2(w, hd[0])), false);
t.name = "Title";
var rl = V("mail_rule", 62f, 3f);
Solid(cell, "Rule", C("mail_rule", UiKit.Red), 0f, rl[0], w, rl[1]);
var bd = V("mail_body", 80f, 132f, 30f, 1.25f);
var b = UiKit.Label(cell, Body(mail), (int)bd[2], tc, TextAnchor.UpperLeft, new Vector2(0f, 1f), new Vector2(0f, 1f), new Vector2(0f, -bd[0]), new Vector2(w, bd[1]), false);
b.name = "Body";
b.lineSpacing = bd[3];
b.horizontalOverflow = HorizontalWrapMode.Wrap;
var tr = V("mail_tray", 228f, 190f);
var tray = UiKit.At(cell, "Tray", 0f, tr[0], w, tr[1]);
UiKit.Skin(tray, "soft", C("mail_tray", UiKit.Paper));
var mc = V("mail_cell", 112f, 24f, 14f, 28f);
MissionScreen.RewardRow(tray, mail.reward, mc[2], mc[0], true, tc);
}
/// 본영 인장(Resources 그림 + 글자 · 회전) — 그림 경로가 비면 없음.
static void Seal(RectTransform panel, float H)
{
var sp = MissionScreen.Res(MissionScreen.Cfg("mail_seal_sprite"));
if (sp == null) return;
var se = V("mail_seal", 744f, 256f, 104f, -8f, 30f);
var img = UiKit.At(panel, "Seal", se[0], H - se[1], se[2], se[2]).gameObject.AddComponent();
img.sprite = sp;
img.raycastTarget = false;
img.rectTransform.localEulerAngles = new Vector3(0f, 0f, se[3]);
var t = UiKit.Title(UiKit.FillLabel(img.transform, MissionScreen.T("mail_seal"), (int)se[4], C("mail_seal", UiKit.Red)));
t.name = "SealText";
}
static void Solid(RectTransform p, string name, Color c, float x, float y, float w, float h)
{
var img = UiKit.At(p, name, x, y, w, h).gameObject.AddComponent();
img.color = c;
img.raycastTarget = false;
}
static void Claim(OutGameRoot root, List ids)
{
var s = root.Session;
var got = MissionRules.ClaimMail(s.Db, s.Save, ids, s.Rng);
if (got.Count > 0) Sound.Play(SoundKeys.UiReward); // 소리: 우편 수령(2026-11-20 사운드)
if (got.Count == 0) return;
s.Commit();
root.Toast(MissionScreen.F("mail_got", got.Count.ToString(CultureInfo.InvariantCulture)));
bool empty = s.Save.mail == null || s.Save.mail.items.Count == 0;
if (empty && MissionScreen.V("mail_close_after", 1f)[0] > 0f) root.CloseModal(); // 전부 받았으면 닫음(빈 우편함을 다시 열지 않음)
else Open(root, false); // 남은 우편으로 다시 연다(펼친 채)
}
}
}