using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public enum LevelType
{
    Sausage,
    Bottle,
    Knife
}

public class SausageSystem : MonoBehaviour
{
    public GameObject sausagePrefab;
    private PlayerInputManager playerInputManager;
    public GameObject sausageBox;

    public int players;

    private bool spawned;

    public Text winnerText;

    private float cptEnd;

    public AudioSource zic;

    public LevelType levelType;

    // Start is called before the first frame update
    void Start()
    {
        playerInputManager = GetComponent<PlayerInputManager>();
        playerInputManager.onPlayerJoined += PlayerInputManager_onPlayerJoined;
        StartCoroutine(SpawnSausages());
        foreach (var playerdata in PlayersTracking.Instance.players)
        {
            Debug.Log("Join with " + playerdata.Idx);
            playerInputManager.JoinPlayer(playerdata.Idx, -1, null, playerdata.Device);
        }
        if(PlayersTracking.Instance.players.Count == 0)
        {
            playerInputManager.JoinPlayer();
            PlayersTracking.Instance.AddPlayer(0, null, "Magenta");
        }
        winnerText.text = "";
    }

    private void PlayerInputManager_onPlayerJoined(PlayerInput obj)
    {
        Debug.Log("On Joined for " + obj.playerIndex);
        obj.GetComponent<SausageGamePlay>().Init(obj.playerIndex);
    }

    // Update is called once per frame
    void Update()
    {
        if(sausageBox.transform.childCount == 0 && spawned)
        {            
            int best = PlayersTracking.Instance.players.Max(p => p.SausageScore);
            PlayerData winner = PlayersTracking.Instance.players.Where(p => p.SausageScore == best).First();
            foreach (var p in PlayersTracking.Instance.players) {
                Debug.Log(p.PlayerName + " " + p.SausageScore);
            }
            if (levelType == LevelType.Knife)
            {
                winnerText.text = winner.PlayerName + " wins with " + winner.SausageScore + "  ! BUT YOU ARE ALL DEAD ANYWAY.....";
            }
            else
            {
                winnerText.text = winner.PlayerName + " wins with " + winner.SausageScore + "  ! We need to go deeper into madness.";
            }
            cptEnd += Time.deltaTime;
            winnerText.color = new Color(Mathf.Abs(Mathf.Sin(cptEnd*2f)), 0, 0);
            winnerText.transform.localScale = Vector3.one * Mathf.Abs(Mathf.Sin(cptEnd/2f));
            if(cptEnd > 5)
            {
                SceneManager.LoadScene(levelType == LevelType.Sausage?2:(levelType == LevelType.Bottle?3:0));
            }
        }
        if(levelType == LevelType.Bottle && spawned)
        {
            zic.pitch = 0.8f + (sausageBox.transform.childCount / (25f*PlayersTracking.Instance.players.Count))/5f;
        }
        if(levelType == LevelType.Knife)
        {
            zic.pitch = 0.5f + (sausageBox.transform.childCount / (25f * PlayersTracking.Instance.players.Count)) / 2f;            
        }
    }

    IEnumerator SpawnSausages()
    {
        yield return new WaitForSeconds(7f);
        
        for (int i = 0; i < 17 * PlayersTracking.Instance.players.Count; i++)
        {
            var s = Instantiate(sausagePrefab, Vector3.up * 10f + this.transform.position, Quaternion.identity);
            s.transform.parent = sausageBox.transform;
            yield return new WaitForSeconds(0.25f);
        }
        spawned = true;
    }
}
