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

public class ScoreTable : MonoBehaviour
{
    public Text usernamePrefab;
    public Text bankPrefab;
    public Text winsPrefab;
    public Text losesPrefab;
    public Text wlPrefab;
    public RectTransform content;

    private void Start()
    {
        StartCoroutine(Callstats());
    }

    IEnumerator Callstats()
    {
        WWWForm form = new WWWForm();
        WWW www = new WWW("http://localhost/Table.php", form);
        yield return www;

        if (www.text[0] == '0')
        {
            // If no results were returned, do nothing
        }
        else
        {
            // Split the text returned by the PHP script into separate variables for username, bank, wins, and loses
            string[] lines = www.text.Split(new string[] { "<br>" }, System.StringSplitOptions.RemoveEmptyEntries);


            // Create new rows for each line of values
            float yOffset = 100;
            foreach (string line in lines)
            {
                string[] values = line.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries);

                if (values.Length >= 4)
                {
                    string usernameStr = values[0];
                    string bankStr = values[1];
                    string winsStr = values[2];
                    string losesStr = values[3];

                    // Divide the integers in winsStr and losesStr
                    int wins = int.Parse(winsStr);
                    int loses = int.Parse(losesStr);

                    // Check if loses is greater than 0, if not set it to 1
                    if (loses == 0)
                    {
                        loses = 1;
                    }

                    float wl = (float)wins / (float)loses;
                    string wlStr = wl.ToString("F2");

                    // Create new UI elements for each value
                    Text usernameText = Instantiate(usernamePrefab, content.transform);
                    usernameText.text = usernameStr;

                    Text bankText = Instantiate(bankPrefab, content.transform);
                    bankText.text = bankStr;

                    Text winsText = Instantiate(winsPrefab, content.transform);
                    winsText.text = winsStr;

                    Text losesText = Instantiate(losesPrefab, content.transform);
                    losesText.text = losesStr;

                    Text wlText = Instantiate(wlPrefab, content.transform);
                    wlText.text = wlStr;

                    // Position the new row of UI elements
                    Vector3 pos = new Vector3(-243, yOffset, 0);
                    usernameText.rectTransform.localPosition = pos;
                    bankText.rectTransform.localPosition = pos + new Vector3(150, 0, 0);
                    winsText.rectTransform.localPosition = pos + new Vector3(310, 0, 0);
                    losesText.rectTransform.localPosition = pos + new Vector3(430, 0, 0);
                    wlText.rectTransform.localPosition = pos + new Vector3(550, 0, 0);

                    // Increase the yOffset for the next row
                    yOffset -= 30;
                }
            }
        }
    }
}