using UnityEngine;
using System.Collections;
using Framework.Entities;
using UnityEngine.UI;
using Framework.Extensions;
using System.Collections.Generic;

public class JournalDisplay : CoreBehaviour
{
    public Journal journal;

    private Text contentsText;
    private int currentPage;

    public void NextPage()
    {
        if (this.currentPage < this.journal.pages.Count-1)
        {
            this.currentPage++;
            RefreshPage();
        }
    }

    public void PreviousPage()
    {
        if (this.currentPage > 0)
        {
            this.currentPage--;
            RefreshPage();
        }
    }

    public void Close()
    {
        this.gameObject.SetActive(false);
    }

    protected override void InnerAwake()
    {
        AddGlobalActionHandler<Journal.UpdatedAction>(this, UpdateEntries);
        this.contentsText = this.transform.RecursivelyFindChild("ContentsText").GetComponent<Text>();
    }

    protected override void InnerUpdate()
    {

    }

    private void UpdateEntries(Journal.UpdatedAction action)
    {
        this.currentPage = this.journal.pages.Count - 1;
        RefreshPage();
    }

    private void RefreshPage()
    {
        List<string> pageContents = this.journal.pages[this.currentPage];
        this.contentsText.text = "Page " + (this.currentPage + 1) + "\n\n" + string.Join("\n", pageContents.ToArray());
    }
}
