using TMPro;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif


public class GameController : MonoBehaviour {
    public GameObject prefab; // Dra in din prefab i Inspector
    public int numberOfPrefabs = 100; // Antal att skapa
    public Vector2 spawnAreaMin = new Vector2(-25f, -20f);
    public Vector2 spawnAreaMax = new Vector2(25f, 20f);
    public float minDistanceFromPlayer = 5f; // Minsta avstånd från spelaren
    public TMP_Text enemysLeftText, WinText, LooseText;
    public Slider healthBar;
    public Image barColor;
    
    int enemyLeft;
    int currentHealth = 100;

    private AudioSource audioSource;
    public AudioClip backgroundMusic;
    public AudioClip explosion;

    void Start() {
        audioSource = GetComponent<AudioSource>();
        audioSource.PlayOneShot(backgroundMusic);
        WinText.enabled = false;
        LooseText.enabled = false;
        SpawnPrefabs();
        enemyLeft = GameObject.FindGameObjectsWithTag("Enemy").Length;
        UpdateUI();
    }

    void SpawnPrefabs() {
        int spawned = 0;

        while (spawned < numberOfPrefabs) {
            float randomX = Random.Range(spawnAreaMin.x, spawnAreaMax.x);
            float randomY = Random.Range(spawnAreaMin.y, spawnAreaMax.y);
            Vector2 randomPosition = new Vector2(randomX, randomY);

            // Kontrollera avstånd till spelaren (0,0)
            if (Vector2.Distance(randomPosition, Vector2.zero) >= minDistanceFromPlayer) {
                Instantiate(prefab, randomPosition, Quaternion.identity);
                spawned++;
            }
        }
    }

    void Update() {
        if (currentHealth <= 0) {
            LooseText.enabled = true;
            RemoveAllEnemies();
            RemovePlayer();
            Time.timeScale = 0;
        }
        if(enemyLeft <= 0) {
            WinText.enabled = true;
            RemoveAllEnemies();
            RemovePlayer();
            Time.timeScale = 0;
        }
        
        if (Input.GetKeyDown(KeyCode.Q))
        {
            QuitGame();
        }
    }
    
    public static void QuitGame()
    {
#if UNITY_EDITOR
        // Stoppar Play Mode i Editor
        EditorApplication.isPlaying = false;
#else
        // Avslutar applikationen i build
        Application.Quit();
#endif
    }

    void RemoveAllEnemies() {
        GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
        foreach (GameObject enemy in enemies) {
            Destroy(enemy);
        }
    }
    void RemovePlayer() {
        GameObject player = GameObject.FindGameObjectWithTag("Player");
        if (player != null) {
            player.SetActive(false);
        }

    }

    public void KilledAnemy() {
        enemyLeft--;
        audioSource.PlayOneShot(explosion, 0.65f);
        UpdateUI();
    }
    
    void UpdateUI() {
        enemysLeftText.text = "LEFT: " + enemyLeft.ToString();
        healthBar.value = currentHealth;
    }

    public void DrawHealth() {
        currentHealth -= 15;
        if (currentHealth <= 60) {
            barColor.color = Color.orange;
        }
        else if (currentHealth <= 20) {
            barColor.color = Color.red;
        }
        UpdateUI();
    }
}