using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public enum WORK_TYPE
{
    FARMER,
    SHOPKEEPER,
    BUILDER
}

/*
 *  0. wander to random points
 *  1. if given job to work on it
 *  2. Attempts job
 *  3. if can't perform job goto 0
 *  4. Work on job
 *  5. if job complete goto 0
 */

public class Person : MonoBehaviour 
{
    public string full_name;

    public float speed = 5;
    public int piety;
    public int work_ethic;
    public int charity;
    public int skill;
    public WORK_TYPE work_type;
    public Vector3 wander_position;
    public float skill_timer = 250;
    public Job current_job;
    public Text name_tag;

    public Transform sprite;
    public Animator animator;

    Vector3 last_pos;

    void Start()
    {
        last_pos = transform.position;
        sprite = transform.GetChild(0);
        animator = sprite.GetComponent<Animator>();

        wander_position = transform.position;
        //happiness = 0.5f;
        piety = Random.Range(1, 20);
        work_ethic = Random.Range(1, 20);
        charity = Random.Range(1, 20);
        skill = 1;

        full_name = FirstNames.names[Random.Range(0, FirstNames.names.Length)] + 
                    " " + 
                    LastNames.names[Random.Range(0, LastNames.names.Length)];

        GameObject text = GameObject.Find("NameText");

        GameObject new_txt = Instantiate(text);
        new_txt.transform.SetParent(text.transform.parent);
        name_tag = new_txt.GetComponent<Text>();
        name_tag.text = full_name;

        TownManager.tm.people.Add(this);
    }

    void OnDestroy()
    {
        TownManager.tm.people.Remove(this);
    }

    void Update()
    {
        if ( current_job != null ) skill_timer -= Time.deltaTime;
        if ( skill_timer < 0 )
        {
            skill_timer = 200;
            skill++;
        }

        Resolution current_res = Screen.currentResolution;
        float meters_to_pixels = (Camera.main.orthographicSize * 4) / current_res.height;
        name_tag.transform.position = Camera.main.WorldToScreenPoint(transform.position) +
            Vector3.up * 1 / meters_to_pixels;

        if ( transform.position.y > 0.1f )
        {
            animator.SetBool("WalkingDown", false);
            animator.SetBool("IdleDown", false);
            animator.SetBool("IdleUp", false);
            animator.SetBool("WalkingUp", false);
            animator.SetBool("Sacrifice", true);
        }
        else if ( last_pos != transform.position )
        {
            Vector3 dir = (transform.position - last_pos).normalized;


            if ( Vector3.Dot(dir, Camera.main.transform.up) > 0 )
            {
                if ( Vector3.Dot(dir, Camera.main.transform.right) > 0 )
                {
                    Vector3 scale = sprite.transform.localScale;
                    scale.x = -1;
                    sprite.transform.localScale = scale;
                }
                else
                {
                    Vector3 scale = sprite.transform.localScale;
                    scale.x = 1;
                    sprite.transform.localScale = scale;
                }
                animator.SetBool("WalkingDown", false);
                animator.SetBool("IdleDown", false);
                animator.SetBool("IdleUp", false);
                animator.SetBool("WalkingUp", true);
            }
            else
            {

                if ( Vector3.Dot(dir, Camera.main.transform.right) > 0 )
                {
                    Vector3 scale = sprite.transform.localScale;
                    scale.x = 1;
                    sprite.transform.localScale = scale;
                }
                else
                {
                    Vector3 scale = sprite.transform.localScale;
                    scale.x = -1;
                    sprite.transform.localScale = scale;
                }
                animator.SetBool("WalkingUp", false);   
                animator.SetBool("IdleDown", false);
                animator.SetBool("IdleUp", false);
                animator.SetBool("WalkingDown", true);
            }
        }
        else
        {
            animator.SetBool("WalkingUp", false);
            animator.SetBool("IdleUp", false);
            animator.SetBool("WalkingDown", false);
            animator.SetBool("IdleDown", true);
        }

        last_pos = transform.position;

    }

    public bool WalkToPoint(Vector3 goal, float threshold)
    {
        bool result = false;
        Vector3 diff = (goal - transform.position);
        Vector3 dir = diff.normalized;

        if ( diff.magnitude < threshold )
        {
            result = true;
        }
        else
        {
            float speed_to_use = speed;

            if ( diff.magnitude < speed )
                speed_to_use = diff.magnitude;

            transform.position += dir * speed_to_use * Time.deltaTime;
                //Vector3.Lerp(transform.position, goal, speed * Time.deltaTime);
        }
        return result;
    }
}


