using EC.Entity.Video;
using EC.Onvif;
using LibVLCSharp.Shared;
using System;
using System.Collections.Generic;
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.Navigation;
using System.Windows.Shapes;
namespace ECMonitor.UC.Videos
{
    /// 
    /// UCRtspVlcVideo.xaml 的交互逻辑
    /// 
    public partial class UCRtspVlcVideo : UserControl,IUCPlayer
    {
        
        LibVLC _libVLC;
        MediaPlayer _mediaPlayer;
        public string Message { get; set; }
        public MonitorCamera Camera { get; set; }
        private OnvifClient _onvifClient { get; set; }
        public int VideoType { get; set; }
        /// 
        /// 更新时间
        /// 
        private DateTime upDateTime;
        
        public UCRtspVlcVideo(double width, double height, int index,  string aspectRatio = "4:3") 
        {
            this.Width = width;
            this.Height = height;
            InitializeComponent();
            this.Name = "UCVideoPlay" +index;
            
            // "--no-audio" 没有声音
            _libVLC = new LibVLC(enableDebugLogs: true, "--rtsp-frame-buffer-size=0", "--no-audio", "--quiet-synchro");
            _mediaPlayer = new MediaPlayer(_libVLC);
            _mediaPlayer.AspectRatio = aspectRatio;
            videoView1.MediaPlayer = _mediaPlayer;
           
           
        }
        
         
        public void ReSetSize(double width, double height)
        {
            Width = width;
            Height = height;
        }
       
        /// 
        /// 播放
        /// 
        /// IP地址
        /// 用户
        /// 密码
        //public void Play(string ip,string userName,string passWord)
        //{
        //    this.Ip = ip;
        //    string url = string.Format(RTSPUrlFormat, userName, passWord, ip);
        //    Play(url);
        //}
        public bool Play(MonitorCamera camera)
        {
            if (IsPlaying())
            {
                Stop();
            }
            Camera = camera;
            if (Camera == null)
            {
                return false;
            }
            videoView1.Height = this.ActualHeight - 3;
            videoView1.Width = this.ActualWidth - 3;
            
            string url = Camera.RtspURL();
            var media = new Media(_libVLC, new Uri(url), "--newwork-caching=0", "--rtsp-frame-buffer-size=0", "--no-audio");
            bool play= _mediaPlayer.Play(media);
            //_mediaPlayer.ToggleFullscreen();
            return play;
        }
        public bool Play()
        {
            
            if (Camera == null)
            {
                return false;
            }
            bool play = _mediaPlayer.Play();
            _mediaPlayer.ToggleFullscreen();
            return play;
        }
        public void Stop()
        {
            _mediaPlayer?.Stop();
        }
       
        public void UPDateTime()
        {
            upDateTime = new DateTime();
        }
        /// 
        /// 判断预警是否接触
        /// 
        /// 
        public bool IsTimeOut()
        {
            DateTime now = new DateTime();
            double difSeconds = DateUnit.DateDiffSeconds(upDateTime, now);
            return difSeconds > 30;
        }
        public string GetCameraIp() {
            if (Camera != null)
            {
                return Camera.Ip;
            }
            return "";
        }
        /// 
        /// 播放器类型
        /// 
        /// 
        public int GetPlayerType()
        {
            return VideoType;
        }
        
        /// 
        /// 相机
        /// 
        /// 
        public MonitorCamera GetCamera()
        {
            return Camera;
        }
        public OnvifClient GetOnvifClient()
        {
            return _onvifClient;
        }
        public UserControl GetControl()
        {
            return this;
        }
        public string GetMessage()
        {
            return Message;
        }
        public string GetName()
        {
            return this.Name;
        }
        /// 
        /// 是否是空播放器
        /// 
        /// 
        public bool IsNullPlay()
        {
            return Camera == null;
        }
        /// 
        /// 是否正在播放
        /// 
        /// 
        public bool IsPlaying()
        {
            if (Camera == null|| _mediaPlayer==null)
            {
                return false;
            }
            return _mediaPlayer.IsPlaying;
        }
        private int listIndex = 0;
        public int GetIndex()
        {
            return listIndex;
        }
        public void SetIndex(int index)
        {
            listIndex = index;
        }
        /// 
        /// 是否显示
        /// 
       private  bool IsShow = false;
        public event EventHandler PlayerMouseDown;
        public event EventHandler PlayerMouseDoubleClick;
        public event EventHandler OnFromClose;
        public bool GetIsShow()
        {
            return IsShow;
        }
        public void SetIsShow(bool isShow)
        {
            IsShow = isShow;
        }
        /// 
        /// 设置窗体大小
        /// 
        /// 
        /// 
        public void SetSize(double videoWidth, double videoHeight)
        {
            this.Width = videoWidth;
            this.Height = videoHeight;
        }
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Stop();
        }
        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            
        }
        public void SetSelected(bool selected)
        {
            throw new NotImplementedException();
        }
        public bool Play(MonitorCamera camera, bool replay = false)
        {
            throw new NotImplementedException();
        }
        public bool SetMaximized(double width, double height)
        {
            throw new NotImplementedException();
        }
        public bool IxMaximized()
        {
            throw new NotImplementedException();
        }
        public void ShowMsg(string message)
        {
            throw new NotImplementedException();
        }
    }
}