using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using System.Runtime.Serialization;

using DataPerformer;
using DiagramUI;
using CategoryTheory;
using DataSetService;

namespace DataTableSelection
{
    [Serializable()]
    public class DataTableSeries : Series, IPostSetArrow, IDataSetConsumer
    {

        #region Fields
        protected string x = "";
        protected string y = "";

        protected DataSet dataSet;
        protected IDataSetFactory factory;

        #endregion

        #region Constructors

        public DataTableSeries()
        {
        }


        protected DataTableSeries(SerializationInfo info, StreamingContext context)
        {
            x = info.GetValue("X", typeof(string)) as string;
            y = info.GetValue("Y", typeof(string)) as string;
        }


        #endregion

        #region Overriden Members

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("X", x, typeof(string));
            info.AddValue("Y", y, typeof(string));
        }

        #endregion

        #region IPostSetArrow Members

        void IPostSetArrow.PostSetArrow()
        {
            Set();
            Post();
        }

        #endregion

        #region IDataSetConsumer Members

        void IDataSetConsumer.Add(DataSet dataSet)
        {
            if (this.dataSet != null & dataSet != null)
            {
                throw new Exception("Data set already exists");
            }
            this.dataSet = dataSet;
        }

        void IDataSetConsumer.Remove(DataSet dataSet)
        {
            this.dataSet = null;
        }

        IDataSetFactory IDataSetConsumer.Factory
        {
            get
            {
                return factory;
            }
            set
            {
                factory = value;
            }
        }

        #endregion

        #region Specific Members


        new public string X
        {
            get { return x; }
        }

        new public string Y
        {
            get { return y; }
        }


        public List<string> Names
        {
            get
            {
                return DataSetSelection.GetDoubleNames(dataSet);
            }
        }

        public void Set(string x, string y)
        {
            this.x = x;
            this.y = y;
            Set();
        }

        protected void Set()
        {
            points = new List<double[]>();
            if (dataSet != null)
            {
                DataTable table = dataSet.Tables[0];
                DataColumn cx = DataSetSelection.SelectColumn(x, dataSet);
                DataColumn cy = DataSetSelection.SelectColumn(y, dataSet);
                foreach (DataRow row in table.Rows)
                {
                    object ox = row[cx];
                    if (ox is DBNull)
                    {
                        continue;
                    }
                    object oy = row[cy];
                    if (oy is DBNull)
                    {
                        continue;
                    }
                    points.Add(new double[] { (double)ox, (double)oy });
                }
            }
        }

        #endregion
    }
}
