using System;
using UnityEngine;
using UnityEngine.UIElements;
namespace GrassCutter
{
///
/// T on anything built: its upgrade level and what the next one does and costs, its mode if it is a splitter or a merger,
/// and its switch if it has one (a belt stopped, a machine paused). Everything here is for the whole crew, paid from the wallet.
///
public class PieceScreen
{
public VisualElement Card { get; }
readonly BuildPiece _p;
readonly VisualElement _body;
readonly Action _close;
public PieceScreen(VisualElement layer, Action close, BuildPiece piece)
{
_p = piece;
_close = close;
Card = UIK.El("card", layer);
Card.pickingMode = PickingMode.Position;
Card.style.width = 680;
var header = UIK.El("card-header", Card);
UIK.Text(piece.Def.name.ToUpper(Loc.Culture), "t-h1 t-dark", header);
UIK.Btn("X", close, "btn btn-red btn-small", header);
_body = UIK.El(null, Card);
Refresh();
// the card follows the piece while it is up; the wallet only recolours the price
int was = Stamp();
Card.schedule.Execute(() =>
{
if (!_p || !_p.IsSpawned) { _close(); return; }
int now = Stamp();
if (now != was) { was = now; Refresh(); }
}).Every(200);
UIK.FollowWallet(Card);
}
int Stamp() => !_p ? 0 : _p.Setting;
void Refresh()
{
_body.Clear();
var ts = TeamState.Instance;
if (!_p || !ts) return;
var kind = _p.Upgrades;
int tier = _p.Tier.Value;
if (kind != UpgradeKind.None)
{
var row = UIK.El("tune-row", _body);
var names = UIK.El("tune-names", row);
UIK.Text($"{GameDefs.TierName(tier)}", "t-small t-dark", names, false);
UIK.Text(tier == 0 ? Loc.T("As built.") : Loc.F("Now {0}.", GameDefs.TierEffect(_p.piece, kind, tier)), "tune-effect", names, false);
if (tier < GameDefs.MaxTier)
{
double price = GameDefs.UpgradePrice(_p, tier);
var up = UIK.El("tune-row", _body);
var upNames = UIK.El("tune-names", up);
UIK.Text(Loc.F("Upgrade to {0}", GameDefs.TierName(tier + 1)), "t-small t-dark", upNames, false);
UIK.Text(Loc.F("{0}.", Cap(GameDefs.TierEffect(_p.piece, kind, tier + 1))), "tune-effect", upNames, false);
UIK.Costs(UIK.Btn(UIK.Money(price), () =>
{
if (!UIK.CanPay(price)) { Sfx.Play("error", 0.7f); return; }
_p.UpgradeRpc();
Sfx.Play("cash_register", 0.9f);
}, "btn btn-small", up), price);
}
else UIK.Text(Loc.T("Fully upgraded."), "tune-effect", _body, false);
}
var junction = _p.GetComponentInChildren();
if (junction != null)
{
var row = UIK.El("tune-row", _body);
var names = UIK.El("tune-names", row);
UIK.Text(Loc.T("Mode"), "t-small t-dark", names, false);
UIK.Text(Cap(junction.ModeName(_p.Mode.Value)), "tune-effect", names, false);
UIK.Btn(Loc.T("CHANGE"), () => { _p.NextModeRpc(junction.ModeCount); Sfx.Play("ui_click", 0.7f); }, "btn btn-blue btn-small", row);
}
string switchName = SwitchName();
if (switchName != null)
{
var row = UIK.El("tune-row", _body);
var names = UIK.El("tune-names", row);
UIK.Text(switchName, "t-small t-dark", names, false);
UIK.Text(_p.Switched ? SwitchOff() : _p.Upgrades == UpgradeKind.Silo ? Loc.T("Open: bundles go out to the belts.") : Loc.T("Running."), "tune-effect", names, false);
UIK.Btn(_p.Switched ? (_p.Upgrades == UpgradeKind.Silo ? Loc.T("OPEN") : Loc.T("START")) : (_p.Upgrades == UpgradeKind.Silo ? Loc.T("SHUT") : Loc.T("STOP")), () => { _p.SwitchRpc(); Sfx.Play("ui_click", 0.7f); }, (_p.Switched ? "btn btn-green" : "btn btn-red") + " btn-small", row);
}
if (kind == UpgradeKind.None && junction == null && switchName == null)
UIK.Text(Loc.T("Nothing to set on this one."), "tune-effect", _body, false);
UIK.Text(Loc.T("Upgrades are for the whole crew and come out of the shared wallet."), "t-small t-muted wrap", _body, false).style.marginTop = 10;
}
string SwitchName()
{
switch (_p.Upgrades)
{
case UpgradeKind.Belt: return Loc.T("Belt");
case UpgradeKind.Machine: return Loc.T("Machine");
case UpgradeKind.Robot: return _p.GetComponent() ? Loc.T("Robot") : null;
case UpgradeKind.Silo: return Loc.T("Hatches");
default: return null;
}
}
string SwitchOff()
{
switch (_p.Upgrades)
{
case UpgradeKind.Belt: return Loc.T("Stopped: what's on it waits where it is.");
case UpgradeKind.Robot: return Loc.T("Stopped: it waits where it is.");
case UpgradeKind.Silo: return Loc.T("Shut: everything stays in the silo.");
default: return Loc.T("Paused: it takes grass in but doesn't work it.");
}
}
static string Cap(string s) => string.IsNullOrEmpty(s) ? s : char.ToUpper(s[0], Loc.Culture) + s.Substring(1);
}
}