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

public class TravellerResponsive : Responsive
{
    public class RecoveredAction : CoreAction
    {
    }

    public int healRequirement = 3;
    public int healCountReq = 7;
    private int currentHealCount; 
    private bool responding = false;
    private UnityTimer respondTimer = new UnityTimer(3f, false);

    protected override void InnerAwake()
    {
        base.InnerAwake();
        AddGlobalActionHandler<DayDirector.NewDayAction>(this, NewDay);
        if (this.displayName == "")
        {
            this.displayName = "The Traveller";
        }
    }

    protected override void InnerUpdate()
    {
        if (responding)
        {
            respondTimer.Advance();
            if (respondTimer.IsComplete())
            {
                Announce(new DayDirector.EndDayAction());
                Announce(new RecoveredAction());
                respondTimer.Reset();
                responding = false;
                Announce(new AlchemistPlayerInput.UnfreezeAction());
            }
        }
    }

    private void NewDay(DayDirector.NewDayAction action)
    {
        if (currentHealCount == healCountReq)
        {
            currentHealCount = -1;
            Destroy(this.gameObject);
            Instantiate(Resources.Load("Maro"), this.transform.position, Quaternion.Euler(0, -45, 0));
        }
    }

    protected override void Respond(RespondAction action)
    {
       // Debug.Log(displayName + " responds. You are talking to them.");
        AlchemistBag bag = action.GetSender().GetGameObject().GetComponent<AlchemistBag>();
        if (bag != null && bag.medicineCount >= healRequirement)
        {
            bag.medicineCount -= healRequirement;
            if (currentHealCount == 2)
            {
                Announce(new Journal.AddEntryAction("The traveller is less feverish, but still weak."));
            }
            else if (currentHealCount == 4)
            {
                Announce(new Journal.AddEntryAction("The traveller's condition has become more stable, but not perfect."));
            }
            else if (currentHealCount == 6)
            {
                Announce(new Journal.AddEntryAction("The traveller has nearly recovered fully!"));
            }
            else
            {
                Announce(new Journal.AddEntryAction("I carefully fed the traveller 3 portions of the medicine and then went to bed."));
            }
            responding = true;
            Announce(new AlchemistPlayerInput.FreezeAction());
            Announce(new FadeDisplay.FadeInAction(this.respondTimer.GetEnd()));
            currentHealCount++;
        }
    }
}