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

namespace SiaSun.LMS.Common.Report
{
    public class TableRdlGenerator
    {
        private Dictionary<string, string> _dtnc;

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

        private int _iTop;

        public int iTop
        {
            get { return this._iTop; }
            set { this._iTop = value; }
        }

        private string _sTableName;

        public SiaSun.LMS.Common.Report.TableType CreateTable(string sName)
        {
            this._sTableName = sName;

            SiaSun.LMS.Common.Report.TableType table = new SiaSun.LMS.Common.Report.TableType();

            table.Name = sName;

            table.Items = new object[]
                {
                    CreateTableColumns(),
                    CreateHeader(),
                    CreateDetails(),
                    string.Format("{0}pt",this._iTop.ToString()),
                };
            table.ItemsElementName = new SiaSun.LMS.Common.Report.ItemsChoiceType21[]
                {
                    SiaSun.LMS.Common.Report.ItemsChoiceType21.TableColumns,
                    SiaSun.LMS.Common.Report.ItemsChoiceType21.Header,
                    SiaSun.LMS.Common.Report.ItemsChoiceType21.Details,
                    SiaSun.LMS.Common.Report.ItemsChoiceType21.Top,
                };
            return table;
        }

        private SiaSun.LMS.Common.Report.HeaderType CreateHeader()
        {
            SiaSun.LMS.Common.Report.HeaderType header = new SiaSun.LMS.Common.Report.HeaderType();
            header.Items = new object[]
                {
                    CreateHeaderTableRows(),
                };
            header.ItemsElementName = new SiaSun.LMS.Common.Report.ItemsChoiceType20[]
                {
                    SiaSun.LMS.Common.Report.ItemsChoiceType20.TableRows,
                };
            return header;
        }

        private SiaSun.LMS.Common.Report.TableRowsType CreateHeaderTableRows()
        {
            SiaSun.LMS.Common.Report.TableRowsType headerTableRows = new SiaSun.LMS.Common.Report.TableRowsType();
            headerTableRows.TableRow = new SiaSun.LMS.Common.Report.TableRowType[] { CreateHeaderTableRow() };
            return headerTableRows;
        }

        private SiaSun.LMS.Common.Report.TableRowType CreateHeaderTableRow()
        {
            SiaSun.LMS.Common.Report.TableRowType headerTableRow = new SiaSun.LMS.Common.Report.TableRowType();
            headerTableRow.Items = new object[] { CreateHeaderTableCells(), "0.25in" };
            return headerTableRow;
        }

        private SiaSun.LMS.Common.Report.TableCellsType CreateHeaderTableCells()
        {
            SiaSun.LMS.Common.Report.TableCellsType headerTableCells = new SiaSun.LMS.Common.Report.TableCellsType();

            headerTableCells.TableCell = new SiaSun.LMS.Common.Report.TableCellType[this._dtnc.Count];

            int i = 0;

            foreach (KeyValuePair<string, string> kvp in this._dtnc)
            {
                headerTableCells.TableCell[i] = CreateHeaderTableCell(kvp.Key);

                i++;
            }

            return headerTableCells;
        }

        private SiaSun.LMS.Common.Report.TableCellType CreateHeaderTableCell(string fieldName)
        {
            SiaSun.LMS.Common.Report.TableCellType headerTableCell = new SiaSun.LMS.Common.Report.TableCellType();
            headerTableCell.Items = new object[] { CreateHeaderTableCellReportItems(fieldName) };
            return headerTableCell;
        }

        private SiaSun.LMS.Common.Report.ReportItemsType CreateHeaderTableCellReportItems(string fieldName)
        {
            SiaSun.LMS.Common.Report.ReportItemsType headerTableCellReportItems = new SiaSun.LMS.Common.Report.ReportItemsType();
            headerTableCellReportItems.Items = new object[] { CreateHeaderTableCellTextbox(fieldName) };
            return headerTableCellReportItems;
        }

