using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XCharts.Runtime;

public class WorldUIController : MonoBehaviour
{
    public LineChart WinChart;
    public int[] totalGames;
    public int[] winGames;

    private int maxXAxis = 0;
    // Start is called before the first frame update
    void Start()
    {
        totalGames = new int[(int)TargetController.Targets.Num];
        winGames = new int[(int)TargetController.Targets.Num];
        Array.Clear(totalGames, 0, (int)TargetController.Targets.Num);
        Array.Clear(winGames, 0, (int)TargetController.Targets.Num);
        //WinChart.Init();
        WinChart.RemoveData();
        for (int i = 0; i < (int)TargetController.Targets.Num; i++)
        {
            string lineName = Enum.GetName(typeof(TargetController.Targets), i);
            WinChart.AddSerie<Line>(lineName);
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
    public void updateChart(int targetType, int endType)
    {
        float winRatio = 0f;
        switch (endType)
        {
            case (int)TargetController.EndType.Win:
                //Win
                totalGames[targetType] += 1;
                winGames[targetType] += 1;
                winRatio = (float)winGames[targetType] / totalGames[targetType];
                WinChart.AddData(targetType, winRatio);
                if (totalGames[targetType] > maxXAxis)
                {
                    maxXAxis = totalGames[targetType];
                    WinChart.AddXAxisData(Convert.ToString(maxXAxis));
                }
                break;
            case (int)TargetController.EndType.Lose:
                //lose
                totalGames[targetType] += 1;
                winRatio = (float)winGames[targetType] / totalGames[targetType];
                WinChart.AddData(targetType, winRatio);
                if (totalGames[targetType] > maxXAxis)
                {
                    maxXAxis = totalGames[targetType];
                    WinChart.AddXAxisData(Convert.ToString(maxXAxis));
                }
                break;
            default:
                break;
        }

    }
}
