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

/*????UI*/

public class UIController : MonoBehaviour
{
    
    [Header("Key Viewer")]
    public int updateStep = 600;
    public Text upText;
    public Text downText;
    public Text leftText;
    public Text rightText;
    public Text shootText;
    public Text MouseText;
    public Text remainTimeText;
    public Text remainEnemyText;
    public Image mouseVisualizationBG;
    public Image mouseVisualizationMeter;

    [Header("Reward Viewer")]
    public Text nonRewardText;
    public Text shootRewardText;
    public Text shootWithoutReadyRewardText;
    public Text hitRewardText;
    public Text winRewardText;
    public Text loseRewardText;
    public Text killRewardText;


    [Header("X_Charts")]
    public GameObject realTimeRewardChartOBJ;
    public GameObject realTimeKeyCounterChartOBJ;
    public GameObject EPTotalRewardsChartOBJ;

    [Header("Other Para")]
    public float mouseMaxMovement = 15;

    private LineChart realTimeRewardChart;
    private BarChart realTimeKeyCounterChart;
    private LineChart EPTotalRewardsChart;

    //-----------RewardViewer--------
    public void updateRewardViewer(float nonReward, float shootReward, float shootWithoutReadyReward, float hitReward, float winReward, float loseReward, float killReward)
    {
        nonRewardText.text = Convert.ToString(nonReward);
        shootRewardText.text = Convert.ToString(shootReward);
        shootWithoutReadyRewardText.text = Convert.ToString(shootWithoutReadyReward);
        hitRewardText.text = Convert.ToString(hitReward);
        winRewardText.text = Convert.ToString(winReward);
        loseRewardText.text = Convert.ToString(loseReward);
        killRewardText.text = Convert.ToString(killReward);
    }

    //------------RemainTime----------
    public void updateRemainTime(int remainTime)
    {
        remainTimeText.text = Convert.ToString(remainTime);
    }
    //------------RemainEnemy---------
    public void updateRemainEnemy(int enemyNum)
    {
        remainEnemyText.text = Convert.ToString(enemyNum);
    }

    //------------Key Viewer----------
    public void updateWASDKeyViewer(int vertical,int horizontal)
    {
        if (vertical == 1)
        {
            upText.color = Color.red;
            downText.color = Color.black;
        }
        else if (vertical == -1)
        {
            downText.color = Color.red;
            upText.color = Color.black;
        }
        else
        {
            downText.color = Color.black;
            upText.color = Color.black;
        }
        if (horizontal == 1)
        {
            rightText.color = Color.red;
            leftText.color = Color.black;
        }
        else if (horizontal == -1)
        {
            leftText.color = Color.red;
            rightText.color = Color.black;
        }
        else
        {
            downText.color = Color.black;
            upText.color = Color.black;
        }
    }
    public void updateShootKeyViewer(int shoot,bool isGunReady)
    {
        if(shoot != 0 && isGunReady == true)
        {
            shootText.color = Color.red;
        }
        else if(shoot != 0 && isGunReady == false)
        {
            shootText.color = Color.yellow;
        }
        else
        {
            shootText.color = Color.black;
        }
    }
    public void updateMouseMovementViewer(float Mouse_x)
    {
        MouseText.text = Mouse_x.ToString();
        float mouseBGWidth = mouseVisualizationBG.GetComponent<RectTransform>().sizeDelta.x;
        float mouseBGPosX = mouseVisualizationBG.GetComponent<RectTransform>().position.x;
        float mouseBGPosY = mouseVisualizationBG.GetComponent<RectTransform>().position.y;

        float mouseMeterWidth = mouseBGWidth * Mouse_x / mouseMaxMovement;
        float mouseMeterPositionOffset = mouseMeterWidth / 2;
        mouseVisualizationMeter.rectTransform.sizeDelta = new Vector2(Math.Abs(mouseMeterWidth),mouseVisualizationMeter.GetComponent<RectTransform>().sizeDelta.y);
        mouseVisualizationMeter.rectTransform.position = new Vector3(mouseBGPosX + mouseMeterPositionOffset, mouseBGPosY, 0);
    }

    // ------------X Chart------------
    // Initialize Chart UI
    public void iniChart()
    {
        realTimeRewardChart = realTimeRewardChartOBJ.GetComponent<LineChart>();
        resetStepChart();
        realTimeKeyCounterChart = realTimeKeyCounterChartOBJ.GetComponent<BarChart>();
        resetCounterChat();

        EPTotalRewardsChart = EPTotalRewardsChartOBJ.GetComponent<LineChart>();
        resetEPChart();
    }

    // Resert------------
    // reset setp by setp update Chart
    public void resetStepChart()
    {
        realTimeRewardChart.RemoveData();
        realTimeRewardChart.AddSerie<Line>("RealTimeRewardChart");
    }
    // reset keyCounter Chart
    public void resetCounterChat()
    {
        realTimeKeyCounterChart.RemoveData();
        realTimeKeyCounterChart.AddSerie<Bar>("RealTimeKeyCounterChart");
        realTimeKeyCounterChart.AddXAxisData("W");
        realTimeKeyCounterChart.AddXAxisData("S");
        realTimeKeyCounterChart.AddXAxisData("A");
        realTimeKeyCounterChart.AddXAxisData("D");
        realTimeKeyCounterChart.AddXAxisData("Pew");
        realTimeKeyCounterChart.AddData("RealTimeKeyCounterChart", 0, "W");
        realTimeKeyCounterChart.AddData("RealTimeKeyCounterChart", 0, "S");
        realTimeKeyCounterChart.AddData("RealTimeKeyCounterChart", 0, "A");
        realTimeKeyCounterChart.AddData("RealTimeKeyCounterChart", 0, "D");
        realTimeKeyCounterChart.AddData("RealTimeKeyCounterChart", 0, "Pew");
    }
    // reset EP by EP update Chart
    public void resetEPChart()
    {
        EPTotalRewardsChart.RemoveData();
        EPTotalRewardsChart.AddSerie<Line>("EPTotalRewardsChart");
    }

    // Update------------
    // update setp by setp update Type Chart
    public void stepUpdateChart(int step, float reward)
    {
        if(step % updateStep == 0)
        {
            resetStepChart();
        }
        realTimeRewardChart.AddXAxisData(Convert.ToString(step));
        realTimeRewardChart.AddData(0,reward);
    }
    // update  KeyCounter chart
    public void updateKeyCounterChart(int kWCount, int kSCount, int kACount, int kDCount,int shootCount)
    {
        realTimeKeyCounterChart.UpdateData(0, 0, kWCount);
        realTimeKeyCounterChart.UpdateData(0, 1, kSCount);
        realTimeKeyCounterChart.UpdateData(0, 2, kACount);
        realTimeKeyCounterChart.UpdateData(0, 3, kDCount);
        realTimeKeyCounterChart.UpdateData(0, 4, shootCount);
    }
    // update EP by EP update Type Chart
    public void epUpdateChart(int EP,float totalReward)
    {
        EPTotalRewardsChart.AddXAxisData(Convert.ToString(EP));
        EPTotalRewardsChart.AddData(0,totalReward);
    }
}