using Common;
using MeterPlugInLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CarMeterSystem
{
    public partial class frmVoice : Form
    {
        public frmVoice()
        {
            InitializeComponent();
        }

        List<CamerEs> lc = new List<CamerEs>();
        private void LoadVoice(List<loginParam> lt) 
        {
            int i = 1;
            foreach (loginParam lp in lt) 
            {
                CamerEs c = new CamerEs(lp);
                PictureBox pb = new PictureBox();
                pb.Name = $"pb{i++}";
                pb.Height = 100;
                pb.BackColor = Color.Black;
                pb.Dock = DockStyle.Top;
                flowLayoutPanel1.Controls.Add(pb);
                c.Login(pb);
                lc.Add(c);
            }
        }

        public void LogOut() 
        {
            foreach (CamerEs lp in lc) 
            {
                lp.LoginOut();
            }
        }

        /// <summary>
        /// 传入了值，则按传入的路径及Ip进行截图，否则则全部截图
        /// </summary>
        /// <param name="imgUrl"></param>
        /// <param name="ip"></param>
        public void CapturePictrue(string actualFirstNo, string imgUrl = default, string ip = default)
        {
            string strUrl = imgUrl ?? PbCache.path + string.Format("\\imgShort\\formalImg\\{0}_{1}_{2}.jpg", PbCache.sportInfo.baseSpotNo, "C" + actualFirstNo, 1);
            lc[0]?.CapturePictrue(strUrl);
            /*
             * 只截取第一张图片
            if (!string.IsNullOrEmpty(ip))
            {
                string strUrl = imgUrl ?? PbCache.path + string.Format("\\imgShort\\formalImg\\{0}_{1}_{2}.jpg", PbCache.sportInfo.baseSpotNo, "C" + actualFirstNo, 1);
                lc.Where(s => s._lg?.ip == ip).FirstOrDefault()?.CapturePictrue(strUrl);
            }
            else
            {
                string strUrl = imgUrl ?? $"{Environment.CurrentDirectory}/imgShort/formalImg";
                int i = 1;
                foreach (CamerEs lp in lc)
                {

                    //lp.CapturePictrue($"{strUrl}/{DateTime.Now.ToString("yyyyMMddHHmmss")}_{i++}.jpg");
                }
            }
            */
        }

        internal void LoadVoiceInfo() 
        {
            List<MeterBaseVideoChild> lt = PbCache.videoChild;
            List<loginParam> lp = new List<loginParam>();
            
            lp.Add(new loginParam { ip = lt[0].p_ip, port = int.Parse(lt[0].vdo_port), userId = lt[0].vdo_user, pwd = lt[0].vdo_pwd });
           
            LoadVoice(lp);
            this.Hide();
        }

        private void frmVoice_Load(object sender, EventArgs e)
        {
            
        }

        private void frmVoice_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                LogOut();
            }
            catch { }
        }
    }
}
