using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ImageBlockz.Api;
using ImageBlockz.Core;
using System.Drawing;
using ZedGraph;

namespace ImageBlockz.Operations.InOut
{
    public class Analyse : AbstractOperation
    {
        public Analyse()
            : base(OperationType.Output)
        {

        }

        public string AnalyseType
        {
            get;
            set;
        }

        public override Bitmap Do(Bitmap bitmap)
        {
            // Get newBmp from bitmap
            Bitmap newBmp = new Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
            newBmp = (Bitmap) bitmap.Clone();

            // Get newBmp Intensity Image
            AForge.Imaging.Filters.Grayscale grayScale = new AForge.Imaging.Filters.Grayscale(0.2125, 0.7154, 0.0721);
            newBmp = grayScale.Apply(newBmp);

            // Definition of new chart, pane, list of values and window 
            ZedGraph.ZedGraphControl chart = new ZedGraph.ZedGraphControl();
            GraphPane myPane = chart.GraphPane;
            PointPairList list1 = new PointPairList();
            AnalyseWindow window = new AnalyseWindow();
            

            switch (AnalyseType)
            {
                case "vertical":
                    // Set new intensity statistic
                    AForge.Imaging.HorizontalIntensityStatistics horStat = new AForge.Imaging.HorizontalIntensityStatistics(newBmp);

                    // Set the title and axis labels
                    myPane.Title.Text = "Vertical Intensity Analyse";
                    myPane.XAxis.Title.Text = "Width";
                    myPane.YAxis.Title.Text = "Sum of Intensity";

                    for (int i = 0; i < horStat.Gray.Values.Length; i++)
                    {
                        double x = (double)i;
                        double y1 = horStat.Gray.Values[i];
                        list1.Add(x, y1);
                    }
                    break;

                case "horizontal":
                    // Set new intensity statistic
                    AForge.Imaging.VerticalIntensityStatistics verStat = new AForge.Imaging.VerticalIntensityStatistics(newBmp);

                    // Set the title and axis labels
                    myPane.Title.Text = "Horizontal Intensity Analyse";
                    myPane.XAxis.Title.Text = "Height";
                    myPane.YAxis.Title.Text = "Sum of Intensity";

                    for (int i = 0; i < verStat.Gray.Values.Length; i++)
                    {
                        double x = (double)i;
                        double y1 = verStat.Gray.Values[i];
                        list1.Add(x, y1);
                    }
                    break;

                default:
                    System.Windows.Forms.MessageBox.Show("Analyse type is invalid. [CodeError]");
                    return bitmap;
                    
            }
            
            // Set window title
            window.Text = "Analyse Window [" + AnalyseType + "]";


            // Generate a red curve with diamond
            // symbols, and "Porsche" in the legend
            LineItem myCurve = myPane.AddCurve("Intensity", list1, Color.Red, SymbolType.None);

            // Calculate the Axis Scale Ranges
            chart.AxisChange();

            // Disable FontScaling in graph
            myPane.IsFontsScaled = false;

            // Set DockStyle of control to Fill in Analyse window
            chart.Dock = System.Windows.Forms.DockStyle.Fill;
            
            // Set Maximized Window on Load
            window.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            
            // Set chart to window and show window
            window.Controls.Add(chart);
            window.Show();

            
            return bitmap;
        }

        public override XmlNode ToXml(XmlDocument xmlDoc)
        {
            XmlElement analyse = xmlDoc.CreateElement(typeof(Analyse).FullName);
            XmlElement type = XmlOperation.StringToXml("type", AnalyseType, xmlDoc);
            return analyse;
        }

        public override void FromXml(XmlNode node)
        {
            AnalyseType = XmlOperation.StringFromXml(node["type"]);
        }

        public override string GetFormattedName()
        {
            return "Analyse";
        }


        public override string GetInfo()
        {
            return "Type: " + AnalyseType;
        }
    }
}
