UI/Story/NotesUI.razor
@using Sandbox;
@using Sandbox.UI;

@namespace Opium
@inherits PanelComponent

<root>
	@if(Note != null)
	{
		<div class="pagecount">
			<div class="control">
                <InputHint @Action="Left" />
                <label>@left</label>
            </div>
			<label>@CurrentPage / @Pages</label>
			<div class="control">
                <label>@right</label>
                <InputHint @Action="Right" />
            </div>
		</div>
		<div class="notepage">@MyStringValue</div>
		<div class="stoplooking">
            <label>[</label>
            <InputHint @Action="Use" />
            <label>] Close</label> 
        </div>
	}
	@if(Model != null)
	{
		<ModelPanelInspector Model="@(Model)"></ModelPanelInspector>
		<div class="stoplooking">
            <label>[</label>
            <InputHint @Action="Attack1" />
            <label>] Rotate</label>
            
            <div class="spacer" />

            <label>[</label>
            <InputHint @Action="Attack2" />
            <label>] Pan</label> 

            <div class="spacer" />

            <label>[</label>
            <InputHint @Action="Use" />
            <label>] Close</label> 
      </div>
	}
</root>

@code
{
    public TextInteractor LookingAtObject { get; set; }

    public void StartInteract( TextInteractor text )
    {
        LookingAtObject = text;

        var player = GameObject.Components.Get<Opium.PlayerController>( FindMode.EverythingInSelfAndAncestors );

        player.LockMovement = true;
        player.LockCameraMovement = true;


        if ( !LookingAtObject.IsModel )
        {
            StartLookingAtNotes( LookingAtObject.Note );
        }
        else
        {
            StartLookingAtModel(LookingAtObject.Components.Get<ModelRenderer>(FindMode.EnabledInSelf).Model.ResourcePath);

            // Disable it
            LookingAtObject.Components.Get<ModelRenderer>(FindMode.EnabledInSelf).Enabled = false;
        }
    }

    public void StopInteract()
    {
        StopLookingAtNotes();

        var player = GameObject.Components.Get<Opium.PlayerController>( FindMode.EverythingInSelfAndAncestors );

        player.LockMovement = false;
        player.LockCameraMovement = false;

        // Re-enable model renderer if we can
        LookingAtObject.Components.Get<ModelRenderer>(FindMode.EverythingInSelf).Enabled = true;
        LookingAtObject.NextInteract = 1f;
        LookingAtObject = null;
    }

    TextNoteResource Note { get; set; }
    string Model { get; set; }

    TimeSince TimeSinceOpened = 100;

    string left => "<";
    string right => ">";

    public string MyStringValue { get; set; } = "!";

    int Pages { get; set; } = 0;
    int CurrentPage { get; set; } = 1;

    public void StartLookingAtModel(string model)
    {
        SetClass("visible", true);
        Model = model;
        TimeSinceOpened = 0;
    }

    public void StartLookingAtNotes(TextNoteResource notes)
    {
        SetClass("visible", true);
        Note = notes;
        Pages = Note.TextNotes.Count;
        UpdateNoteContent();
        CurrentPage = 1;
        TimeSinceOpened = 0;
    }

    public void StopLookingAtNotes()
    {
        SetClass("visible", false);
        Note = null;
        Model = null;
    }

    void PrevPage()
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            UpdateNoteContent();
        }
    }

    void NextPage()
    {
        if (CurrentPage < Pages)
        {
            CurrentPage++;
            UpdateNoteContent();
        }
    }

    void UpdateNoteContent()
    {
        if (Note != null && CurrentPage <= Pages)
        {
            MyStringValue = Note.TextNotes[CurrentPage - 1].Text; // -1 because list is 0-indexed but pages are 1-indexed
        }
    }

    protected override void OnUpdate()
    {
        if ( Note is null && Model is null ) return;

        if ( Input.Pressed( "Use" ) && TimeSinceOpened > 0.5f )
        {
            StopInteract();
        }

		if (Note != null)
		{
			if(Input.Pressed("left"))
				PrevPage();

			if(Input.Pressed("right"))
				NextPage();
		}
	}

	/// <summary>
	/// the hash determines if the system should be rebuilt. If it changes, it will be rebuilt
	/// </summary>
	protected override int BuildHash() => System.HashCode.Combine(MyStringValue, CurrentPage, Model);
}