using System;
using System.Collections;
using System.Collections.Generic;
//using System.Linq;

//using UnityEditor;
using UnityEngine;
//using UnityEngine.UI;
//using Uhity.Entities;

using URD = UnityEngine.Random;

public class EatFood : MonoBehaviour {
	
	
	public Beaver beaver;
	
	public Transform[] wayPoint;
	public LayerMask foodMask = 1<<8;
	
	public int eatCount = 0;
	
	public float radios = 2f;
	
	public float pupuTime = 0;
	
	public bool eated = false;
	
	public GameObject pupu_prefab;
	
	public float dialogTime = -15;
	
	public void Begin() {
		StartCoroutine(BeginSearch());
	}
	
	public IEnumerator BeginSearch() {
		
		while(true) {
			
			yield return StartCoroutine(Walk());
			yield return StartCoroutine(Idle());
			
			yield return null;
		}
		
	}
	
	private IEnumerator Idle() {
		var t = URD.Range(0f,5f);
		
		while(t>0) {
			beaver.Stop();
			t -= Time.deltaTime;
			
			var food = FindNearFood();
			
			if(HasFood(food))
				yield return StartCoroutine(GoGetFood(food));
			
			yield return null;
		}
	}
	
	private IEnumerator Walk() {
		
		var t = URD.Range(4f,10f);
		
		var target = URD.Range(0, 100) % wayPoint.Length;
		var targetPoint = wayPoint[target].position;
		
		while(t>0 && DistanceTo(targetPoint) > 0.5f) {
			t -= Time.deltaTime;
			
			beaver.targetPoint = targetPoint;
			
			var food = FindNearFood();
			
			if(HasFood(food)) {
				yield return StartCoroutine(GoGetFood(food));
				break;
			}
			
			if(eated && Time.time - pupuTime > 15f)
				TryPupu();
			
			yield return null;
		}
	}
	
	
	private void TryPupu() {
		eated = false;
		
		if(URD.value < 0.25f) return;
		
		pupuTime = Time.time;
		
		Instantiate(pupu_prefab, beaver.transform.position - beaver.transform.forward * 0.5f, beaver.transform.rotation);
	}
	
	private IEnumerator GoGetFood(Transform food) {
		while(food != null && DistanceTo(food.position) < 3.5f && DistanceTo(food.position) > 0.5f) {
			
			beaver.targetPoint = food.position;
			yield return null;
		}
		
		yield return StartCoroutine(Eat(food));
	}
	
	private IEnumerator Eat(Transform food) {
		beaver.Stop();
		yield return new WaitForSeconds(0.5f);
		
		beaver.Eat();
		var pupued = false;
		
		if(food.gameObject.tag == "Pupu") {
			Dialog.Display("树根：告诉你们一个秘密，别被他的外表骗了，看着可爱，其实它吃屎！我亲眼看见的！但是它放的屁居然是香的，emmm，大概是香草味吧，别问我是怎么知道的");
			dialogTime = 15f;
			pupued = true;
		}
		
		Destroy(food.gameObject);
		
		
		yield return new WaitForSeconds(1f);
		
		pupuTime = Time.time - 8f;
		
		eatCount++;
		if(!pupued)
			eated = true;
	}
	
	private Transform FindNearFood() {
		var p = beaver.transform.position + beaver.transform.forward;
		var collider = Physics.OverlapSphere(p, radios, foodMask);
		
		if(collider != null && collider.Length > 0)
			return collider[0].transform;
		
		return null;
	}
	
	private float DistanceTo(Vector3 point) {
		var disp = point - beaver.transform.position;
		disp.y = 0;
		
		return disp.magnitude;
	}
	
	private bool HasFood(Transform food) {
		if(food == null) return false;
		
		if(food.gameObject.tag == "Pupu" && Time.time - pupuTime < 10) return false;
		
		return true;
	}
	
	void Update() {
		if(dialogTime > 0) {
			dialogTime -= Time.deltaTime;
			if(!(dialogTime > 0))
				Dialog.Close();
		}
	}
}
