using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using RenderHeads.Media.AVProVideo;

public class SLogicManagerVideo : MonoBehaviour
{
    public MediaPlayer _mediaPlayer;

    public Slider _videoSeekSlider;
    public Slider _audioVolumeSlider;
    private bool _wasPlayingOnScrub;
    private float _setVideoSeekSliderValue;
    private float _setAudioVolumeSliderValue;
    public RectTransform _bufferedSliderRect;
    private Image _bufferedSliderImage;
    //public string m_VideoPath;
    //public MediaPlayer.FileLocation _location = MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder;
    //public Toggle _AutoStartToggle;

    void Awake()
    {
        //_mediaPlayer.m_VideoPath = "http://meng.wuyou-zuida.com/20191226/24687_6b7d549c/index.m3u8";
        SLogicManager logicManager = GetComponent<SLogicManager>();
        Debug.Log("getVideoUrl --> " + logicManager.getVideoUrl());
        _mediaPlayer.m_VideoPath = logicManager.getVideoUrl();
    }

    void Start()
    {
        if (_bufferedSliderRect != null)
        {
            _bufferedSliderImage = _bufferedSliderRect.GetComponent<Image>();
        }
    }

    void Update()
    {
        if (_mediaPlayer && _mediaPlayer.Info != null && _mediaPlayer.Info.GetDurationMs() > 0f)
        {
            float time = _mediaPlayer.Control.GetCurrentTimeMs();
            float duration = _mediaPlayer.Info.GetDurationMs();
            float d = Mathf.Clamp(time / duration, 0.0f, 1.0f);

            _setVideoSeekSliderValue = d;
            _videoSeekSlider.value = d;

            if (_bufferedSliderRect != null)
            {
                if (_mediaPlayer.Control.IsBuffering())
                {
                    float t1 = 0f;
                    float t2 = _mediaPlayer.Control.GetBufferingProgress();
                    if (t2 <= 0f)
                    {
                        if (_mediaPlayer.Control.GetBufferedTimeRangeCount() > 0)
                        {
                            _mediaPlayer.Control.GetBufferedTimeRange(0, ref t1, ref t2);
                            t1 /= _mediaPlayer.Info.GetDurationMs();
                            t2 /= _mediaPlayer.Info.GetDurationMs();
                        }
                    }

                    Vector2 anchorMin = Vector2.zero;
                    Vector2 anchorMax = Vector2.one;

                    if (_bufferedSliderImage != null &&
                        _bufferedSliderImage.type == Image.Type.Filled)
                    {
                        _bufferedSliderImage.fillAmount = d;
                    }
                    else
                    {
                        anchorMin[0] = t1;
                        anchorMax[0] = t2;
                    }

                    _bufferedSliderRect.anchorMin = anchorMin;
                    _bufferedSliderRect.anchorMax = anchorMax;
                }
            }
        }
    }

    public void OnPlayButton()
    {
        if (_mediaPlayer)
        {
            _mediaPlayer.Control.Play();
        }
    }

    public void OnPauseButton()
    {
        if (_mediaPlayer)
        {
            _mediaPlayer.Control.Pause();
        }
    }

    public void OnVideoSeekSlider()
    {
        if (_mediaPlayer && _videoSeekSlider && _videoSeekSlider.value != _setVideoSeekSliderValue)
        {
            _mediaPlayer.Control.Seek(_videoSeekSlider.value * _mediaPlayer.Info.GetDurationMs());
        }
    }

    public void OnVideoSliderDown()
    {
        _wasPlayingOnScrub = _mediaPlayer.Control.IsPlaying();
        if (_wasPlayingOnScrub)
        {
            _mediaPlayer.Control.Pause();
        }
        OnVideoSeekSlider();
}

    public void OnVideoSliderUp()
    {
        if (_mediaPlayer && _wasPlayingOnScrub)
        {
            _mediaPlayer.Control.Play();
            _wasPlayingOnScrub = false;
        }
    }

    public void OnAudioVolumeSlider()
    {
        if (_mediaPlayer && _audioVolumeSlider && _audioVolumeSlider.value != _setAudioVolumeSliderValue)
        {
            _mediaPlayer.Control.SetVolume(_audioVolumeSlider.value);
        }
        if (_mediaPlayer && _audioVolumeSlider && _audioVolumeSlider.value != _setAudioVolumeSliderValue)
        {
            _mediaPlayer.Control.SetVolume(_audioVolumeSlider.value);
        }
    }
}
