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

public class BombController : MonoBehaviour
{
	[SerializeField] public float timer;
	[SerializeField] private float resetTimer;
	[SerializeField, ReadOnly] private float timer_;
	[SerializeField, ReadOnly] private float resetTimer_;
	[SerializeField, ReadOnly] private bool run;
	[SerializeField, ReadOnly] private bool reset;

	[SerializeField] private Transform owner;
	[SerializeField] private Text bombText;

	public Transform Owner {get {return owner; } }

	[SerializeField, ReadOnly] private Animator bombAnimator;

	private ParticleSystem detonationEffect;



    void Awake()
	{
		bombAnimator = GetComponent<Animator>();

		bombText = GetComponentInChildren<Text>();

		detonationEffect = transform.Find("BombExplosion").GetComponent<ParticleSystem>();
    }

	void Update()
	{
		if(run && !reset)
		{
			timer_ -= Time.deltaTime;
			if(timer_ <= 0) Detonate();
		}

		if(reset && !run)
		{
			resetTimer_ -= Time.deltaTime;
			if(resetTimer_ <= 0)
			{
				TransferToRandom();
				Reset();
			}
		}

		if (timer_ <= 5)
		{
			bombAnimator.SetBool("bounce", true);
		}


		bombText.text = Mathf.RoundToInt(timer_).ToString();
		//bombAnimator.SetFloat("Speed", GetValueInRange(timer_, 30, 0, 1, 3));

		if(owner != null && run)
			transform.position = owner.position + Vector3.up * (0.373859f);
	}

	private void Detonate()
	{
		bool isGameOver = GameStart.Instance.CheckGameOver();

		run = false;
		transform.position = owner.position;
		detonationEffect.Play();
		Debug.Log("A");

		if (isGameOver)
		{
			ToggleGraphics(false);
			Pause();
		}
		else
		{
			EZCameraShake.CameraShaker.Instance.ShakeOnce(5f, 6, 0.2f, 0.5f);
			Cooldown();
		}

		CatController cc = owner.GetComponent<CatController>();
		// cc.Appearance.ToggleVisibility(false);
		// cc.Input.SetPermissions(false);

		cc.Appearance.Kill(true);

		SoundManager.Instance.Play(SoundManager.SoundType.Bomb);



		// if(GameManager.Instance.GameStart.AlivePlayerCount <= 2)
		// {
		// 	Time.timeScale = 0.5f;
		// 	cc.Appearance.ToggleVisibility(false);
		// 	cc.Input.SetPermissions(false);
			
		// 	GetComponent<SpriteRenderer>().enabled = false;
		// }
		// else
		// {
		// 	EZCameraShake.CameraShaker.Instance.ShakeOnce(5f, 6, 0.2f, 0.5f);
		// 	//Kill owner
		// 	cc.Appearance.ToggleVisibility(false);
		// 	cc.Input.SetPermissions(false);

		// 	//Reset
		// 	Reset();
		// 	//Transfer

		// }

	}

	private IEnumerator WaitToTransferBomb()
	{
		yield return new WaitForSeconds(resetTimer);
		TransferToRandom();
		Reset();
	}

	public float GetValueInRange(float value, float myMinRange, float myMaxRange, float newMinRange, float newMaxRange)
    {
        return (value - myMinRange) * (newMaxRange - newMinRange)/(myMaxRange - myMinRange) + newMinRange;
    }

	public void Transfer(Transform newOwner)
	{
		owner = newOwner;
	}

	public void TransferToRandom()
	{
		owner = GameStart.Instance.GetAliveRandomPlayer().transform;
	}

	public void Reset()
	{
		timer_ = timer;
		bombAnimator.SetBool("bounce", false);
		ToggleGraphics(true);
		run = true;
		reset = false;
	}

	public void Cooldown()
	{
		resetTimer_ = resetTimer;
		reset = true;
		ToggleGraphics(false);
		Pause();
	}

	public void Pause()
	{
		run = false;
	}

	public void ToggleGraphics(bool toogle)
	{
		GetComponent<SpriteRenderer>().enabled = toogle;
		GetComponentInChildren<Canvas>().enabled = toogle;
	}
}