using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

using DataSetService;

namespace DataSetSerializable
{
    [SerializableAttribute()]
    public class TableSerializable : AbstractTable, ISerializable
    {
        #region Constructors

        public TableSerializable()
        {
        }

        public TableSerializable(SerializationInfo info, StreamingContext context)
        {
            x = (int) info.GetValue("X", typeof(int));
            y = (int) info.GetValue("Y", typeof(int));
            name = info.GetValue("Name", typeof(string)) + "";
            try
            {
                columns = info.GetValue("Columns", typeof(Dictionary<string, IColumn>)) as Dictionary<string, IColumn>;
            }
            catch (Exception)
            {
                int i = 0;
            }
        }
        #endregion

        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("X", x);
            info.AddValue("Y", y);
            info.AddValue("Name", name);
            info.AddValue("Columns", columns);
        }

        #endregion

    }

    [SerializableAttribute()]
    public class LinkSerializable : AbstractLink, ISerializable
    {
        #region Constructors

        internal LinkSerializable()
        {
        }

        public LinkSerializable(SerializationInfo info, StreamingContext context)
        {
            sourceTable = info.GetValue("SourceTable", typeof(string)) + "";
            targetTable = info.GetValue("TargetTable", typeof(string)) + "";
            sourceColumn = info.GetValue("SourceColumn", typeof(string)) + "";
            targetColumn = info.GetValue("TargetColumn", typeof(string)) + "";
            isMarked = (bool)info.GetValue("IsMarked", typeof(bool));

        }

        #endregion

        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("SourceTable", sourceTable);
            info.AddValue("TargetTable", targetTable);
            info.AddValue("SourceColumn", sourceColumn);
            info.AddValue("TargetColumn", targetColumn);
            info.AddValue("IsMarked", isMarked);
        }

        #endregion
    }

    [SerializableAttribute()]
    public class ColumnSerializable : AbstractColumn, ISerializable
    {

        #region Constructors

        public ColumnSerializable()
        {
        }

        public ColumnSerializable(SerializationInfo info, StreamingContext context)
        {
            name = info.GetValue("Name", typeof(string)) + "";
            isMarked = (bool)info.GetValue("IsMarked", typeof(bool));
            try
            {
                type = info.GetValue("Type", typeof(string)) + "";
                isNullable = (bool)info.GetValue("IsNullable", typeof(bool));
                isNull = (bool)info.GetValue("IsNull", typeof(bool));
                modifier = info.GetValue("Modifier", typeof(string)) + "";
                val = info.GetValue("Value", typeof(string)) + "";
                length = (int)info.GetValue("Length", typeof(int));
            }
            catch (Exception)
            {
            }
        }
        #endregion

        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Name", name);
            info.AddValue("IsMarked", isMarked);
            if (type != null)
            {
                info.AddValue("Type", type);
            }
            info.AddValue("IsNullable", isNullable, typeof(bool));
            info.AddValue("IsNull", isNull, typeof(bool));
            info.AddValue("Modifier", modifier, typeof(string));
            info.AddValue("Value", val, typeof(string));
            info.AddValue("Length", length, typeof(int));
        }

        #endregion

    }

    [SerializableAttribute()]
    public class DataDesktopSerializable : AbstractDataSetDesktop, ISerializable
    {

        #region Constructors

        public DataDesktopSerializable()
        {
        }

        public DataDesktopSerializable(SerializationInfo info, StreamingContext context)
        {
            try
            {
                tables = info.GetValue("Tables", typeof(Dictionary<string, ITable>)) as Dictionary<string, ITable>;
                links = info.GetValue("Links", typeof(List<ILink>)) as List<ILink>;
                foreach (ITable table in tables.Values)
                {
                    table.Desktop = this;
                }
                foreach (ILink link in links)
                {
                    link.Desktop = this;
                }
            }
            catch (Exception)
            {
                int i = 0;
            }
        }
        #endregion

        #region Overriden
        public override IColumn Column
        {
            get
            {
                return new ColumnSerializable();
            }
        }

        public override ITable Table
        {
            get
            {
                return new TableSerializable();
            }
        }

        public override ILink Link
        {
            get
            {
                return new LinkSerializable();
            }
        }


        public override IDataSetDesktop Desktop
        {
            get
            {
                return new DataDesktopSerializable();
            }
        }

        #endregion

        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Tables", tables);
            info.AddValue("Links", links);
        }

        #endregion
    }
}
