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

public class Computer : MonoBehaviour
{
    [SerializeField] private ComputerCoordinator computerCoordinator;
    [SerializeField] private GameObject hardDrivePrefab;
    [SerializeField] private Transform projectileSpawnTransform;
    [SerializeField] private float attackCooldown;
    [SerializeField] private int damage;
    [SerializeField] private float projectileLifeTime;
    [SerializeField] private SpriteRenderer faultIndicatorSpriteRenderer;
    [SerializeField] private Image fillImage;
    
    public bool CanShoot;
    
    private Transform _playerTransform;
    private Health _health;
    private BoxCollider2D _collider;
    private float _lastAttackTime = -999f;
    private bool isBrokenOnce = false;
    private void Awake()
    {
        _health = GetComponent<Health>();
        _collider = GetComponent<BoxCollider2D>();
    }

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        _health.OnHealthChanged += HandleHealthChanged;
        _playerTransform = GameObject.FindWithTag("Player").transform;
    }

    // Update is called once per frame
    void Update()
    {
        if (CanShoot)
        {
            ShootProjectile();
        }
        else
        {
            _lastAttackTime = -999f;
        }
    }

    private void OnDestroy()
    {
        _health.OnHealthChanged -= HandleHealthChanged;
    }

    private void ShootProjectile()
    {
        if (Time.time >= _lastAttackTime + attackCooldown)
        {
            Vector2 spawnPos = projectileSpawnTransform.position;
            GameObject projectile = Instantiate(hardDrivePrefab, spawnPos, Quaternion.identity);
            Projectile projectileScript = projectile.GetComponent<Projectile>();
            projectileScript.Direction = (_playerTransform.position - transform.position).normalized;
            projectileScript.projectileDamage = damage;
            Destroy(projectile, projectileLifeTime);

            _lastAttackTime = Time.time;
        }
    }
    
    public void StartComputer()
    {
        CanShoot = true;
        DeactivateCollider();
        faultIndicatorSpriteRenderer.color = Color.red;
    }
    
    public void BreakComputer()
    {
        CanShoot = false;
        ActivateCollider();
        faultIndicatorSpriteRenderer.color = Color.green;
    }
    
    private void ActivateCollider()
    {
        _collider.enabled = true;
    }

    private void DeactivateCollider()
    {
        _collider.enabled = false;
    }

    private void HandleHealthChanged(int currentHealth)
    {
        float t = (_health.GetMaxHealth() <= 0) ? 0f : (float)currentHealth / _health.GetMaxHealth();
        fillImage.fillAmount = t;

        if (!isBrokenOnce && currentHealth <= _health.GetMaxHealth() / 2)
        {
            //time to start the computer and deactivate the next one
            computerCoordinator.BreakNextComputer(currentHealth);
            isBrokenOnce = true;
            StartCoroutine(StartWithDelay());
        }

        if (currentHealth <= 0)
        {
            computerCoordinator.BreakNextComputer(currentHealth);
        }
    }

    private IEnumerator StartWithDelay()
    {
        yield return new WaitForSeconds(1.5f);
        StartComputer();
    }
    
}
