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

public class BigEnemyController : MonoBehaviour, IPooledObject{

    public float speed;
    public GameObject healthBar;
    public GameObject enemy;
    public float bulletDamage;
    public float bigBulletDamage;
    public GameObject spawnParticles;
    public Color level1Color;
    public Color level2Color;
    public Color level3Color;
    public SpriteRenderer spriteRenderer;

    Animator anim;
    bool ready;
    bool spawned;
    BoxCollider2D boxCol;
    public int maxHp;
    float hp;

    GameController gameController;
    Rigidbody2D rb2d;
    Transform target;
    Vector2 targetPos;

    public void OnObjectSpawn(){
        gameController = FindObjectOfType<GameController>();
        if (gameController.level == 1 || gameController.level == 0){
            spriteRenderer.color = level1Color;
            ParticleSystem ps = spawnParticles.GetComponent<ParticleSystem>();
            var main = ps.main;
            main.startColor = level1Color;
        }
        if (gameController.level == 2){
            spriteRenderer.color = level2Color;
            ParticleSystem ps = spawnParticles.GetComponent<ParticleSystem>();
            var main = ps.main;
            main.startColor = level2Color;
        }
        if (gameController.level == 3){
            spriteRenderer.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);
        Invoke(nameof(Ready), 2f);
        ready = false;
        spawned = false;
        enemy.transform.rotation = Quaternion.Euler(Vector3.zero);
        boxCol = GetComponent<BoxCollider2D>();
        boxCol.enabled = false;
        spawnParticles.SetActive(true);
        spawnParticles.GetComponent<ParticleSystem>().Play();
        hp = maxHp;
        healthBar.transform.localScale = new Vector2(hp / maxHp, healthBar.transform.localScale.y);
        target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
        rb2d = GetComponent<Rigidbody2D>();
    }

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

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

    void OnTriggerEnter2D(Collider2D col){
        if(col.CompareTag("Bullet")){
            if(hp > 0 && hp > bulletDamage){
                hp -= bulletDamage;
                healthBar.transform.localScale = new Vector2(hp / maxHp, healthBar.transform.localScale.y);
            }else if(hp > 0 && hp <= bulletDamage){
                hp = 0;
                ScreenShake.Shake(1f, .15f);
                AudioManager.instance.Play("EnemyDeath");
                ObjectPooler.Instance.SpawnFromPool("EnemyDeath", transform.position, Quaternion.identity);
                healthBar.transform.localScale = new Vector2(hp / maxHp, healthBar.transform.localScale.y);
                gameObject.SetActive(false);
            }
            else if(hp <= 0){
                ScreenShake.Shake(1f, .15f);
                AudioManager.instance.Play("EnemyDeath");
                ObjectPooler.Instance.SpawnFromPool("EnemyDeath", transform.position, Quaternion.identity);
                gameObject.SetActive(false);
            }
        }
        if (col.CompareTag("BigBullet")){
            if (hp > 0 && hp > bulletDamage){
                hp -= bigBulletDamage;
                healthBar.transform.localScale = new Vector2(hp / maxHp, healthBar.transform.localScale.y);
            }else if (hp > 0 && hp <= bulletDamage){
                hp = 0;
                ScreenShake.Shake(1f, .15f);
                AudioManager.instance.Play("EnemyDeath");
                ObjectPooler.Instance.SpawnFromPool("EnemyDeath", transform.position, Quaternion.identity);
                healthBar.transform.localScale = new Vector2(hp / maxHp, healthBar.transform.localScale.y);
                gameObject.SetActive(false);
            }else if (hp == 0){
                ScreenShake.Shake(1f, .15f);
                AudioManager.instance.Play("EnemyDeath");
                ObjectPooler.Instance.SpawnFromPool("EnemyDeath", transform.position, Quaternion.identity);
                gameObject.SetActive(false);
            }
        }
    }
    #region OnSpawn
    void StopAnimation(){
        anim.SetBool("Spawned", true);
    }

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

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

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