using System;
using System.Data;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace SiaSun.LMS.Common.Report
{
    public class RdlGenerator
    {
        //private int _iTop = 0;

        private Dictionary<string, string> _dtnc;

        public Dictionary<string, string> dtnc
        {
            get { return this._dtnc; }
            set { this._dtnc = value; }
        }

        private string _sTitle;

        public string sTitle
        {
            get { return this._sTitle; }
            set { this._sTitle = value; }
        }

        private SiaSun.LMS.Common.Report.Report CreateReport()
        {
            SiaSun.LMS.Common.Report.Report report = new SiaSun.LMS.Common.Report.Report();
            report.Items = new object[] 
                {
                    CreateDataSources(),
                    CreateBody(),
                    CreateDataSets(),
                    "6.5in",
                    "zh-CN",
                };
            report.ItemsElementName = new SiaSun.LMS.Common.Report.ItemsChoiceType37[]
                { 
                    SiaSun.LMS.Common.Report.ItemsChoiceType37.DataSources, 
                    SiaSun.LMS.Common.Report.ItemsChoiceType37.Body,
                    SiaSun.LMS.Common.Report.ItemsChoiceType37.DataSets,
                    SiaSun.LMS.Common.Report.ItemsChoiceType37.Width,
                    SiaSun.LMS.Common.Report.ItemsChoiceType37.Language,
                    SiaSun.LMS.Common.Report.ItemsChoiceType37.Code,
                };
            return report;
        }

        private SiaSun.LMS.Common.Report.DataSourcesType CreateDataSources()
        {
            SiaSun.LMS.Common.Report.DataSourcesType dataSources = new SiaSun.LMS.Common.Report.DataSourcesType();
            dataSources.DataSource = new SiaSun.LMS.Common.Report.DataSourceType[] { CreateDataSource() };
            return dataSources;
        }

        private SiaSun.LMS.Common.Report.DataSourceType CreateDataSource()
        {
            SiaSun.LMS.Common.Report.DataSourceType dataSource = new SiaSun.LMS.Common.Report.DataSourceType();
            dataSource.Name = "DummyDataSource";
            dataSource.Items = new object[] { CreateConnectionProperties() };
            return dataSource;
        }

        private SiaSun.LMS.Common.Report.ConnectionPropertiesType CreateConnectionProperties()
        {
            SiaSun.LMS.Common.Report.ConnectionPropertiesType connectionProperties = new SiaSun.LMS.Common.Report.ConnectionPropertiesType();
            connectionProperties.Items = new object[]
                {
                    "",
                    "SQL",
                };
            connectionProperties.ItemsElementName = new SiaSun.LMS.Common.Report.ItemsChoiceType[]
                {
                    SiaSun.LMS.Common.Report.ItemsChoiceType.ConnectString,
                    SiaSun.LMS.Common.Report.ItemsChoiceType.DataProvider,
                };
            return connectionProperties;
        }

        //private SiaSun.LMS.Common.Report.TextboxType CreateTitle(string sTitle)
        //{
        //    SiaSun.LMS.Common.Report.TextboxType titleTableCellTextbox = new SiaSun.LMS.Common.Report.TextboxType();

        //    titleTableCellTextbox.Name = "txtTitle";

        //     titleTableCellTextbox.Items = new object[] 
        //        {
        //            sTitle,
        //            CreateTitleTableCellTextboxStyle(),
  

        //        };
        //     titleTableCellTextbox.ItemsElementName = new SiaSun.LMS.Common.Report.ItemsChoiceType14[] 
        //        {
        //            SiaSun.LMS.Common.Report.ItemsChoiceType14.Value,
        //            SiaSun.LMS.Common.Report.ItemsChoiceType14.Style,
        
        
        //        };
        //     return titleTableCellTextbox;
        //}

        private SiaSun.LMS.Common.Report.BodyType CreateBody()
        {
            SiaSun.LMS.Common.Report.BodyType body = new SiaSun.LMS.Common.Report.BodyType();
            body.Items = new object[]
                {
                    CreateReportItems(),
                    "1in",
                };
            body.ItemsElementName = new SiaSun.LMS.Common.Report.ItemsChoiceType30[]
                {
                    SiaSun.LMS.Common.Report.ItemsChoiceType30.ReportItems,
                    SiaSun.LMS.Common.Report.ItemsChoiceType30.Height,
                };
            return body;
        }

        private SiaSun.LMS.Common.Report.ReportItemsType CreateReportItems()
        {
            SiaSun.LMS.Common.Report.ReportItemsType reportItems = new SiaSun.LMS.Common.Report.ReportItemsType();

            TableRdlGenerator tableGen = new TableRdlGenerator();

            tableGen.dtnc = this._dtnc;

            tableGen.iTop = 20;

            reportItems.Items = new object[] { tableGen.CreateTable("Table1") };

            return reportItems;
        }

        private SiaSun.LMS.Common.Report.DataSetsType CreateDataSets()
        {
            SiaSun.LMS.Common.Report.DataSetsType dataSets = new SiaSun.LMS.Common.Report.DataSetsType();
            dataSets.DataSet = new SiaSun.LMS.Common.Report.DataSetType[] { CreateDataSet() };
            return dataSets;
        }

        private SiaSun.LMS.Common.Report.DataSetType CreateDataSet()
        {
            SiaSun.LMS.Common.Report.DataSetType dataSet = new SiaSun.LMS.Common.Report.DataSetType();
            dataSet.Name = "MyData";
            dataSet.Items = new object[] { CreateQuery(), CreateFields() };
            return dataSet;
        }

        private SiaSun.LMS.Common.Report.QueryType CreateQuery()
        {
            SiaSun.LMS.Common.Report.QueryType query = new SiaSun.LMS.Common.Report.QueryType();
            query.Items = new object[] 
                {
                    "DummyDataSource",
                    "",
                };
            query.ItemsElementName = new SiaSun.LMS.Common.Report.ItemsChoiceType2[]
                {
                    SiaSun.LMS.Common.Report.ItemsChoiceType2.DataSourceName,
                    SiaSun.LMS.Common.Report.ItemsChoiceType2.CommandText,
                };
            return query;
        }

        private SiaSun.LMS.Common.Report.FieldsType CreateFields()
        {
            SiaSun.LMS.Common.Report.FieldsType fields = new SiaSun.LMS.Common.Report.FieldsType();

            fields.Field = new SiaSun.LMS.Common.Report.FieldType[this._dtnc.Count];

            int i = 0;

            foreach (KeyValuePair<string, string> kvp in this._dtnc)
            {
                fields.Field[i] = CreateField(kvp.Key);

                i++;
            }

            return fields;
        }

        private SiaSun.LMS.Common.Report.FieldType CreateField(String fieldName)
        {
            SiaSun.LMS.Common.Report.FieldType field = new SiaSun.LMS.Common.Report.FieldType();
            field.Name = fieldName;
            field.Items = new object[] { fieldName };
            field.ItemsElementName = new SiaSun.LMS.Common.Report.ItemsChoiceType1[] { SiaSun.LMS.Common.Report.ItemsChoiceType1.DataField };
            return field;
        }

        public void WriteXml(System.IO.Stream stream)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(SiaSun.LMS.Common.Report.Report));
            serializer.Serialize(stream, CreateReport());
        }

        private SiaSun.LMS.Common.Report.StyleType CreateTitleTableCellTextboxStyle()
        {
            SiaSun.LMS.Common.Report.StyleType titleTableCellTextboxStyle = new SiaSun.LMS.Common.Report.StyleType();
            titleTableCellTextboxStyle.Items = new object[]
                {
                    "700",
                    "12pt",
                    "Left",
                    "zh-CN",
                    "����",
                };
            titleTableCellTextboxStyle.ItemsElementName = new SiaSun.LMS.Common.Report.ItemsChoiceType5[]
                {
                    SiaSun.LMS.Common.Report.ItemsChoiceType5.FontWeight,
                    SiaSun.LMS.Common.Report.ItemsChoiceType5.FontSize,
                    SiaSun.LMS.Common.Report.ItemsChoiceType5.TextAlign,
                    SiaSun.LMS.Common.Report.ItemsChoiceType5.Language,
                    SiaSun.LMS.Common.Report.ItemsChoiceType5.FontFamily,
                };
            return titleTableCellTextboxStyle;
        }
    }
}