using JianGongYun.TRTC.Models;
using JianGongYun.TRTC.Utils;
using ManageLiteAV;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;
using System.Linq;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace JianGongYun.TRTC.ViewModels
{
    public class LiveWindowViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 教室信息
        /// </summary>
        public Models.ClassroomEntity ClassroomEntity { get { return LiveClassroom.CurrentClassroomEntity; } }

        /// <summary>
        /// 学生总数
        /// </summary>
        private int _StudentCount = 0;
        public int StudentCount
        {
            set
            {
                _StudentCount = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("StudentCount"));//对StudentCount进行监听
                }
            }
            get
            {
                return _StudentCount;
            }
        }

        /// <summary>
        /// 是否在直播中
        /// </summary>
        private bool _IsLive = false;
        public bool IsLive
        {
            set
            {
                _IsLive = value;
                if (value)//开始直播，清空时间并开始计时
                {
                    _LiveTimeCount = new DateTime(1970, 1, 1, 0, 0, 0);
                    timer.Start();
                }
                else
                {
                    timer.Stop();
                }
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("IsLive"));
                }
            }
            get
            {
                return _IsLive;
            }
        }

        public LiveWindowViewModel()
        {
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler((a, b) => LiveTimeCount = "");
        }
        /// <summary>
        /// 直播计时器
        /// </summary>
        private DispatcherTimer timer = null;
        private DateTime _LiveTimeCount;
        public string LiveTimeCount
        {
            get
            {
                return _LiveTimeCount.ToString("HH:mm:ss");
            }
            set
            {
                _LiveTimeCount = _LiveTimeCount.AddSeconds(1);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("LiveTimeCount"));
                }
            }
        }

        /// <summary>
        /// 直播类型
        /// </summary>
        private LiveTypeEnum _LiveType = LiveTypeEnum.OnlyAudio;
        public LiveTypeEnum LiveType
        {
            set
            {
                _LiveType = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("LiveType"));
                }
            }
            get
            {
                return _LiveType;
            }
        }

        /// <summary>
        /// 摄像头采集中
        /// </summary>
        private bool _CameraRunning = false;
        public bool CameraRunning
        {
            set
            {
                _CameraRunning = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("CameraRunning"));
                }
            }
            get
            {
                return _CameraRunning;
            }
        }

        /// <summary>
        /// 屏幕分享中
        /// </summary>
        private bool _ScreenRunning = false;
        public bool ScreenRunning
        {
            set
            {
                _ScreenRunning = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("ScreenRunning"));
                }
            }
            get
            {
                return _ScreenRunning;
            }
        }

        /// <summary>
        /// 麦克风使用中
        /// </summary>
        private bool _MicRunning = false;
        public bool MicRunning
        {
            set
            {
                _MicRunning = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("MicRunning"));
                }
            }
            get
            {
                return _MicRunning;
            }
        }
        /// <summary>
        /// 麦克风静音
        /// </summary>
        private bool _MicMute = false;
        public bool MicMute
        {
            set
            {
                _MicMute = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("MicMute"));
                }
            }
            get
            {
                return _MicMute;
            }
        }


        private ObservableCollection<TRTCScreenEntity> _LiveScreens = new ObservableCollection<TRTCScreenEntity>();
        /// <summary>
        /// 可分享桌面
        /// </summary>
        public ObservableCollection<TRTCScreenEntity> LiveScreens
        {
            get
            {
                return _LiveScreens;
            }
        }

        static object ScreenListLock = new object();
        private SIZE thumbSize = new SIZE { cx = 300, cy = 200 };
        private SIZE iconSize = new SIZE { cx = 50, cy = 50 };
        /// <summary>
        /// 获取所有桌面列表
        /// </summary>
        public void LoadAllScreen()
        {
            lock (ScreenListLock)
            {
                Console.WriteLine("get LiveScreens");
                _LiveScreens.Clear();
                Console.WriteLine("get LiveScreens Clear");
                var temp = LiveClassroom.lTRTCCloud.getScreenCaptureSources(ref thumbSize, ref iconSize);
                var count = temp.getCount();
                Console.WriteLine($"get LiveScreens {count}");
                for (uint i = 0; i < count; i++)
                {
                    var info = temp.getSourceInfo(i);
                    //var icon = Util.ToWriteableBitmap(ref info.iconBGRA);
                    Console.WriteLine($"get LiveScreens {i} {info.sourceId}");
                    var thumb = info.thumbBGRA.ToWriteableBitmap();
                    _LiveScreens.Add(new TRTCScreenEntity { SourceId = info.sourceId, SourceName = info.sourceName, Type = info.type, Thumb = thumb, Info = info });
                }
                temp.release();
            }
        }


        /// <summary>
        /// 当前分享的桌面列表
        /// </summary>
        private bool _ShowShareScreenList = false;
        public bool ShowShareScreenList
        {
            get
            {
                return _ShowShareScreenList;
            }
            set
            {
                _ShowShareScreenList = value;
                if (_ShowShareScreenList)
                {
                    var temp = LiveScreens;//获取一次刷新列表
                }
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("ShowShareScreenList"));
                }
            }
        }

        /// <summary>
        /// 当前分享的桌面
        /// </summary>
        private TRTCScreenCaptureSourceInfo _CurrentShareScreen;
        public TRTCScreenCaptureSourceInfo CurrentShareScreen
        {
            get
            {
                if (_CurrentShareScreen == null && LiveScreens.Count > 0)
                {
                    _CurrentShareScreen = LiveScreens[0].Info;
                }
                return _CurrentShareScreen;
            }
            set
            {
                _CurrentShareScreen = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("CurrentShareScreen"));
                }
            }
        }

    }
}
