using MWS.Domain.Dto;
using System;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace MWS.Domain.Prints
{
    public class TableDocumentRenderer : IDocumentRenderer
    {
        public void Render<T>(FlowDocument doc, object data)
        {

            var source = (PrintTableDto<T>)data;
            Style styleCell = doc.Resources["BorderedCell"] as Style;

            (doc.FindName("Title") as TextBlock).Text = source.Title;
            (doc.FindName("LeftSubtitle") as TextBlock).Text = source.LeftSubtitle;
            (doc.FindName("RightSubtitle") as TextBlock).Text = source.RightSubtitle;
            (doc.FindName("Footer") as TextBlock).Text = source.Footer;
            var table = doc.FindName("Content") as Table;
            var header = new TableRow() { FontWeight = FontWeights.Bold, FontSize = 14 };
            foreach (var column in source.Columns)
            {
                table.Columns.Add(new TableColumn() { Width = column.Width });
                header.Cells.Add(new TableCell(new Paragraph(new Run(column.DisplayName))) { Style = styleCell, TextAlignment = TextAlignment.Center });
            }
            var rowGroup = new TableRowGroup();
            rowGroup.Rows.Add(header);
            table.RowGroups.Add(rowGroup);

            if (!source.Data.Any())
                return;

            PropertyInfo[] properties = source.Data[0].GetType().GetProperties();
            TableRow row;
            TableCell cell;
            PropertyInfo property;
            foreach (var item in source.Data)
            {
                row = new TableRow();
                foreach (var column in source.Columns)
                {
                    property = properties.FirstOrDefault(c => c.Name == column.FiledName);
                    cell = new TableCell(new Paragraph(new Run(property == null ? "" : ObjectFomart(property.PropertyType, property.GetValue(item), column.FormatString))))
                    {
                        Style = styleCell
                    };
                    row.Cells.Add(cell);
                }
                rowGroup.Rows.Add(row);
            }

        }
        public string ObjectFomart(Type type, object value, string fomartString)
        {
            switch (type.Name)
            {
                case "Int16":
                case "Int32":
                case "Int64":
                    return Convert.ToInt64(value).ToString(fomartString ?? "G");
                case "UInt16":
                case "UInt32":
                case "UInt64":
                    return Convert.ToUInt64(value).ToString(fomartString ?? "G");
                case "Double":
                case "Float":
                    return Convert.ToDouble(value).ToString(fomartString ?? "G");
                case "Boolean":
                    return ((bool)value) ? "是" : "否";
                case "DateTime":
                    return Convert.ToDateTime(value).ToString(fomartString ?? "G");
                default:
                    return value.ToString();
            }

        }
    }
}
