using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SmartAquaViewer.View
{
    /// <summary>
    /// CCTVView.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class CCTVView : UserControl
    {
        private Thread _videoThread;
        private Thread _renderingThread;
        private CancellationTokenSource _videoCancellationTokenSource;
        private CancellationTokenSource _renderingCancellationTokenSource;
        private bool _stopThread = false;

        private ConcurrentQueue<Bitmap> _frameQueue = new ConcurrentQueue<Bitmap>();

        private readonly object _lockObject = new object();

        public CCTVView()
        {
            InitializeComponent();
        }

        public void StartMedia(string rtspURL)
        {
            ClosePlayer();
            _stopThread = false;

            _videoThread = new Thread(new ThreadStart(OpenMedia));
            _renderingThread = new Thread(new ThreadStart(RenderImage));

            _videoThread.Priority = ThreadPriority.Highest; // 우선순위 설정
            _videoThread.Start();

            _renderingThread.Start();
        }

        private void OpenMedia()
        {
            throw new NotImplementedException();
        }

        private void RenderImage()
        {
            throw new NotImplementedException();
        }

        

        public void ClosePlayer()
        {
            _stopThread = true;

            lock (_lockObject)
            {
                if (_videoCancellationTokenSource != null)
                {
                    _videoCancellationTokenSource.Cancel();
                    Debug.WriteLine("ClosePlayer(): videoThread 종료함");
                }

                if (_renderingCancellationTokenSource != null)
                {
                    _renderingCancellationTokenSource.Cancel();
                    Debug.WriteLine("ClosePlayer(): RenderingThread 종료함");
                }
            }
        }
    }
}
