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

public class CircleEnemyController : MonoBehaviour, IPooledObject{

    public float speed;
    public GameObject spikes2;
    [HideInInspector]
    public int hp, maxHp = 2;
    public GameObject spawnParticles;
    public Color level1Color;
    public Color level2Color;
    public Color level3Color;
    public SpriteRenderer[] spriteRenderers;

    GameController gameController;
    Animator anim;
    CircleCollider2D cirCol;
    bool ready;
    Rigidbody2D rb2d;
    Transform target;
    Vector2 targetPos;

    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>();
        anim.SetBool("Spawned", false);
        cirCol = GetComponent<CircleCollider2D>();
        cirCol.enabled = false;
        hp = maxHp;
        spikes2.SetActive(true);
        target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
        rb2d = GetComponent<Rigidbody2D>();
        ready = false;
        Invoke(nameof(StopAnimation), 1f);
        Invoke(nameof(Ready), 2f);
    }

    // Update is called once per frame
    void Update(){
        targetPos = target.position - transform.position;
    }

    void FixedUpdate(){
        if (ready){
            rb2d.MovePosition(rb2d.position + (targetPos.normalized * speed * Time.fixedDeltaTime));
        }
    }

    void OnTriggerEnter2D(Collider2D col){
        if (col.CompareTag("Bullet") && hp == 2){
            spikes2.SetActive(false);
            hp = 1;
        }else if(col.CompareTag("Bullet") && hp == 1){
            gameObject.SetActive(false);
            AudioManager.instance.Play("EnemyDeath");
            hp = 0;
            ScreenShake.Shake(1f, .15f);
            ObjectPooler.Instance.SpawnFromPool("CircleEnemyDeath", transform.position, Quaternion.identity);
        }
        if (col.CompareTag("BigBullet") && hp == 2){
            gameObject.SetActive(false);
            AudioManager.instance.Play("EnemyDeath");
            hp = 0;
            ScreenShake.Shake(1f, .15f);
            ObjectPooler.Instance.SpawnFromPool("CircleEnemyDeath", transform.position, Quaternion.identity);
        }else if (col.CompareTag("BigBullet") && hp == 1){
            gameObject.SetActive(false);
            AudioManager.instance.Play("EnemyDeath");
            hp = 0;
            ScreenShake.Shake(1f, .15f);
            ObjectPooler.Instance.SpawnFromPool("CircleEnemyDeath", transform.position, Quaternion.identity);
        }
    }

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

    void Ready(){
        Invoke(nameof(StartRunning), 0.5f);
        Invoke(nameof(EnableCollider), 0.25f);
    }

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

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