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

using TMPro;

public class RemoteVideoControl : MonoBehaviour
{
    public GameObject Icon;
    public GameObject Stop;
    bool? isStart = true;
  //  public VideoPlayer videoPlayer;
    public AVProVideoPlayer avProVideoPlayer;
    public Slider slider;
    //当前时间
    public Text currentTime;
    //影片时间
    public Text textTotalTime;
    //是否获取总时长
    private bool IsStart;
    //时 分的转换
    private int hour, mint;
    private float time;
 
    public void VideoStartSwitch()
    {
        // isStart = videoPlayer.isPlaying;
        isStart = avProVideoPlayer.IsPlaying();
        isStart = !isStart;
        //Icon.SetActive(!isStart.Value);
        //Stop.SetActive(isStart.Value);
        if (isStart.Value)
        {
            //  videoPlayer?.Play();
            avProVideoPlayer?.Play();
           // slider.value = 0;
        }
        else
        {
         //   videoPlayer?.Pause();
            avProVideoPlayer?.Pause();
        }
    }

    private void Start()
    {
        IsStart = true;
        slider.onValueChanged.AddListener(delegate { ChangeVideo(slider.value); });

        StartCoroutine(VideoTimer());
        StartCoroutine(VideoState());

        
    }

    public void ChangeVideo(float value)
    {
        //if (avProVideoPlayer.IsVideoReady())
        //{
        //    float tempvalue = max * value;
        //    time = tempvalue;
        //    hour = (int)time / 60;
        //    mint = (int)time % 60;
        //    currentTime.text = string.Format("{0}:{1}", hour.ToString("00"), mint.ToString("00"));
        //}
    }

    float max;

    private IEnumerator VideoState()
    {
        while(true)
        {
            yield return new WaitForSeconds(0.1f);

            switch (avProVideoPlayer.mediaPlayer.m_CurrentState)
            {
                case MediaPlayerCtrl.MEDIAPLAYER_STATE.NOT_READY:
                    if (!Icon.activeSelf)
                    {
                        Icon.SetActive(true);
                        Stop.SetActive(false);
                    }
                    break;
                case MediaPlayerCtrl.MEDIAPLAYER_STATE.READY:
                    break;
                case MediaPlayerCtrl.MEDIAPLAYER_STATE.END:
                    if (!Icon.activeSelf)
                    {
                        Icon.SetActive(true);
                        Stop.SetActive(false);
                    }
                    break;
                case MediaPlayerCtrl.MEDIAPLAYER_STATE.PLAYING:
                    if (Icon.activeSelf)
                    {
                        Icon.SetActive(false);
                        Stop.SetActive(true);
                    }
                    break;
                case MediaPlayerCtrl.MEDIAPLAYER_STATE.PAUSED:
                    if (!Icon.activeSelf)
                    {
                        Icon.SetActive(true);
                        Stop.SetActive(false);
                    }
                    break;
                case MediaPlayerCtrl.MEDIAPLAYER_STATE.STOPPED:
                    if (!Icon.activeSelf)
                    {
                        Icon.SetActive(true);
                        Stop.SetActive(false);
                    }
                    break;
                case MediaPlayerCtrl.MEDIAPLAYER_STATE.ERROR:
                    if (!Icon.activeSelf)
                    {
                        Icon.SetActive(true);
                        Stop.SetActive(false);
                    }
                    break;
                default:
                    break;
            }
        }
    }

    private IEnumerator VideoTimer()
    {
        while (true)
        {
            yield return new WaitForSeconds(0.5f);
            if (avProVideoPlayer.IsVideoReady()&& !AVideoSlide.isDown)
            {
                float maxTimer = avProVideoPlayer.GetMaxTimer();
                float nowTimer = avProVideoPlayer.GetNowTimer();
                textTotalTime.text = string.Format("{0}:{1}", (maxTimer / 60).ToString("00"), (maxTimer % 60).ToString("00"));
                currentTime.text = string.Format("{0}:{1}", (nowTimer / 60).ToString("00"), (nowTimer % 60).ToString("00"));

              //  if(slider.value!=1)
              float value = nowTimer / maxTimer;

                if (value == float.NaN|| value.ToString() =="NaN")
                    value = 0.1f;
                else if( slider.value != value)
                    slider.value = value;

                //if (textTotalTime.text == currentTime.text)
                //{
                //    Icon.SetActive(true);
                //    Stop.SetActive(false);
                //}
            }

        }
    }
    //void Update()
    //{
    //    if (IsStart && avProVideoPlayer.IsVideoReady())
    //    {
    //        //max = avProVideoPlayer.GetMaxTimer();
    //        //time = max;
    //        //hour = (int)time / 60;
    //        //mint = (int)time % 60;
    //        // textTotalTime.text = string.Format("/ {0:D2}:{1:D2}", hour.ToString(), mint.ToString());
    //        textTotalTime.text = string.Format("{0}:{1}",avProVideoPlayer.GetNowTimer().ToString("00"), avProVideoPlayer.GetMaxTimer().ToString("00"));
    //        IsStart = !IsStart;
    //    }
    //}
}