﻿using RenderHeads.Media.AVProVideo;
using System;
using UnityEngine;
using UnityEngine.UI;

namespace AsmodeeDigital.CrossPromo.Plugin.UI
{
    public class VideoContainer : Container
    {
        [Serializable]
        public class UI
        {
            public Image PlayImage;
            public DisplayUGUI VideoDisplay;
        }

        public UI ui;

        //private VideoDesc _videoDesc;
        private string _videoUrl;
        private PlayingVideoState _playingVideoState = PlayingVideoState.VideoNotLoaded;

        public void Init(GameDetailsPopup popup, string videoUrl)
        {
            base.Init(popup);

            //_videoDesc = videoDesc;
            _videoUrl = videoUrl;

            this.gameObject.SetActive(true);
            uiContainer.Loading.gameObject.SetActive(false);
            ui.PlayImage.gameObject.SetActive(true);

            _playingVideoState = PlayingVideoState.VideoNotLoaded;

            ui.VideoDisplay.CurrentMediaPlayer = GameObject.FindObjectOfType<MediaPlayer>();

            if (ui.VideoDisplay.CurrentMediaPlayer.Events.GetPersistentEventCount() == 0)
                ui.VideoDisplay.CurrentMediaPlayer.Events.AddListener(MediaPlayer_Event);
        }

        public void PlayButton_Clicked()
        {
            if (_playingVideoState == PlayingVideoState.VideoNotLoaded)
            {
                ui.VideoDisplay.CurrentMediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, _videoUrl);
                ui.VideoDisplay.CurrentMediaPlayer.Play();

                uiContainer.Loading.gameObject.SetActive(true);
                ui.PlayImage.gameObject.SetActive(false);
            }
            else if (_playingVideoState == PlayingVideoState.Playing)
            {
                SetPauseMode();

                _popup.Zoom(this);
            }
            //---> Pause
            else
            {
                //_playingVideoState = PlayingVideoState.Playing;
                ui.VideoDisplay.CurrentMediaPlayer.Play();

                ui.PlayImage.gameObject.SetActive(false);

                //_popup.Zoom(this);
            }
        }

        private void SetPauseMode()
        {
            _playingVideoState = PlayingVideoState.Pause;
            ui.VideoDisplay.CurrentMediaPlayer.Pause();

            ui.PlayImage.gameObject.SetActive(true);
        }

        public void MediaPlayer_Event(MediaPlayer mediaPlayer, MediaPlayerEvent.EventType eventType)
        {
            if (eventType == MediaPlayerEvent.EventType.Started)
            {
                ui.VideoDisplay.gameObject.SetActive(true);

                _playingVideoState = PlayingVideoState.Playing;
                uiContainer.Loading.gameObject.SetActive(false);

                _popup.Zoom(this);
            }
        }

        public override void Zoom(bool zoomed)
        {
            base.Zoom(zoomed);

            if(!zoomed)
            {
                SetPauseMode();
            }
        }

        protected override void OnDisable()
        {
            ui.VideoDisplay.gameObject.SetActive(false);
            ui.VideoDisplay.CurrentMediaPlayer.Stop();
            ui.VideoDisplay.CurrentMediaPlayer.CloseVideo();

            GameObject.Destroy(this.gameObject);
        }

        enum PlayingVideoState
        {
            VideoNotLoaded,
            Playing,
            Pause
        }
    }
}
