using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lumenera.USB;
using LumeneraCamera;

namespace LumeneraCamera
{
    public partial class VideoPreview : UserControl
    {
        private LucamClass cam;
        private int counterFrame;
        private bool statsEnable = true;
      //  private float avgAll = 0, avgRed, avgGreen, avgBlue;
        private int callbackId;
        private delegate void RefreshDisplayDelegate();
        private RefreshDisplayDelegate screenHandler;
        private dll.LucamRgbPreviewCallback callbackHandler;
        private bool isVideoPause = false;
        private dll.LucamPixelFormat currentPF= dll.LucamPixelFormat.PF_24 ;
        private bool isUsingVideoWindow = true;
    //    private bool mono=false;

        public VideoPreview()
        {
            InitializeComponent();
            cam = new LucamClass();
            counterFrame = 0;
            screenHandler = new RefreshDisplayDelegate(RefreshDisplay);

        }
       

        public Object camera
        {
            get
            {
                return ((Object)cam);
            }
            set
            {
                cam = (LucamClass)value;
                cam.CameraVideoPreviewWindows = this.VideoPanel.Handle.ToInt32();
                cam.VideoObject = this;
                try
                {
                    dll.LucamFrameFormat ff = api.GetFormat(cam.CHandle);
                    currentPF = dll.LucamPixelFormat.PF_24;
                    if (ff.PixelFormat == dll.LucamPixelFormat.PF_16)
                        currentPF = dll.LucamPixelFormat.PF_32;
                }
                catch
                {
                    MessageBox.Show("Cannot read frame format", "Set Callback");
                    return;
                }
 

            }
        }
        public void ResetWindowHandle()
        {
            cam.CameraVideoPreviewWindows = 0;
        }
        private void RefreshDisplay()
        {
            if (statsEnable)
            {
                FrameCounterLabel.Text = counterFrame.ToString();
            }
        }
      
        private void PreviewCallback(IntPtr pContext, IntPtr pData, int n, uint unused)
        {
            ++counterFrame;
            //Calc Int
            this.BeginInvoke (screenHandler);

        }
        public bool VideoStreamStart(bool showVideo)
        {
               
            if (!isVideoPause)
            {
                if (cam.IsStreamingVideo) return (true);
                if (!api.IsCameraValid(cam.CHandle)) return (false);
               
                callbackHandler = new dll.LucamRgbPreviewCallback(PreviewCallback);
                callbackId = dll.LucamAddRgbPreviewCallback(cam.CHandle, callbackHandler, (IntPtr)0, currentPF);
                if (callbackId == -1)
                {
                    MessageBox.Show("Cannot add RGB callback", "Set Callback");
                    return (false);
                }
            }
            if (showVideo)
            {
                if (!dll.LucamStreamVideoControl(cam.CHandle, dll.LucamStreamMode.START_DISPLAY, VideoPanel.Handle.ToInt32()))
                    return (false);
            }
            else
            {
                if (!dll.LucamStreamVideoControl(cam.CHandle, dll.LucamStreamMode.START_STREAMING, VideoPanel.Handle.ToInt32()))
                    return (false);
            }
            isUsingVideoWindow = showVideo;
            cam.IsStreamingVideo  = true;
            return (true);
        }
        public bool VideoStreamStop()
        {
            if (!cam.IsStreamingVideo) return (true);
            if (!dll.LucamStreamVideoControl(cam.CHandle, dll.LucamStreamMode.STOP_STREAMING, VideoPanel.Handle.ToInt32()))
                return (false);
            cam.IsStreamingVideo = false;
            dll.LucamRemoveRgbPreviewCallback(cam.CHandle, callbackId);
            VideoPanel.Image = null;
            VideoPanel.BackColor = Color.Aquamarine;
            return (true);
        }
        public bool VideoStreamPause()
        {
            if (!cam.IsStreamingVideo) return (false);
            if (!dll.LucamStreamVideoControl(cam.CHandle, dll.LucamStreamMode.PAUSE_STREAM, VideoPanel.Handle.ToInt32()))
                return (false);
            
            return (true);
        }

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

        private void toolStripContainer1_TopToolStripPanel_Click(object sender, EventArgs e)
        {

        }
    }
}