        private SiaSun.LMS.Common.Report.TextboxType CreateHeaderTableCellTextbox(string fieldName)
        {
            SiaSun.LMS.Common.Report.TextboxType headerTableCellTextbox = new SiaSun.LMS.Common.Report.TextboxType();
            headerTableCellTextbox.Name = string.Format("{0}_{1}_Header", this._sTableName, fieldName);

            string sValue = string.Empty;

            this.dtnc.TryGetValue(fieldName, out sValue);

            headerTableCellTextbox.Items = new object[] 
                {
                    sValue.Trim(),
                    CreateHeaderTableCellTextboxStyle(),
                    true,
                    "100pt",
                  //  this.CreateUserSort(fieldName),

                };
            headerTableCellTextbox.ItemsElementName = new SiaSun.LMS.Common.Report.ItemsChoiceType14[] 
                {
                    SiaSun.LMS.Common.Report.ItemsChoiceType14.Value,
                    SiaSun.LMS.Common.Report.ItemsChoiceType14.Style,
                    SiaSun.LMS.Common.Report.ItemsChoiceType14.CanGrow,
                    SiaSun.LMS.Common.Report.ItemsChoiceType14.Width,
                   // SiaSun.LMS.Common.Report.ItemsChoiceType14.UserSort,
                };
            return headerTableCellTextbox;
        }

        private SiaSun.LMS.Common.Report.StyleType CreateHeaderTableCellTextboxStyle()
        {
            SiaSun.LMS.Common.Report.StyleType headerTableCellTextboxStyle = new SiaSun.LMS.Common.Report.StyleType();
            headerTableCellTextboxStyle.Items = new object[]
                {
                    "700",
                    //"14pt",
                    "Center",
                    "AliceBlue",
                    "zh-CN",
                    "����",
                    CreateBorderColorStyleWidth("Solid"),
                     CreateBorderColorStyleWidth("0.5pt"),
                };
            headerTableCellTextboxStyle.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.BackgroundColor,
                    SiaSun.LMS.Common.Report.ItemsChoiceType5.Language,
                     SiaSun.LMS.Common.Report.ItemsChoiceType5.FontFamily,
                     SiaSun.LMS.Common.Report.ItemsChoiceType5.BorderStyle,
                    SiaSun.LMS.Common.Report.ItemsChoiceType5.BorderWidth,
                };
            return headerTableCellTextboxStyle;
        }

        private SiaSun.LMS.Common.Report.DetailsType CreateDetails()
        {
            SiaSun.LMS.Common.Report.DetailsType details = new SiaSun.LMS.Common.Report.DetailsType();
            details.Items = new object[] { CreateTableRows() };
            return details;
        }

        private SiaSun.LMS.Common.Report.TableRowsType CreateTableRows()
        {
            SiaSun.LMS.Common.Report.TableRowsType tableRows = new SiaSun.LMS.Common.Report.TableRowsType();
            tableRows.TableRow = new SiaSun.LMS.Common.Report.TableRowType[] { CreateTableRow() };
            return tableRows;
        }

        private SiaSun.LMS.Common.Report.TableRowType CreateTableRow()
        {
            SiaSun.LMS.Common.Report.TableRowType tableRow = new SiaSun.LMS.Common.Report.TableRowType();
            tableRow.Items = new object[] { CreateTableCells(), "0.25in" };
            return tableRow;
        }

        private SiaSun.LMS.Common.Report.TableCellsType CreateTableCells()
        {
            SiaSun.LMS.Common.Report.TableCellsType tableCells = new SiaSun.LMS.Common.Report.TableCellsType();

            tableCells.TableCell = new SiaSun.LMS.Common.Report.TableCellType[this._dtnc.Count];

            int i = 0;

            foreach (KeyValuePair<string, string> kvp in this._dtnc)
            {
                tableCells.TableCell[i] = CreateTableCell(kvp.Key);

                i++;
            }

            return tableCells;
        }

        private SiaSun.LMS.Common.Report.TableCellType CreateTableCell(string fieldName)
        {
            SiaSun.LMS.Common.Report.TableCellType tableCell = new SiaSun.LMS.Common.Report.TableCellType();
            tableCell.Items = new object[] { CreateTableCellReportItems(fieldName) };
            return tableCell;
        }

        private SiaSun.LMS.Common.Report.ReportItemsType CreateTableCellReportItems(string fieldName)
        {
            SiaSun.LMS.Common.Report.ReportItemsType reportItems = new SiaSun.LMS.Common.Report.ReportItemsType();
            reportItems.Items = new object[] { CreateTableCellTextbox(fieldName) };
            return reportItems;
        }

