
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
using TMPro;
using NeonTea.Quakeball.Networking;
using NeonTea.Quakeball.Game;
using System.Collections.Generic;

namespace NeonTea.Quakeball.Interface {
    public class StatScreen : MonoBehaviour {

        public Color RowColor = new Color();
        public RectTransform BackroundPanel;
        public GameObject RowPrefab;
        public bool ForceOpen;

        public Color SunColor = new Color();
        public Color MoonColor = new Color();

        private bool Open;

        private InputAction OpenStatScreenAction;

        private List<RectTransform> Rows = new List<RectTransform>();
        private bool LastRowIsDark = true;
        private Color DarkColor;

        private int LastRowAmount = 0;
        private float LastUpdate;

        public void Start() {
            OpenStatScreenAction = new InputAction("Open stats", binding: "<Keyboard>/tab");
            OpenStatScreenAction.Enable();

            DarkColor = new Color(0, 0, 0, 0);
            DarkColor.a = RowColor.a;
            BackroundPanel.gameObject.SetActive(false);
        }

        public void Update() {
            if (OpenStatScreenAction.ReadValue<float>() > 0 || ForceOpen) {
                Open = true;
            } else {
                Open = false;
            }
            BackroundPanel.gameObject.SetActive(Open);
            if (Open && (Time.time - LastUpdate) > 0.5f && Net.Singleton.Instance != null) {
                UpdateRows();
                LastUpdate = Time.time;
            }
        }

        public void UpdateRows() {
            int rowAmount = Net.Singleton.Instance.PlayerList.Count;
            if (rowAmount != LastRowAmount) {
                UpdateRowAmount();
                LastRowAmount = rowAmount;
            }
            for (int i = 0; i < rowAmount; i++) {
                NetPlayer player = Net.Singleton.Instance.PlayerList[i];
                Color playerColor = new Color(1, 1, 1, 1);
                switch (player.Team) {
                    case Team.Sun:
                        playerColor = SunColor;
                        break;
                    case Team.Moon:
                        playerColor = MoonColor;
                        break;

                }
                TMP_Text NickText = Rows[i].GetChild(0).GetComponent<TMP_Text>();
                NickText.color = playerColor;
                NickText.text = player.Nick;
                int ping = (int)(player.Ping * 1000);
                Rows[i].GetChild(1).GetComponent<TMP_Text>().text = $"{ping} ms";
            }

        }

        public void UpdateRowAmount() {
            int rowAmount = Net.Singleton.Instance.PlayerList.Count;
            while (Rows.Count < rowAmount) {
                AddRow();
            }
            while (Rows.Count > rowAmount) {
                RemoveRow();
            }
        }

        public void AddRow() {
            GameObject rowobj = GameObject.Instantiate(RowPrefab);
            rowobj.transform.parent = BackroundPanel;

            RectTransform row = rowobj.GetComponent<RectTransform>();
            Rows.Add(row);
            Vector2 offsetMin = row.offsetMin;
            offsetMin.x = 0;
            row.offsetMin = offsetMin;

            Vector2 offsetMax = row.offsetMax;
            offsetMax.x = 0;
            row.offsetMax = offsetMax;

            Vector2 pos = row.anchoredPosition;
            pos.y = 20 - (row.rect.height * Rows.Count);
            row.anchoredPosition = pos;

            Image rowImage = row.GetComponent<Image>();
            if (LastRowIsDark) {
                rowImage.color = RowColor;
            } else {
                rowImage.color = DarkColor;
            }
            LastRowIsDark = !LastRowIsDark;
        }

        public void RemoveRow() {
            if (Rows.Count > 0) {
                GameObject.Destroy(Rows[Rows.Count - 1].gameObject);
                Rows.RemoveAt(Rows.Count - 1);
                LastRowIsDark = !LastRowIsDark;
            }
        }
    }
}