using System;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using UnityEngine.UI;
namespace TKS.OutGame
{
///
/// 임무 화면(2026-11-20 PD 컨펌 · 목업 미션_img 02~06) — 탭 3(일일 · 주간 · 업적 · 빨간 점 = 받기 줄 수) · 포인트 판(원 + 막대 + 구간 상자 + 초기화 시계) ·
/// 임무 줄(아이콘 · 제목 · 대체 표시 · 진행 막대 · 도움말 · +포인트 · 받기/완료/이동/진행 중) · 업적(분류 칩 · 지금 단계 1줄 · 연쇄 안내) · 모두 받기 · 보상 팝업.
/// 값 = 임무 표(MissionDb) · 글자 = UI/Mission/mission_text · 아이콘·색·바로가기 = UI/Mission/mission_screen(C45).
/// 캔버스 = 화면 Event(OutGameRoot.Add) · 줄 목록 = ScrollGrid Frame · 시계 칩 = 자기 Event 캔버스(1초마다 글자만).
///
public class MissionScreen : OutGameScreen
{
public const string Table = "UI/Mission/mission_screen";
public const string Texts = "UI/Mission/mission_text";
public const int DailyTab = 0, WeeklyTab = 1, AchieveTab = 2;
const float RowW = 1032f;
const float RowH = 100f;
const float AchH = 132f;
const float BottomH = 136f;
static Dictionary cfg;
int tab;
string cat = "";
RectTransform body;
RectTransform page;
readonly Button[] tabs = new Button[3];
readonly Text[] tabBadges = new Text[3];
/// 시험 · 캡처용 — 지금 탭 · 줄 상태(claim · done · go · wait) · 마지막 팝업 제목.
public int CurrentTab
{
get { return tab; }
}
public readonly List RowStates = new List();
public readonly List RowTitles = new List();
public string LastPopup { get; private set; }
public static string T(string key)
{
return UiText.T(key).Replace("\\n", "\n");
}
public static string F(string key, params string[] args)
{
return UiText.F(key, args).Replace("\\n", "\n");
}
/// 화면 표 값(없으면 def).
public static string Cfg(string key, string def = "")
{
if (cfg == null)
{
cfg = new Dictionary();
var rows = UiText.Rows(Table);
if (rows != null)
foreach (var r in rows)
{
string k, v;
if (r.TryGetValue("s_key", out k) && r.TryGetValue("s_value", out v) && k.Length > 0) cfg[k] = v;
}
}
string s;
return cfg.TryGetValue(key, out s) && s.Length > 0 ? s : def;
}
public static Color Col(string key, Color def)
{
Color c;
return ColorUtility.TryParseHtmlString(Cfg("color." + key), out c) ? c : def;
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void ResetStatics()
{
cfg = null;
}
public override void Build()
{
UiText.Use(Texts);
Header(T("mission_title"), T("mission_sub"), true);
body = Body();
string[] keys = { "tab_daily", "tab_weekly", "tab_achieve" };
for (int i = 0; i < 3; i++)
{
int k = i;
tabs[i] = UiKit.SubTab(body, "Tab" + i, T(keys[i]), 24f + i * 348f, 18f, 332f, 84f, () => SetTab(k));
var b = UiKit.Skin(UiKit.Rect("Badge", tabs[i].transform, new Vector2(1f, 1f), new Vector2(0.5f, 0.5f), new Vector2(-10f, -6f), new Vector2(48f, 50f)), "badge_circle", Col("badge", UiKit.Red));
b.preserveAspect = true;
tabBadges[i] = UiKit.Title(UiKit.FillLabel(b.transform, "", 26, UiKit.White), true);
}
page = UiKit.Fill("Page", body, 0f, 0f, 116f, 0f);
}
public override void OnShow()
{
S.MissionRoll();
}
public void SetTab(int t)
{
tab = Mathf.Clamp(t, 0, 2);
Refresh();
}
public void SetCategory(string c)
{
cat = c ?? "";
Refresh();
}
public override void Refresh()
{
if (page == null || S.Db.Missions == null) return;
var og = S.Db;
var d = S.Save;
int[] n = { MissionRules.Badge(og, d, MissionDb.Daily), MissionRules.Badge(og, d, MissionDb.Weekly), MissionRules.AchBadge(og, d) };
for (int i = 0; i < 3; i++)
{
UiKit.SetSubTab(tabs[i], i == tab);
tabBadges[i].text = n[i].ToString(CultureInfo.InvariantCulture);
tabBadges[i].transform.parent.gameObject.SetActive(n[i] > 0);
}
UiKit.Clear(page);
RowStates.Clear();
RowTitles.Clear();
if (tab == AchieveTab) BuildAchievements();
else BuildMissions(tab == DailyTab ? MissionDb.Daily : MissionDb.Weekly);
}
// ---------- 일일 · 주간 ----------
void BuildMissions(string kind)
{
var og = S.Db;
var db = og.Missions;
var d = S.Save;
int max = db.PointMax(kind);
int pts = MissionRules.Points(d, kind);
var panel = UiKit.At(page, "Points", 24f, 4f, RowW, 262f);
UiKit.Skin(panel, "card", Col("panel", UiKit.Dark));
Txt(panel, "Title", T("points_" + kind), 34, Col("text", UiKit.White), 30f, 16f, 360f, 50f, TextAnchor.MiddleLeft, true);
Timer(panel, kind);
// 원(지금 포인트 / 상한)
foreach (var ring in new[] { UiKit.Box("Ring", panel, new Vector2(0f, 1f), new Vector2(0.5f, 0.5f), new Vector2(105f, -160f), new Vector2(152f, 152f), Col("ring", UiKit.Coin)),
UiKit.Box("Inner", panel, new Vector2(0f, 1f), new Vector2(0.5f, 0.5f), new Vector2(105f, -160f), new Vector2(138f, 138f), Col("chip", UiKit.Dark)) })
{
ring.sprite = UiKit.Circle; // 평면 원(팩 dot 은 검은 외곽선이 있어 금테가 안 보임)
ring.type = Image.Type.Simple;
}
var pv = UiKit.Title(UiKit.Label(panel, pts.ToString(CultureInfo.InvariantCulture), 62, Col("text_gold", UiKit.Coin), TextAnchor.MiddleCenter, new Vector2(0f, 1f), new Vector2(0.5f, 0.5f), new Vector2(105f, -146f), new Vector2(140f, 70f)), true);
pv.name = "PointValue";
UiKit.Label(panel, F("point_max", max.ToString(CultureInfo.InvariantCulture)), 24, Col("text_dim", UiKit.InkSoft), TextAnchor.MiddleCenter, new Vector2(0f, 1f), new Vector2(0.5f, 0.5f), new Vector2(105f, -196f), new Vector2(140f, 34f));
// 막대 + 구간 상자
const float X0 = 216f, W = 760f, BarY = 150f;
var fill = UiKit.Bar("Bar", panel, new Vector2(0f, 1f), new Vector2(0f, 0.5f), new Vector2(X0, -BarY), new Vector2(W, 30f), Col("chip", UiKit.Dark), Col("bar_full", UiKit.Coin));
UiKit.SetBar(fill, max > 0 ? pts / (float)max : 0f);
var steps = db.Steps(kind);
var icons = Split(Cfg("box_icons"));
for (int i = 0; i < steps.Count; i++)
{
var s = steps[i];
float x = X0 + W * Mathf.Clamp01(s.need / (float)Mathf.Max(1, max));
bool got = MissionRules.BoxClaimed(d, kind, s.step);
bool ready = !got && pts >= s.need;
string icon = icons.Count == 0 ? "" : icons[Mathf.Min(icons.Count - 1, Mathf.RoundToInt(i * (icons.Count - 1) / (float)Mathf.Max(1, steps.Count - 1)))];
var box = UiKit.Rect("Box" + s.step, panel, new Vector2(0f, 1f), new Vector2(0.5f, 0.5f), new Vector2(x, -BarY + 4f), new Vector2(96f, 96f));
UiKit.Skin(box, "card", Col("chip", UiKit.Dark), true);
if (ready)
{
// 빛남 = 뒤 판만 깜빡임(UiPulse = 자기 Frame 캔버스 · 누름은 상자 판이 받음)
var glow = UiKit.Skin(UiKit.Fill("Glow", box, -6f, -6f, -6f, -6f), "card", Col("ring", UiKit.Coin));
glow.gameObject.AddComponent();
}
var bt = box.gameObject.AddComponent