using UnityEngine;
using System.Collections;
using Framework.Entities;

public class DayDirector : CoreBehaviour
{
    public class NewDayAction : CoreAction
    {
        public int dayNumber;

        public NewDayAction(int dayNumber)
            : base()
        {
            this.dayNumber = dayNumber;
        }
    }

    public class EndDayAction : CoreAction
    {
    }

    public int dayNumber = 0;
    public GameObject spokenToTarget;

    protected override void InnerAwake()
    {
        AddGlobalActionHandler<PersonResponsive.ReportSpokenToAction>(this, RecordSpokenTo);
        AddGlobalActionHandler<EndDayAction>(this, EndDay);
    }

    protected override void InnerStart()
    {
        StartNewDay();
    }

    protected override void InnerUpdate()
    {
        if (Input.GetKeyDown(KeyCode.N))
        {
            EndOldDay();
            StartNewDay();
        }
    }

    private void EndOldDay()
    {
        DestroyImmediate(this.spokenToTarget);
    }

    private void StartNewDay()
    {
        if (dayNumber != 0)
        {
            Announce(new Journal.AddEntryAction(dayNumber + " day(s) have passed since the traveller arrived."));
        }
        dayNumber++;
        Announce(new NewDayAction(dayNumber));
        Announce(new FadeDisplay.FadeOutAction(2f));
    }

    private void EndDay(EndDayAction action)
    {
        EndOldDay();
        StartNewDay();
    }

    private void RecordSpokenTo(PersonResponsive.ReportSpokenToAction action)
    {
        if (this.spokenToTarget == null)
        {
            this.spokenToTarget = action.GetSender().GetGameObject();
        }
    }
}
