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 Story : MonoBehaviour {
	
	public GameObject[] storyCamera;
	public GameObject[] beaver;
	
	
	public Animation sAnim_0;
	public Animator sAnim_1;
	
	public DragItem clampDrag;
	public Beaver clampBeaver;
	public ClampCheckCast[] cast;
	public Transform target;
	
	public Ken ken;
	public Animator treeFall;
	
	public EatFood eatFood;
	
	public DragItem nestDrag;
	public ClampCheckCast[] nestCast;
	
	public GameObject otherBeaver;
	
	public GameObject end;
	
	// private
	
	// Start is called before the first frame update
	public IEnumerator Start() {
		ken.Show(false);
		
		
		yield return StartCoroutine(Story_0());
		yield return StartCoroutine(Story_1());
		yield return StartCoroutine(Story_2());
		yield return StartCoroutine(Story_3());
		yield return StartCoroutine(Story_4());
		yield return StartCoroutine(Story_5());
		yield return StartCoroutine(Story_End());
		
	}
	
	private IEnumerator Story_0() {
		Dialog.Close();
		SetCamera(0);
		SetBeaver(0);
		
		sAnim_0.Play();
		
		yield return new WaitForSeconds(0.5f);
		
		Dialog.Display("？？？：哦！");
		yield return new WaitForSeconds(0.75f);
		
		Dialog.Display("？？？：痛！");
		yield return new WaitForSeconds(0.75f);
		
		Dialog.Display("？？？：好痛！");
		yield return new WaitForSeconds(1.5f);
		
		Dialog.Close();
		yield return new WaitForSeconds(2f);
	}
	
	private IEnumerator Story_1() {
		SetCamera(1);
		SetBeaver(1);
		
		yield return new WaitForSeconds(1f);
		
		
		sAnim_1.Play("TreeFall");
		yield return new WaitForSeconds(1f);
		
		Dialog.Display("树根：我是受害者树根，原本我是一棵完整的数，自从被这家伙盯上，我就遭殃了。");
		yield return new WaitForSeconds(5f);
		
	}
	
	private IEnumerator Story_2() {
		SetCamera(2);
		SetBeaver(2);
		
		clampBeaver.gameObject.SetActive(true);
		clampDrag.enabled = true;
		
		Dialog.Display("看那个小短腿，他爬不上去了哈哈，活该。");
		yield return new WaitForSeconds(3.5f);
		
		Dialog.Display("树根：如果没有踮脚的东西，我看他是一辈子也别想爬上去了");
		
		while(!HasPath() && !Input.GetKeyDown(KeyCode.K)) {
			
			yield return null;
		}
		
		clampDrag.enabled = false;
		
		Debug.Log("Finish");
		
		// clampBeaver.rbody.isKinematic = false;
		
		clampBeaver.targetPoint = target.position;
		
		Dialog.Display("树根：你为什么要帮他？完蛋，又有我的同类要遭殃了");
		
		yield return new WaitForSeconds(2f);
	}
	
	private IEnumerator Story_3() {
		SetCamera(3);
		SetBeaver(3);
		
		Dialog.Display("树根：看，又一个受害者");
		yield return new WaitForSeconds(2f);
		
		ken.Show(true);
		ken.Begin();
		
		while(ken.count < 1) {
			
			yield return null;
		}
		
		ken.Show(false);
		
		treeFall.Play("tree");
		Dialog.Display("树根：哦！现在他不光有了搭窝的材料，还能吃到甜美的果子。我简直不敢相信。");
		yield return new WaitForSeconds(5f);
		
		Dialog.Close();
	}
	
	private IEnumerator Story_4() {
		SetCamera(4);
		SetBeaver(4);
		
		yield return new WaitForSeconds(1f);
		
		eatFood.Begin();
		
		Dialog.Display("树根：嘿嘿，他难度还不会自己吃饭吗？ 不需要你喂。");
		
		yield return new WaitForSeconds(3f);
		
		Dialog.Close();
		
		while(eatFood.eatCount < 5 || eatFood.dialogTime > 0) {
			
			yield return null;
		}
		
		eatFood.StopAllCoroutines();
		eatFood.beaver.Stop();
		
		Dialog.Display("树根：看来他吃饱了，谁知道他还会干点什么");
		
		yield return new WaitForSeconds(3f);
		
	}
	
	private IEnumerator Story_5() {
		SetCamera(5);
		SetBeaver(5);
		
		yield return new WaitForSeconds(0.5f);
		Dialog.Display("树根：可怜的家伙，连个窝都搭不好，这河狸吗？ 喂喂，你又要出手帮忙了吗？");
		
		nestDrag.enabled = true;
		
		while(!FinishNest()) {
			
			
			yield return null;
		}
		
		
		yield return new WaitForSeconds(0.5f);
		
	}
	
	private IEnumerator Story_End() {
		SetCamera(6);
		SetBeaver(6);
		otherBeaver.SetActive(true);
		CloseChecker();
		
		yield return null;
		yield return null;
		yield return null;
		yield return null;
		
		yield return null;
		Dialog.Display("树根：你问后面发生了什么？");
		while(!Input.GetMouseButtonDown(0)) yield return null;
		
		yield return null;
		Dialog.Display("树根：后来他找到了一只母河狸");
		while(!Input.GetMouseButtonDown(0)) yield return null;
		
		yield return null;
		Dialog.Display("树根：一起住在你帮着盖得窝里");
		while(!Input.GetMouseButtonDown(0)) yield return null;
		
		yield return null;
		Dialog.Display("树根：生了一窝可爱的小家伙");
		while(!Input.GetMouseButtonDown(0)) yield return null;
		
		yield return null;
		Dialog.Display("树根：生了一窝可爱的小家伙");
		while(!Input.GetMouseButtonDown(0)) yield return null;
		
		yield return null;
		Dialog.Display("树根：这期间又发生了很多很多事情");
		while(!Input.GetMouseButtonDown(0)) yield return null;
		
		yield return null;
		Dialog.Display("树根：等我有时间在讲给你听");
		while(!Input.GetMouseButtonDown(0)) yield return null;
		
		end.SetActive(true);
	}
	
	private bool FinishNest() {
		if(nestDrag.draged != null) return false;
	
		for(int i=0; i<nestCast.Length; i++) {
			if(!nestCast[i].isHit) return false;
		}
		
		return true;
	}
	
	private void CloseChecker() {
		for(int i=0; i<cast.Length; i++) {
			cast[i].gameObject.SetActive(false);
		}
		for(int i=0; i<nestCast.Length; i++) {
			nestCast[i].gameObject.SetActive(false);
		}
	}
	
	private bool HasPath() {
		if(clampDrag.draged != null) return false;
	
		for(int i=0; i<cast.Length; i++) {
			if(!cast[i].isHit) return false;
		}
		
		return true;
	}
	
	private void SetBeaver(int index) {
		for(int i=0; i<beaver.Length; i++) {
			beaver[i].SetActive(i==index);
		}
	}
	
	private void SetCamera(int index) {
		for(int i=0; i<storyCamera.Length; i++) {
			storyCamera[i].SetActive(i==index);
		}
	}
}
