using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Video;

namespace Thousandto.UpdateForm.Movie
{
#if UNITY_STANDALONE_WIN
    /// <summary>
    /// 影片播放器
    /// </summary>
    public class PCMoviePlayer : IMoviePlayer
    {
        private string _fileName = null;
        private VideoPlayer _videoPlayer = null;
        private AudioSource _audioSource = null;
        private Renderer _renderer = null;
        private bool _isPlay = false;
        private GameObject _filmGo;
        private MovieAction _finishedCallBack;

        public PCMoviePlayer()
        {
            _filmGo = InitFilmGo();
            _videoPlayer = _filmGo.AddComponent<VideoPlayer>();
            _audioSource = _filmGo.AddComponent<AudioSource>();

            _videoPlayer.playOnAwake = false;
            _videoPlayer.renderMode = VideoRenderMode.MaterialOverride;
            _videoPlayer.targetMaterialRenderer = _filmGo.GetComponent<Renderer>();
            _videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
            _videoPlayer.SetTargetAudioSource(0, _audioSource);

            _renderer = _filmGo.GetComponent<Renderer>();
            _renderer.enabled = false;

            _videoPlayer.loopPointReached += OnVideoFinished;
        }

        public void Play(string fileName, MovieAction finishedCallBack)
        {
            if (!_isPlay)
            {
                _isPlay = true;
                _fileName = fileName;
                _finishedCallBack = finishedCallBack;
                _filmGo.SetActive(true);
                LoadAndPlayVideo();
            }
        }

        public void Stop()
        {
            _isPlay = false;
            Reset();
        }

        private void LoadAndPlayVideo()
        {
            string videoPath = Application.streamingAssetsPath + "/" + _fileName;
            if (!System.IO.File.Exists(videoPath))
            {
                Debug.LogError($"Video file not found at {videoPath}");
                Finished();
                return;
            }

            _videoPlayer.url = "file://" + videoPath;
            _videoPlayer.Prepare();
            _videoPlayer.prepareCompleted += OnVideoPrepared;
        }

        private void OnVideoPrepared(VideoPlayer source)
        {
            if (_isPlay)
            {
                _renderer.enabled = true;
                _videoPlayer.Play();
                _audioSource.Play();
            }
        }

        private void OnVideoFinished(VideoPlayer source)
        {
            Finished();
        }

        private void Reset()
        {
            _isPlay = false;

            if (_videoPlayer.isPlaying)
            {
                _videoPlayer.Stop();
            }

            if (_audioSource.isPlaying)
            {
                _audioSource.Stop();
            }

            _renderer.enabled = false;
            _filmGo.SetActive(false);
        }

        private void Finished()
        {
            Reset();
            _finishedCallBack?.Invoke();
        }

        private GameObject InitFilmGo()
        {
            var go = GameObject.CreatePrimitive(PrimitiveType.Plane);
            go.name = "[MoviePlayerGo]";
            go.AddComponent<AudioSource>();
            go.transform.position = Vector3.zero;
            go.transform.eulerAngles = Vector3.zero;

            float curAspect = (float)Screen.width / (float)Screen.height;
            go.transform.localScale = new Vector3(curAspect, 1, 1);

            var cameraGo = new GameObject("[MovieCamera]");
            cameraGo.transform.parent = go.transform;
            cameraGo.transform.localPosition = new Vector3(0, 10 * Mathf.Sin(Mathf.PI / 3f), 0);
            cameraGo.transform.localEulerAngles = new Vector3(90, 180, 0);
            cameraGo.transform.localScale = Vector3.one;

            var cam = cameraGo.AddComponent<Camera>();
            cam.backgroundColor = Color.black;
            cam.depth = 20;
            cam.farClipPlane = 20;

            cameraGo.AddComponent<AudioListener>();

            return go;
        }
    }
#endif
}