UI/InvestmentPanel/InvestmentPanel.razor
@using System.Text;
@using Milaine.Managers;
@using Milaine.Settings;
@using Milaine.Utils;
@using Sandbox;
@using Sandbox.UI;
@inherits PanelComponent
@namespace Sandbox

<root>
  <header>
    <div class="container-title">
      <Image src="resources/hexagon.png" class="image" style="width: 20px; height: 20px;" />
      <h1 class="title">SHARES // WORKS</h1>
    </div>
    <div class="content" style="pointer-events: @(MouseManager.State == StateMouse.Busy ? "none" : "all");">
      <Image src="resources/wallet-minimal.png" class="image" style="width: 20px; height: 20px;" />
      <p class="text">[email protected](InvestmentManagerInstance.Wallet)</p>
    </div>
  </header>
  <div class="container">
    <section class="card" style="pointer-events: @(MouseManager.State == StateMouse.Busy ? "none" : "all");">
      <div class="card-header">
        <div class="card-header-wrapper">
          @if (InvestmentManagerInstance.Profit == 0) {
            <Image src="resources/dot.png" />
          }
          @if (InvestmentManagerInstance.Profit > 0) {
            <Image src="resources/trending-up.png" />
          }
          @if (InvestmentManagerInstance.Profit < 0) {
            <Image src="resources/trending-down.png" />
          }
          <p class="card-title">
            @* #milaine.profit.total *@
            @TranslationsInstance.T("milaine.profit.total")
          </p>
        </div>
        <div class=@ClassName()>
          @if (InvestmentManagerInstance.Profit == 0) {
            <p>
              @* #milaine.critical.standard-profit *@
              @TranslationsInstance.T("milaine.critical.standard-profit")
            </p>
          }
          @if (InvestmentManagerInstance.Profit < 0) {
            <p>
              @* #milaine.critical.bad-profit *@
              @TranslationsInstance.T("milaine.critical.bad-profit")
            </p>
          }
          @if (InvestmentManagerInstance.Profit > 0) {
            <p>
              @* #milaine.critical.profit *@
              @TranslationsInstance.T("milaine.critical.profit")
            </p>
          }
        </div>
      </div>
      <div class="card-body">
        <div class="investment-container">
          <div class="investment-content">
            <p
              class="investment-text @(InvestmentManagerInstance.Profit == 0 ? "normal" : InvestmentManagerInstance.Profit < 0 ? "bad" : "good")">
              @BuildInvestment(InvestmentManagerInstance.Investment)
            </p>

            @if (InvestmentManagerInstance.Profit > 0) {
              <p class="shares">+@InvestmentManagerInstance.Profit</p>
            }
          </div>
          <div class="action">
            <button class="btn btn-primary" onclick=@HandleClickInvestment>
              @* #milaine.invest *@
              @TranslationsInstance.T("milaine.invest")
            </button>
            <button class="btn btn-secondary" onclick=@HandleClickDeposit>
              @* #milaine.deposit *@
              @TranslationsInstance.T("milaine.deposit")
            </button>
          </div>
        </div>
      </div>
    </section>
  </div>
</root>

@code
{
  [Property, Title("InvestmentManager")]
  [Feature("Managers")]
  public InvestmentManager InvestmentManagerInstance { get; set; }

  [Property, Title("IdleManager")]
  [Feature("Managers")]
  public IdleManager IdleManagerInstance { get; set; }

  [Property, Title("Translations")]
  [Feature("Managers")]
  public Translations TranslationsInstance { get; set; }

  public Dictionary<string, object> Data = [];

  protected override int BuildHash() => System.HashCode.Combine(Data, InvestmentManagerInstance.Wallet,
InvestmentManagerInstance.Profit, TranslationsInstance.language, MouseManager.State);

  protected override void OnStart() {
    if (!InvestmentManagerInstance.IsValid()) {
      Log.Warning("Need Instance the InvestmentManager into [InvestmentView]");
      return;
    }

    Refresh();
  }

  protected override void OnUpdate() {
    base.OnUpdate();

    if (Input.EscapePressed) {
      StateHasChanged();
    }
  }

  public string ClassName() => InvestmentManagerInstance.Profit == 0 ? "tag standard" : InvestmentManagerInstance.Profit
< 0 ? "tag low" : "tag";

  private void HandleClickInvestment() {
    InvestmentManagerInstance.Invest();
    Refresh();
  }

  private void HandleClickDeposit() {
    InvestmentManagerInstance.Deposit(IdleManagerInstance.Dollars);
    IdleManagerInstance.Deposit();
    Refresh();
  }

  private string BuildInvestment(float investment) {
    StringBuilder sb = new();
    float profit = InvestmentManagerInstance.Profit;

    if (profit < 0) {
      sb.Append("- $");
    }

    if (profit > 0) {
      sb.Append("+ $");
    }

    sb.Append(Formatter.ToDisplay(investment));

    return sb.ToString();
  }

  private void Refresh() {
    Data["WalletValue"] = Formatter.ToDisplayShort(InvestmentManagerInstance.Wallet);
    Data["InvestmentValue"] = Formatter.ToDisplay(InvestmentManagerInstance.Investment);
    Data["ProfitValue"] = Formatter.ToDisplay(InvestmentManagerInstance.Profit);
    Data["SharesValue"] = InvestmentManagerInstance.Shares;
  }
}