        private SiaSun.LMS.Common.Report.TextboxType CreateTableCellTextbox(string fieldName)
        {
            SiaSun.LMS.Common.Report.TextboxType textbox = new SiaSun.LMS.Common.Report.TextboxType();
            textbox.Name = string.Format("{0}_{1}", this._sTableName, fieldName);
            textbox.Items = new object[] 
                {
                    "=Fields!" + fieldName + ".Value",
                    CreateTableCellTextboxStyle(),
                    true,
                };
            textbox.ItemsElementName = new SiaSun.LMS.Common.Report.ItemsChoiceType14[] 
                {
                    SiaSun.LMS.Common.Report.ItemsChoiceType14.Value,
                    SiaSun.LMS.Common.Report.ItemsChoiceType14.Style,
                    SiaSun.LMS.Common.Report.ItemsChoiceType14.CanGrow,
                };
            return textbox;
        }

        private SiaSun.LMS.Common.Report.StyleType CreateTableCellTextboxStyle()
        {
            SiaSun.LMS.Common.Report.StyleType style = new SiaSun.LMS.Common.Report.StyleType();
            style.Items = new object[]
                {
                    "=iif(RowNumber(Nothing) mod 2, \"White\", \"AliceBlue\")",
                    "Center",
                    "zh-CN",
                    "����",
                     CreateBorderColorStyleWidth("Solid"),
                     CreateBorderColorStyleWidth("0.5pt"),
                };
            style.ItemsElementName = new SiaSun.LMS.Common.Report.ItemsChoiceType5[]
                {
                    SiaSun.LMS.Common.Report.ItemsChoiceType5.BackgroundColor,
                    SiaSun.LMS.Common.Report.ItemsChoiceType5.TextAlign,
                    SiaSun.LMS.Common.Report.ItemsChoiceType5.Language,
                    SiaSun.LMS.Common.Report.ItemsChoiceType5.FontFamily,
                    SiaSun.LMS.Common.Report.ItemsChoiceType5.BorderStyle,
                    SiaSun.LMS.Common.Report.ItemsChoiceType5.BorderWidth,
                };
            return style;
        }

        private SiaSun.LMS.Common.Report.TableColumnsType CreateTableColumns()
        {
            SiaSun.LMS.Common.Report.TableColumnsType tableColumns = new SiaSun.LMS.Common.Report.TableColumnsType();

            tableColumns.TableColumn = new SiaSun.LMS.Common.Report.TableColumnType[this._dtnc.Count];

            int i = 0;

            foreach (KeyValuePair<string, string> kvp in this._dtnc)
            {
                tableColumns.TableColumn[i] = CreateTableColumn(kvp.Value.Length * 9 * 2);

                i++;
            }

            return tableColumns;
        }

        private SiaSun.LMS.Common.Report.TableColumnType CreateTableColumn(int iWidth)
        {
            SiaSun.LMS.Common.Report.TableColumnType tableColumn = new SiaSun.LMS.Common.Report.TableColumnType();

            tableColumn.Items = new object[] { string.Format("{0}pt", iWidth.ToString()) };

            return tableColumn;
        }

        private SiaSun.LMS.Common.Report.UserSortType CreateUserSort(string fieldName)
        {
            SiaSun.LMS.Common.Report.UserSortType userSortType = new SiaSun.LMS.Common.Report.UserSortType();

            userSortType.Items = new object[] 
                {
                    "=Fields!" + fieldName + ".Value",
                };
            userSortType.ItemsElementName = new SiaSun.LMS.Common.Report.ItemsChoiceType13[] 
                {
                    SiaSun.LMS.Common.Report.ItemsChoiceType13.SortExpression,
                };

            return userSortType;
        }

        private SiaSun.LMS.Common.Report.BorderColorStyleWidthType CreateBorderColorStyleWidth(string s)
        {
            SiaSun.LMS.Common.Report.BorderColorStyleWidthType b = new SiaSun.LMS.Common.Report.BorderColorStyleWidthType();
            b.Items = new object[]
                 {
                     s,
                 };
            b.ItemsElementName = new SiaSun.LMS.Common.Report.ItemsChoiceType3[]
                 {
                     SiaSun.LMS.Common.Report.ItemsChoiceType3.Default,
                 };
            return b;
        }
    }
}