using BrewMonster.Network;
using BrewMonster.Scripts;
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace BrewMonster.UI
{
///
/// Change warehouse password (C++ CDlgStorageChangePW / Win_InputString3).
/// Dialog id for : .
///
public class DlgStorageChangePW : AUIDialog
{
public const string DialogId = "DlgStorageChangePW";
const int StringIdPasswordMismatch = 254;
[SerializeField] private TMP_InputField oldPasswordInput;
[SerializeField] private TMP_InputField newPasswordInput;
[SerializeField] private TMP_InputField confirmPasswordInput;
[SerializeField] private Button confirmButton;
[SerializeField] private Button cancelButton;
[SerializeField] private Button closeButton;
public static DlgStorageChangePW GetDialog()
{
return EC_Game.GetGameRun()?.GetUIManager()?.GetInGameUIMan()?.GetDialog(DialogId) as DlgStorageChangePW;
}
public override void Awake()
{
base.Awake();
ResolveControls();
WireButtons();
}
public override void OnShowDialogue()
{
base.OnShowDialogue();
WireButtons();
ClearFields();
}
void ResolveControls()
{
oldPasswordInput = DlgStoragePasswordUiHelper.FindInputField(transform, oldPasswordInput, "DEFAULT_Txt_Input");
newPasswordInput = DlgStoragePasswordUiHelper.FindInputField(transform, newPasswordInput, "Txt_New");
confirmPasswordInput = DlgStoragePasswordUiHelper.FindInputField(transform, confirmPasswordInput, "Txt_NewConfirm");
EnsurePasswordFieldLayout();
DlgStoragePasswordUiHelper.ConfigurePasswordField(oldPasswordInput);
DlgStoragePasswordUiHelper.ConfigurePasswordField(newPasswordInput);
DlgStoragePasswordUiHelper.ConfigurePasswordField(confirmPasswordInput);
DlgStoragePasswordUiHelper.ResolveButton(ref confirmButton, transform, "confirm", "btn_decide", "btn_confirm", "ok");
DlgStoragePasswordUiHelper.ResolveButton(ref cancelButton, transform, "cancel", "idcanel", "btn_cancel", "btn_exit");
DlgStoragePasswordUiHelper.ResolveButton(ref closeButton, transform, "btn_close", "close");
}
void WireButtons()
{
DlgStoragePasswordUiHelper.ResolveButton(ref confirmButton, transform, "confirm", "btn_decide", "btn_confirm", "ok");
DlgStoragePasswordUiHelper.WireConfirmButton(transform, OnConfirm, confirmButton);
DlgStoragePasswordUiHelper.WireCancelButtons(transform, OnCancel);
}
void EnsurePasswordFieldLayout()
{
if (oldPasswordInput == null)
return;
if (newPasswordInput == null)
newPasswordInput = DlgStoragePasswordUiHelper.ClonePasswordField(oldPasswordInput, "Txt_New", new Vector2(0f, -22f));
if (confirmPasswordInput == null)
confirmPasswordInput = DlgStoragePasswordUiHelper.ClonePasswordField(oldPasswordInput, "Txt_NewConfirm", new Vector2(0f, -44f));
}
void ClearFields()
{
if (oldPasswordInput != null)
oldPasswordInput.SetTextWithoutNotify(string.Empty);
if (newPasswordInput != null)
newPasswordInput.SetTextWithoutNotify(string.Empty);
if (confirmPasswordInput != null)
confirmPasswordInput.SetTextWithoutNotify(string.Empty);
}
void OnConfirm()
{
string oldPw = oldPasswordInput != null ? oldPasswordInput.text : string.Empty;
string newPw = newPasswordInput != null ? newPasswordInput.text : string.Empty;
string confirmPw = confirmPasswordInput != null ? confirmPasswordInput.text : string.Empty;
if (newPw != confirmPw)
{
var gameUi = EC_Game.GetGameRun()?.GetUIManager()?.GetInGameUIMan();
gameUi?.ShowErrorMsg(GetStringFromTable(StringIdPasswordMismatch), string.Empty);
return;
}
UnityGameSession.c2s_CmdNPCSevChgTrashPsw(oldPw, newPw);
CloseDialog();
}
void OnCancel()
{
CECUIManager.Instance?.EndNpcTalkAfterStorageService();
}
void CloseDialog()
{
CECUIManager.Instance?.EndNpcTalkAfterStorageService();
}
}
static class DlgStoragePasswordUiHelper
{
public static TMP_InputField FindInputField(Transform root, TMP_InputField assigned, string controlName)
{
if (assigned != null)
return assigned;
var direct = root.Find(controlName);
if (direct != null && direct.TryGetComponent(out TMP_InputField directField))
return directField;
var all = root.GetComponentsInChildren(true);
for (int i = 0; i < all.Length; i++)
{
if (all[i] != null && string.Equals(all[i].name, controlName, StringComparison.OrdinalIgnoreCase))
return all[i];
}
if (all.Length == 1 && string.Equals(controlName, "DEFAULT_Txt_Input", StringComparison.OrdinalIgnoreCase))
return all[0];
return null;
}
public static void ConfigurePasswordField(TMP_InputField field)
{
if (field == null)
return;
field.contentType = TMP_InputField.ContentType.Password;
field.inputType = TMP_InputField.InputType.Password;
}
public static TMP_InputField ClonePasswordField(TMP_InputField template, string name, Vector2 anchoredOffset)
{
if (template == null)
return null;
var clone = UnityEngine.Object.Instantiate(template, template.transform.parent);
clone.name = name;
var rt = clone.GetComponent();
if (rt != null)
rt.anchoredPosition += anchoredOffset;
ConfigurePasswordField(clone);
return clone;
}
public static void ResolveButton(ref Button field, Transform root, params string[] nameHints)
{
if (field != null)
return;
var buttons = root.GetComponentsInChildren