using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SuitDudeController : MonoBehaviour {

	[SerializeField] Transform _player;
	[SerializeField] Vector3 _lookAtOffset;
	[SerializeField] IKLookAt[] _suitDudeLookAt;
	Animator[] _animators;

	void Awake(){
		if(_player == null){
			GameObject tmp = GameObject.FindWithTag("Player");
			if(tmp != null) _player = tmp.transform;
		}

		if(_player != null){
			foreach(IKLookAt i in _suitDudeLookAt){
				i.SetTarget(_player, _lookAtOffset);
			}
		}

		_animators = GetComponentsInChildren<Animator>();
		RandomizePositions();
	}

	// haha o wow
	void RandomizePositions(){
		List<Vector3> _positions = new List<Vector3>();
		Transform[] transforms = new Transform[_animators.Length];
		for(int i = 0; i < _animators.Length; i++){
			transforms[i] = _animators[i].transform;
			_positions.Add(transforms[i].position);
		}

		for(int i = 0; i < transforms.Length; i++){
			Vector3 pos = _positions[Random.Range(0, _positions.Count)];
			_positions.Remove(pos);
			transforms[i].position = pos + new Vector3(Random.Range(-.5f, .5f), 0, Random.Range(-1f, 0));
			transforms[i].localEulerAngles += new Vector3(0, Random.Range(-10, 10), 0);
		}		
	}

	public void DoReaction(bool positive){
		foreach(Animator a in _animators){
			a.SetTrigger((positive) ? "tYes" : "tNo");
		}

		foreach(IKLookAt i in _suitDudeLookAt){
			i.DisableFor(3);
		}
	}




}
