using Common;
using MeterPlugInLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StorageMeterSystem
{
    /// <summary>
    /// 截图调用类
    /// </summary>
    public class CameraShotCls
    {
        public CameraShotCls()
        {
            _dvr = new HkDvr();
        }

        private HkDvr _dvr;
        bool ret = false;

        public string ip { get; set; }
        public string port { get; set; }
        public string uid { get; set; }
        public string pwd { get; set; }

        /// <summary>
        /// 连接设备
        /// </summary>
        /// <param name="Ip">IP地址</param>
        /// <param name="Port">端口</param>
        /// <param name="Uid">用户名</param>
        /// <param name="Pwd">密码</param>
        public void Connection()
        {
            if (_dvr.UserId < 0)
            {
                //IP,PORT,UID,PWD
                ret = _dvr.Init(ip + "," + port + "," + uid + "," + pwd);
                ret = _dvr.Open();
            }
        }


        private bool startTalk = false;
        /// <summary>
        /// 打开对讲
        /// </summary>
        /// <returns></returns>
        public bool StartTalk()
        {
            if (_dvr.UserId > -1 && !startTalk)
            {
                _dvr.StartTalk();
                startTalk = true;
            }
            return true;
        }

        /// <summary>
        /// 关闭对讲
        /// </summary>
        /// <returns></returns>
        public bool StopTalk()
        {
            if (_dvr.UserId > -1 && startTalk)
            {
                _dvr.StopTalk();
                startTalk = false;
                return true;
            }
            return true;
        }


        /// <summary>
        /// 关闭视频
        /// </summary>
        public void Close()
        {
            if (_dvr.UserId > -1)
            {
                _dvr.Close();
            }
        }

        public void RealPlay(PictureBox picture, int i)
        {
            if (_dvr.UserId > -1)
            {
                _dvr.RealPlay(i, picture.Handle);
            }
        }

        public void StopRealPlay(int i)
        {
            if (_dvr.UserId > -1)
            {
                _dvr.StopRealPlay(i);
            }
        }

        public bool SoundVoice(string fileName) 
        {
            _dvr.SetVolume(0xffff);
            if (_dvr.VoiceHandle > -1) //播放语音前，需要关闭对讲
            {
                _dvr.StopTalk();
            }
            return _dvr.SendVoiceData(fileName);
        }

        /// <summary>
        /// 从硬盘录像机 进行截图
        /// </summary>
        /// <param name="fileUrl"></param>
        public void CapPicFromVideo(uint chanel,string fileUrl)
        {
            _dvr.CapturePicture(chanel, fileUrl + ".jpg");
        }


        #region 直接通过摄像头进行截图的方式
        /// <summary>
        /// 连接视频截图并关闭
        /// </summary>
        /// <param name="actualFirstNo">作业编号</param>
        public void CapMethod(string actualFirstNo)
        {
            Connection();
            CapPic(actualFirstNo);
            Close();
        }

        /// <summary>
        /// 截图
        /// </summary>
        /// <param name="actualFirstNo">作业编号</param>
        public void CapPic(string actualFirstNo)
        {
            int iCnt = 1; //抓拍摄像头配置顺序必须是前面2个
            foreach (CameraShotCls shot in CarCache.cameraShots)
            {
                //放在tempImg文件夹下面就进行图片的压缩操作，否则不进行图片压缩
                shot.CapPic(iCnt < 3 ? 0 : 1, PbCache.path + string.Format("\\imgShort\\formalImg\\{0}_{1}_{2}.jpg", PbCache.sportInfo.baseSpotNo, "C" + actualFirstNo, iCnt));

                iCnt++;
            }
        }

        /// <summary>
        /// 截图
        /// </summary>
        /// <param name="voiType">0抓拍机  1一般的摄像头</param>
        /// <param name="filePath">文件存储位置及文件名称，文件名称应该是 计量作业编号_计量点_第几张 or 计量作业编号_第几张</param>
        /// <returns>是否截图成功,true成功，false失败，异常的话会在logs中写入信息</returns>
        public bool CapPic(int voiType, string filePath)
        {
            return _dvr.CapturePicture(voiType, 1, filePath);
        }
        #endregion

    }

    public class CarCache
    {
        /// <summary>
        /// 摄像头的开关类集合
        /// </summary>
        public static List<CameraShotCls> cameraShots { get; set; }
    }
}
