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

public class LevelManager : Singleton<LevelManager>{
    const string AIMTARGET = "Aim";
    const string START = "Start";
    const string END = "End";

    public Transform cam;
    [Space]
    public Material firstPlayer;
    public Material secondPLayer;
    public Material firstPlayerAIM;
    public Material secondPLayerAIM;
    [Space]
    public List<Weapon> weapons;
    [Space]
    public Animator startMenu;
    public Animator endMenu;
    public TextMeshProUGUI textMeshPro;

    List<GameObject> spawnedWeapon;
    

    PlayerInputManager playerManager;
    InputMaster inputs;
    bool gameStarted = false;

    void Awake(){
        inputs = new InputMaster();
        inputs.Player.Any.performed += ctx => startGame();
    }

    void Start() {
        inputs.Enable(); 
        playerManager = GetComponent<PlayerInputManager>(); 
        playerManager.enabled = false;
        spawnedWeapon = new List<GameObject>();
    }

    void startGame(){
        startMenu.SetTrigger(START);

        StartCoroutine(waitAndStartManager());
        inputs.Player.Any.performed -= ctx => startGame();
        inputs.Disable(); 
        gameStarted = true;
    }

    IEnumerator waitAndStartManager(){
        yield return new WaitForSeconds(1);
        playerManager.enabled = true;
    }

    void OnPlayerJoined(PlayerInput ctx){

        PlayerController pc = ctx.gameObject.GetComponent<PlayerController>();
        pc.id = ctx.playerIndex;

        if(ctx.playerIndex == 0){
            setCorrectMaterial(pc.transform, firstPlayer, firstPlayerAIM);
        }else{
            setCorrectMaterial(pc.transform, secondPLayer, secondPLayerAIM);
        }

    }

    void setCorrectMaterial(Transform parent, Material m, Material aim){
        Transform[] allChildren = parent.GetComponentsInChildren<Transform>();
        foreach (Transform child in allChildren) {
            MeshRenderer mr = child.GetComponent<MeshRenderer>();
            if(mr != null){
                if(!child.CompareTag(AIMTARGET)){
                    mr.sharedMaterial = m;
                }else{
                    mr.sharedMaterial = aim;
                }
            }
        }
    }

    public void gameOver(int id){
        endMenu.SetTrigger(END);
        string winner = "one";
        if(id == 0){
            winner = "two";
        }
        textMeshPro.text = "Player " + winner + " wins!";
        StartCoroutine(waitAndRestart());
    }

    IEnumerator waitAndRestart(){
        yield return new WaitForSeconds(3);
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
    

    void Update() {
       if(playerManager.playerCount >= 2){
           playerManager.enabled = false;
       }

        if(gameStarted){

            var r = Random.Range(1, 2000);
            if(r <= 10){
                spawnWeapon();
            }

        }
    }

    void spawnWeapon(){
        int r = Random.Range(0, 2);
        Vector2 weaponPos = Random.insideUnitCircle * 30;
        Vector3 desiredPos = new Vector3(
            weaponPos.x,
            1.5f,
            weaponPos.y
        );

        spawnedWeapon.Add(
            Instantiate(weapons[r], desiredPos, Quaternion.identity).gameObject
        );
        
    }
}
