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

public class EnvironmentUIControl : MonoBehaviour
{
    public GameObject TargetControllerObj;
    public GameObject ParameterContainerObj;
    public GameObject GroundCanvasObj;
    public GameObject chartObj;
    public GameObject HUDObj;
    public TextMeshProUGUI remainTimeText;
    public TextMeshProUGUI targetTypeText;
    public TextMeshProUGUI winLoseText;
    public TextMeshProUGUI stateText;
    public float resultTimeout = 1f;
    public GameObject gaugeImgObj;

    private StringBuilder stateBuilder = new StringBuilder();
    private LineChart realTimeRewardChart = null;
    private TargetController targetController;
    private ParameterContainer paramContainer;
    private HUDController hudController;
    private Image gaugeImg;
    private float overTime = 0f;
    private int step = 0;
    private bool resultActive = false;

    // Start is called before the first frame update
    void Start()
    {
        targetController = TargetControllerObj.GetComponent<TargetController>();
        paramContainer = ParameterContainerObj.GetComponent<ParameterContainer>();
        gaugeImg = gaugeImgObj.GetComponent<Image>();
        hudController = HUDObj.GetComponent<HUDController>();
    }

    // Update is called once per frame
    void Update()
    {
        int remainTime = Convert.ToInt32(targetController.startTime + paramContainer.timeLimit - Time.time);
        remainTimeText.text = "RemainTime:" + remainTime.ToString();
        if (resultActive && Time.time - overTime >= resultTimeout)
        {
            // while result is active and show time over timeOut
            // clear the result
            Debug.Log("clear result");
            winLoseText.text = "";
            winLoseText.color = Color.white;
            resultActive = false;
        }
    }
    public void updateChart(float reward)
    {
        if (hudController.chartOn && realTimeRewardChart == null)
        {
            initChart();
        }
        step += 1;
        realTimeRewardChart.AddXAxisData(Convert.ToString(step));
        realTimeRewardChart.AddData(0, reward);
    }

    public void initChart()
    {
        if (hudController.chartOn && realTimeRewardChart == null)
        {
            Vector3 chartPos = new Vector3(-210f, 90f, 0f) * GroundCanvasObj.transform.localScale.x;
            realTimeRewardChart = chartObj.AddComponent<LineChart>();
            realTimeRewardChart.Init();
        }
        realTimeRewardChart.RemoveData();
        realTimeRewardChart.AddSerie<Line>("Rewards");
    }

    public void removeChart()
    {

        GameObject.Destroy(realTimeRewardChart);
        realTimeRewardChart = null;
    }

    // show result in UI
    public void showResult(int resultState)
    {
        switch (resultState)
        {
            case (int)TargetController.EndType.Win:
                //Win
                Debug.Log("win");
                winLoseText.text = "Win";
                winLoseText.color = Color.green;
                overTime = Time.time;
                resultActive = true;
                break;
            case (int)TargetController.EndType.Lose:
                //lose
                Debug.Log("lose");
                winLoseText.text = "Lose";
                winLoseText.color = Color.red;
                overTime = Time.time;
                resultActive = true;
                break;
            default:
                break;
        }
    }

    // update firebases target state gauge
    public void updateTargetGauge(float firebasesBelong, float belongMaxPoint)
    {
        if (firebasesBelong >= 0)
        {
            gaugeImgObj.transform.localPosition = new Vector3(25f, 0, 0);
            gaugeImg.color = Color.blue;
            gaugeImg.fillOrigin = 0;
            gaugeImg.fillAmount = firebasesBelong / belongMaxPoint;
        }
        else
        {
            gaugeImgObj.transform.localPosition = new Vector3(-25f, 0, 0);
            gaugeImg.color = Color.red;
            gaugeImg.fillOrigin = 1;
            gaugeImg.fillAmount = -firebasesBelong / belongMaxPoint;
        }
    }
    // update targetType text
    public void updateTargetType(int targetInt)
    {
        switch (targetInt)
        {
            case (int)TargetController.Targets.Go:
                targetTypeText.text = "GOTO";
                targetTypeText.color = Color.blue;
                break;
            case (int)TargetController.Targets.Attack:
                targetTypeText.text = "Attack!";
                targetTypeText.color = Color.red;
                break;
            case (int)TargetController.Targets.Defence:
                targetTypeText.text = "Defence";
                targetTypeText.color = Color.green;
                break;
            case (int)TargetController.Targets.Free:
                targetTypeText.text = "Free";
                targetTypeText.color = Color.yellow;
                break;
            default:
                targetTypeText.text = "TYPE ERROR";
                targetTypeText.color = Color.red;
                break;
        }
    }

    // update state text
    // public TextMeshProUGUI stateText;
    // targetState[0] = targetTypeInt;
    // targetState[1] = targetPosition.x / raySensors.viewDistance; // normalization
    // targetState[2] = targetPosition.y / raySensors.viewDistance;
    // targetState[3] = targetPosition.z / raySensors.viewDistance;
    // targetState[4] = blockCont.thisBlock.firebasesAreaDiameter / raySensors.viewDistance;
    // targetState[5] = blockCont.thisBlock.belongRatio;
    // float[] myObserve = { transform.localPosition.x/raySensors.viewDistance, transform.localPosition.y / raySensors.viewDistance, transform.localPosition.z / raySensors.viewDistance, transform.eulerAngles.y/360f }
    // ??????????string??"targetType: 1.0 \r\n targetX: 2.0 \r\n"
    public void updateStateText(float[] targetStates,float inAreaState,float remainTime, bool gunReadyToggle, float[] myObserve, float[] rayTagResultOnehot, float[] rayDisResult)
    {
        stateBuilder.Clear();

        stateBuilder.Append($"States:\r\nTType:{targetStates[0]}");
        stateBuilder.Append("\r\nTPosition:");
        stateBuilder.Append(targetStates[1]).Append(targetStates[2]).Append(targetStates[3]);
        stateBuilder.Append("\r\nTDiameter:");
        stateBuilder.Append(targetStates[4]);
        stateBuilder.Append("\r\nTRatio:");
        stateBuilder.Append(targetStates[5]);
        stateBuilder.Append("\r\nInArea:");
        stateBuilder.Append(inAreaState);
        stateBuilder.Append("\r\nRemainTime:");
        stateBuilder.Append(remainTime);
        stateBuilder.Append("\r\nGunReady:");
        stateBuilder.Append(gunReadyToggle);
        stateBuilder.Append("\r\nMyPosition:");
        stateBuilder.Append(myObserve[0]).Append(myObserve[1]).Append(myObserve[2]);
        stateBuilder.Append("\r\nMyRotation:");
        stateBuilder.Append(myObserve[3]);

        stateText.text = stateBuilder.ToString();
    }
}
