using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using SuperWebSocket;
using SuperSocket;
using SuperSocket.SocketBase;
using System.Threading; // for thread sleep
using System.IO; // for memory stream

namespace Beau_sWebsocketServer
{
    public partial class BWS : Form
    {
        // I originally got this idea from: Chris Allick
        // http://badankles.com/?p=209
        // who encoded this with Java

        // makes a new bitmap the size of the screen, this could be global
        //Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        Bitmap bmpScreenCapture = new Bitmap(1024, 768);
        Graphics g;
        WebSocketSession wss;
        private bool capturing;

        delegate void startCaptureDelegate();
        private void startCapture()
        {
            //int loopCount = 0;
            capturing = true;
            wss = ws.GetAppSessionByID(this.lastConnectedSessionID);
            while (capturing)
            {
                captureScreen();
                //loopCount++;
                //cout(loopCount+"\n");
                Thread.Sleep(50); // fps
            }
        }
        // http://stackoverflow.com/questions/5049122/capture-the-screen-shot-using-net
        private void captureScreen()
        {
            // http://stackoverflow.com/questions/2891346/convert-graphics-to-image-in-c-sharp
            // sets a graphics object (which is used for modifying an image) to that bitmap, why cant it be global?
            g = Graphics.FromImage(bmpScreenCapture);
            g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                    Screen.PrimaryScreen.Bounds.Y,
                                    0, 0,
                                    bmpScreenCapture.Size,
                                    CopyPixelOperation.SourceCopy);

            //Image i = bmpScreenCapture;
            streamImage(bmpScreenCapture);
        }
        private void streamImage(Image i)
        {
            string s = ImageToBase64(i);
            //videoBox.Image = i; // writing to this object is usually slow and errors
            //cout(s+"\n");
            try
            {
                wss.Send(s);
            }
            catch (Exception e) {};
        }
        // http://www.dailycoding.com/Posts/convert_image_to_base64_string_and_base64_string_to_image.aspx
        private string ImageToBase64(Image image)
        {
            // http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx
            MemoryStream ms = new MemoryStream();
            // Convert Image to byte[]
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] imageBytes = ms.ToArray();

            // note how much larger this becomes as a bitmap
            // take the number of bytes multiplied by the fps for bytesPerSecond
            cout(imageBytes.Length + "\n"); 

            // Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes);
            // ms.Dispose(); // use this if ms is global, would that be better?
            return base64String;
        }
        private void stopCapture()
        {
            capturing = false;
        }
    }
}