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

public class RocketEnemy : MonoBehaviour, IPooledObject{

    public float speed;
    public GameObject particles;
    public PolygonCollider2D polCol;
    public GameObject spawnParticles;
    public GameObject rocket;
    public Color level1Color;
    public Color level2Color;
    public Color level3Color;
    public SpriteRenderer[] spriteRenderers;

    GameController gameController;
    Animator anim;
    bool spawned;
    bool ready;
    GameObject target;

    public void OnObjectSpawn(){
        gameController = FindObjectOfType<GameController>();
        if (gameController.level == 1 || gameController.level == 0){
            foreach (SpriteRenderer r in spriteRenderers){
                r.color = level1Color;
            }
            ParticleSystem ps = spawnParticles.GetComponent<ParticleSystem>();
            var main = ps.main;
            main.startColor = level1Color;
        }
        if (gameController.level == 2){
            foreach (SpriteRenderer r in spriteRenderers){
                r.color = level2Color;
            }
            ParticleSystem ps = spawnParticles.GetComponent<ParticleSystem>();
            var main = ps.main;
            main.startColor = level2Color;
        }
        if (gameController.level == 3){
            foreach (SpriteRenderer r in spriteRenderers){
                r.color = level3Color;
            }
            ParticleSystem ps = spawnParticles.GetComponent<ParticleSystem>();
            var main = ps.main;
            main.startColor = level3Color;
        }
        anim = GetComponent<Animator>();
        Invoke(nameof(StopAnimation), 1f);
        anim.SetBool("Spawned", false);
        particles.SetActive(false);
        ready = false;
        spawned = false;
        target = GameObject.FindGameObjectWithTag("Player");
        Invoke(nameof(Ready), 2f);
        polCol.enabled = false;
        spawnParticles.SetActive(true);
        spawnParticles.GetComponent<ParticleSystem>().Play();
    }

    // Update is called once per frame
    void Update(){
        if (ready){
            transform.Translate(Vector3.up * speed * Time.deltaTime);
        }else if(spawned){
            Vector2 targetPos = target.transform.position - transform.position;
            float rotZ = Mathf.Atan2(targetPos.y, targetPos.x) * Mathf.Rad2Deg;
            transform.rotation = Quaternion.Euler(0f, 0f, rotZ - 90);
        }else if (!spawned){
            Vector2 targetPos = target.transform.position - transform.position;
            float rotZ = Mathf.Atan2(targetPos.y, targetPos.x) * Mathf.Rad2Deg;
            rocket.transform.rotation = Quaternion.Euler(0f, 0f, rotZ - 90);
        }
    }

    void OnCollisionEnter2D(Collision2D col){
        if(col.gameObject.CompareTag("Wall")){
            Vector2 targetPos = target.transform.position - transform.position;
            float rotZ = Mathf.Atan2(targetPos.y, targetPos.x) * Mathf.Rad2Deg;
            transform.rotation = Quaternion.Euler(0f, 0f, rotZ - 90);
        }
        if(col.gameObject.CompareTag("Player")){
            ObjectPooler.Instance.SpawnFromPool("RocketEnemyDeath", transform.position, Quaternion.identity);
            AudioManager.instance.Play("EnemyDeath");
            gameObject.SetActive(false);
            ScreenShake.Shake(1f, .15f);
        }
    }

    void OnTriggerEnter2D(Collider2D col){
        if (col.CompareTag("Bullet") || col.CompareTag("BigBullet")){
            AudioManager.instance.Play("EnemyDeath");
            ObjectPooler.Instance.SpawnFromPool("RocketEnemyDeath", transform.position, Quaternion.identity);
            ScreenShake.Shake(1f, .15f);
            gameObject.SetActive(false);
        }
    }

    #region OnSpawn
    void StopAnimation(){
        anim.SetBool("Spawned", true);
    }

    void Ready(){
        Invoke(nameof(StartRunning), 1f);
        spawned = true;
        Invoke(nameof(EnableCollider), 0.5f);
        rocket.transform.rotation = Quaternion.Euler(Vector3.zero);
    }

    void EnableCollider(){
        polCol.enabled = true;
    }

    void StartRunning(){
        ready = true;
        particles.SetActive(true);
    }
    #endregion
}
