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

public class PersonResponsive : Responsive
{
    public class ReportSpokenToAction : CoreAction
    {
        public ReportSpokenToAction(CoreBehaviour sender)
            : base(sender)
        {
        }
    }

    public class MarkForDeathAction : CoreAction
    {
    }

    public DialogBehaviour dialog;
    public int giftCount = 3;
    public string dialogFile;
    private int currentDay = 0;
    private bool doneToday = false;

    protected override void InnerAwake()
    {
        base.InnerAwake();
        AddGlobalActionHandler<DayDirector.NewDayAction>(this, NewDay);
        AddActionHandler<MarkForDeathAction>(MarkForDeath);
        if (this.displayName == "")
        {
            this.displayName = "Person";
        }
        dialog = ((GameObject)Instantiate(Resources.Load("DialogUI"), this.transform.position + Vector3.up * 3f, Quaternion.Euler(0, -45, 0))).GetComponent<DialogBehaviour>();
    }

    protected override void InnerStart()
    {
        base.InnerStart();
        dialog.transform.SetParent(this.transform);
        dialog.gameObject.SetActive(false);
    }

    protected override void InnerUpdate()
    {

    }

    protected override void Respond(RespondAction action)
    {
        if (!doneToday)
        {
            Debug.Log(displayName + " responds. You are talking to them.");
            AlchemistBag bag = action.GetSender().GetGameObject().GetComponent<AlchemistBag>();
            if (bag != null)
            {
                bag.medHerbsCount += giftCount;
                //this.giftCount = 0;
            }

            if (dialog != null)
            {
                string[] dayDialogs = Journal.LoadText(dialogFile);
                if (currentDay > dayDialogs.Length)
                {
                    dialog.text = dayDialogs[0];
                }
                else
                {
                    dialog.text = dayDialogs[currentDay - 1];
                }
                dialog.gameObject.SetActive(true);
                dialog.Refresh();
            }
            doneToday = true;
        }
    }

    private void NewDay(DayDirector.NewDayAction action)
    {
        //this.giftCount = 3f;
        currentDay = action.dayNumber;
        doneToday = false;
    }

    private void MarkForDeath(MarkForDeathAction action)
    {
        Announce(new ReportSpokenToAction(this));
        Announce(new Journal.AddEntryAction(displayName + " will not wake up tomorrow."));
        GameObject mark = (GameObject)Instantiate(Resources.Load("Mark"), this.transform.position, new Quaternion());
        mark.transform.SetParent(this.transform);
    }
}