using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    
    public TextMeshProUGUI timeRemainingText;
    public TextMeshProUGUI orbsCollectedText;
    public GameObject player;
    public bool isGameActive;
    public GameObject GameOverScreen;
    public GameObject VictoryScreen;
    public GameObject Orb;
    public GameObject Enviroment;
    public MixerVolume Mixer;

    public float playerPosX;
    public float playerPosY;
    public float playerPosZ;
    
    public float playerRotX;
    public float playerRotY;
    public float playerRotZ;
    
    public int orbsCollected = 0;

    public float seed;

    public int orbsCount;
    public int enviromentCount;

    public List<GameObject> orbs;

    public List<GameObject> enviroments;

    public float timeRemaining = 120;

    public float Volume;
    
    
    
        
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        isGameActive = true;
        for (int i = 0; i < enviromentCount; i++)
        {
            enviroments.Add(Enviroment);
        }

        for (int i = 0; i < enviroments.Count; i++)
        {
            float randomX = Random.Range(-1200, 1200);
            float randomZ = Random.Range(-1200, 1200);
            Instantiate(enviroments[i], new Vector3(randomX, 100, randomZ), Quaternion.identity);
        }

        for (int i = 0; i < orbsCount; i++)
        {
            orbs.Add(Orb);
        }

        for (int i = 0; i < orbs.Count; i++)
        {
            float randomX = Random.Range(-1200, 1200);
            float randomZ = Random.Range(-1200, 1200);
            Instantiate(orbs[i], new Vector3(randomX, 100, randomZ), Quaternion.identity);
        }
    }

    

    public void RestartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
    public void GameOver()
    {
        GameOverScreen.SetActive(true);
        
        isGameActive = false;
        Time.timeScale = 0;
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
    }

    public void Victory()
    {
        VictoryScreen.SetActive(true);
        isGameActive = false;
        Time.timeScale = 0;
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
    }
        



        public void SaveGame()
        {
            SaveSystem.SaveGame(this);
        }



        public void LoadGame()
        {
            GameData data = SaveSystem.LoadGame();


            playerPosX = data.playerPosX;
            playerPosY = data.playerPosY;
            playerPosZ = data.playerPosZ;
            playerRotX = data.playerRotX;
            playerRotY = data.playerRotY;
            playerRotZ = data.playerRotZ;
            orbsCollected = data.orbsCollected;
            seed = data.seed;
            for (int i = 0; i < orbs.Count; i++)
            {
            orbs[i].transform.position = data.orbs[i].transform.position;
            }
            for (int i = 0; i < enviroments.Count; i++)
            {
                enviroments[i].transform.position = data.enviroments[i].transform.position;
            }
        isGameActive = data.isGameActive;
            timeRemaining = data.timeRemaining;
            Volume = data.Volume;
        }
    // Update is called once per frame
    void Update()
    {
        UpdateTimeRemaining();
        //Victory
        if (orbsCollected >= 20)
        {
            Victory();
        }
        
        //Game Over
        if (timeRemaining <= 0)
        {
            GameOver();
        }
        
        
        
        if(Input.GetKeyDown(KeyCode.F5))
        {
            SaveGame();
        }
        
        if(Input.GetKeyDown(KeyCode.F9))
        {
            LoadGame();
        }

        Mixer.SetMasterVolume(Volume);
    }

    public void UpdateTimeRemaining()
    {
        timeRemaining -= 1*Time.deltaTime;
        timeRemainingText.text = "RemainingTime: " +  timeRemaining;
    }

    public void UpdateCollectedOrbs()
    {
        
        orbsCollected++;
        orbsCollectedText.text = "Collected Orbs: " + orbsCollected + " / 20";
    }
}


