using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenNI;
using System.Drawing;

namespace Kinect_viewer.Projection
{
    abstract class AProjection
    {
        #region Fields
        /// <summary>
        /// A context is a workspace where the application builds an OpenNI production graph.
        /// By way of analogy, this is just like a disk partition in which users build their folder trees.
        /// The context object is the entry point to OpenNI and holds the complete state of an application using OpenNI.
        /// This includes the application's entire production graph.
        /// </summary>
        protected Context context;
        /// <summary>
        /// The ScriptNode object loads an XML script from a file or string, and then runs the XML script to build a production graph.
        /// This approach is useful for developers who wish to manipulate the ScriptNode directly.
        /// </summary>
        protected ScriptNode scriptNode;
        /// <summary>
        ///  A DepthGenerator node generates a depth map. Each pixel value of a depth map represents a distance from the sensor.
        /// </summary>
        protected DepthGenerator depth;
        /// <summary>
        /// An Image Generator node is a Map Generator that outputs image maps.
        /// It supports all Map Generator functions, and adds additional functions.
        /// Image maps may arrive in different pixel formats.
        /// </summary>
        protected ImageGenerator image;
        /// <summary>
        /// Bitmap with deph data
        /// </summary>
        protected Bitmap imageBitmap;
        /// <summary>
        /// Array data histogram for drawing depth data of the Kinect.
        /// </summary>
        protected int[] histogram;
        /// <summary>
        /// Path for default configuration of OpenNI. 
        /// If you want to use the library for using OpenNI library you must to have got installed OpenNI library.
        /// </summary>
        protected readonly string SAMPLE_XML_FILE = @"C:\Program Files\OpenNI\Data\SamplesConfig.xml";
        protected ImageMetaData imageMD;
        /// <summary>
        /// If true -> process running
        /// </summary>
        private bool shouldRun;
        /// <summary>
        /// Locking object
        /// </summary>
        public static object lockj = new object();
        #endregion
        #region Properties

        /// <summary>
        /// Gets or Sets state of the cycle in the thread of child classes.
        /// </summary>
        public bool ShouldRun
        {
            get { return shouldRun; }
            set { shouldRun = value; }
        }
        public Bitmap ImageBitmap
        {
            get
            {
                lock (imageBitmap)
                {
                    return imageBitmap; 
                }
            }
            set
            {
                lock (imageBitmap)
                {
                    imageBitmap = value; 
                }
            }
        }
        #endregion

        /// <summary>
        /// Constructor of the class. It bulds all needed datas whitch´s used in child classes
        /// </summary> 
        public AProjection()
        {
           
                this.context = Context.CreateFromXmlFile(SAMPLE_XML_FILE, out scriptNode);
                this.depth = context.FindExistingNode(NodeType.Depth) as DepthGenerator;
                this.image = context.FindExistingNode(NodeType.Image) as ImageGenerator;
                //Here is problem of IR generator class ... new IRgenerator returns allways null ...
                if (this.depth == null || this.image == null)
                {
                    throw new Exception("Viewer must have a depth node!");
                }
                MapOutputMode imageMode = this.image.MapOutputMode;
                MapOutputMode mapMode = this.depth.MapOutputMode;
                imageBitmap = new Bitmap((int)imageMode.XRes, (int)imageMode.YRes, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 
            
        }
    }
}
