using ESBasic;
using OMCS.Passive.Audio;
using Oraycn.MCapture;
using Oraycn.MFile;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Windows;
using OMCS.Passive;

namespace OVCS.ClientWpf
{
    /// <summary>
    /// 录制功能 2022_12_13
    /// </summary>
    internal  class VideoRecorder
    {
        private System.Threading.Timer timer;//录制定时器
        private IDesktopCapturer desktopCapturer;
        private VideoFileMaker videoFileMaker;
        private AudioInOutMixer audioInOutMixer;
        private IMultimediaManager multimediaManager;
        private int recordSeconds = 0;
        private bool isRecording = false; //是否启动录制

        /// <summary>
        /// 当录制时间增加时触发该事件（一秒触发一次）。参数：录制时间
        /// </summary>
        public event CbGeneric<TimeSpan> RecordTimeChanged;
        public bool IsRecording
        {
            get
            {
                return this.isRecording;
            }
        }
        /// <summary>
        /// 录制初始化
        /// </summary>
        public void Initialize(IMultimediaManager _multimediaManager)
        {
            this.multimediaManager = _multimediaManager;
            
        }
        /// <summary>
        /// 结束录制
        /// </summary>
        public void StopRecord()
        {
            this.desktopCapturer.Stop();
            this.audioInOutMixer.Dispose();
            this.videoFileMaker.Close(true);
        }

        /// <summary>
        /// 开始录制
        /// </summary>
        public void StartRecord()
        {
            this.isRecording = true;
            Rectangle region = new Rectangle((int)App.MainWindow.Left, (int)App.MainWindow.Top, (int)App.MainWindow.Width, (int)App.MainWindow.Height);
            region = new Rectangle(region.Location, new System.Drawing.Size(region.Width / 8 * 8, region.Height / 8 * 8)); //宽和高规整为8的倍数
            int fps = 10;
            this.desktopCapturer = CapturerFactory.CreateDesktopCapturer(fps, false, region);
            this.desktopCapturer.ImageCaptured += DesktopCapturer_ImageCaptured;
            this.audioInOutMixer = new AudioInOutMixer();
            this.audioInOutMixer.Initialize(this.multimediaManager);
            this.audioInOutMixer.AudioMixed += AudioInOutMixer_AudioMixed;
            this.videoFileMaker = new VideoFileMaker();
            this.videoFileMaker.Initialize("test.mp4", region.Width, region.Height, fps, VideoQuality.High, 16000, 1, true);
            this.desktopCapturer.Start();
            this.recordSeconds = 0;
        }
        /// <summary>
        /// 采集到的桌面图像
        /// </summary>
        private void DesktopCapturer_ImageCaptured(Bitmap img)
        {
            if (this.isRecording)
            {
                this.videoFileMaker.AddVideoFrame(img);
            }
        }

        /// <summary>
        /// 采集混音数据
        /// </summary>
        private void AudioInOutMixer_AudioMixed(byte[] audioData)
        {
            if (this.isRecording)
            {
                this.videoFileMaker.AddAudioFrame(audioData);
            }
        }
        /// <summary>
        /// 重置录制时间或开始录制时间
        /// </summary>
        public string RecordTime(TimeSpan ts)
        {
            if(this.isRecording)
            {
                return ts.Hours.ToString("00") + ":" + ts.Minutes.ToString("00") + ":" + ts.Seconds.ToString("00");
            }
            return "00:00:00";
        }

        /// <summary>
        /// 录制时间定时器
        /// </summary>
        /// <param name="state">状态</param>
        private void TimerAction(object state)
        {
           if (this.isRecording)
           {
                 TimeSpan ts = new TimeSpan(0, 0, ++this.recordSeconds);
                //this.record_time.Content = ts.Hours.ToString("00") + ":" + ts.Minutes.ToString("00") + ":" + ts.Seconds.ToString("00");
                this.RecordTimeChanged?.Invoke(ts);
            }
        }

        /// <summary>
        /// 点击录制
        /// </summary>
        public void RecordInit()
        {
            if (!this.isRecording)
            {
                this.timer = new System.Threading.Timer(new System.Threading.TimerCallback(this.TimerAction), null, 1000, 1000);
                App.MainWindow.SetRecordingState(!this.isRecording);
                this.isRecording = true;
                this.StartRecord();
                this.recordSeconds = 0;
            }
            else
            {
                this.isRecording = false;
                this.recordSeconds = 0;
                this.StopRecord();
                App.MainWindow.SetRecordingState(this.isRecording);
                this.timer.Dispose();
            }
        }
    }
}
