using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.Video;
using Object = UnityEngine.Object;

namespace NBF
{
    public class ZHWindow : EditorWindow
    {
        [MenuItem("Window/ZH窗口")]
        public static void ShowWindow()
        {
            GetWindow<ZHWindow>("Scene - ZH");
        }

        private bool _isInitialized;
        private VideoPlayer _videoPlayer;
        private RenderTexture _renderTexture;
        private long _lastInitTime;
        private int _videoIndex = 1;

        private void OnEnable()
        {
            InitializePlayer();
            // 注册更新回调
            EditorApplication.update += OnEditorUpdate;
        }

        private void InitializePlayer()
        {
            if (_isInitialized) return;
            _lastInitTime = DateTimeOffset.Now.ToUnixTimeSeconds();
            var obj = GameObject.Find("ZHVideoPlayer");
            if (obj == null) return;
            _videoPlayer = GameObject.Find("ZHVideoPlayer").GetComponent<VideoPlayer>();
            _videoPlayer.isLooping = true;
            _renderTexture = _videoPlayer.targetTexture;
            // 设置 RenderTexture
            // _renderTexture = new RenderTexture(408, 720, 0);
            _videoPlayer.renderMode = VideoRenderMode.RenderTexture;
            // _videoPlayer.targetTexture = _renderTexture;
            // _videoPlayer.playbackSpeed = 0.5f;

            // 加载视频
            string videoPath = $"ResRaw/Test/video{_videoIndex}.mp4"; // 修改为你的视频路径
            var path = System.IO.Path.Combine(Application.dataPath, videoPath);
            _videoPlayer.url = path;
            _videoPlayer.Prepare();
            _videoPlayer.Play();
            _isInitialized = true;
            _videoIndex++;
            if (_videoIndex > 8)
            {
                _videoIndex = 1;
            }
        }

        private void OnEditorUpdate()
        {
            if (_videoPlayer != null && _videoPlayer.isPrepared)
            {
                Repaint();
            }
        }

        private void OnGUI()
        {
            var currentTime = DateTimeOffset.Now.ToUnixTimeSeconds();
            if (currentTime - _lastInitTime > 30)
            {
                _isInitialized = false;
            }

            if (!_isInitialized)
            {
                // EditorGUILayout.HelpBox(
                //     "Video Player is not initialized. Please ensure the video file is properly set.",
                //     MessageType.Warning);
                InitializePlayer();
                return;
            }


            if (_videoPlayer == null || _renderTexture == null)
            {
                ReleaseResources();
                InitializePlayer();
                return;
            }

            if (!_videoPlayer.isPlaying)
            {
                _videoPlayer.Play();
            }

            if (_renderTexture != null)
            {
                float width = position.width;
                float height = position.height;

                // 计算视频显示比例
                float targetAspect = (float)_renderTexture.width / _renderTexture.height;
                float windowAspect = width / height;

                float displayWidth, displayHeight;
                if (windowAspect > targetAspect)
                {
                    displayHeight = height;
                    displayWidth = height * targetAspect;
                }
                else
                {
                    displayWidth = width;
                    displayHeight = width / targetAspect;
                }

                // 居中显示视频
                float offsetX = (width - displayWidth) / 2f;
                float offsetY = (height - displayHeight) / 2f;

                GUI.DrawTexture(new Rect(offsetX, offsetY, displayWidth, displayHeight), _renderTexture);
            }
        }

        private void OnDisable()
        {
            ReleaseResources();
            // 移除更新回调
            EditorApplication.update -= OnEditorUpdate;
        }

        private void ReleaseResources()
        {
            if (_videoPlayer != null)
            {
                _videoPlayer.Stop();
                // DestroyImmediate(_videoPlayer.gameObject);
                _videoPlayer = null;
            }

            _isInitialized = false;
        }
    }
}