using DevExpress.XtraReports.UI;
using iBemsDataService.Controllers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Web;

namespace iBemsDataService.Util
{
    public class ReportTableHandler
    {
        public static XRTable CreateTableFromEnergyWithWidth<T>(List<T> list, float[] columnWidth)
        {
            XRTable table = CreateTableFromEnergy(list);
            AdjustTextSizeEnergy(table, columnWidth);
            return table;
        }

        public static XRTable CreateTableFromEnergy<T>(List<T> list)
        {
            XRTable table = new XRTable();
            table.LocationF = new PointF(0, 0);
            table.Font = new System.Drawing.Font("맑은 고딕", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            table.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleRight;

            List<XRTableRow> rowListtop = ConvertEnergyToTableRowstop(list);

            for (var i = 0; i < rowListtop.Count; i++)
            {
                table.Rows.Add(rowListtop[i]);
            }
            table.AdjustSize();
            table.EndInit();
            table.WidthF = 839f;
            return table;
        }
        private static List<XRTableRow> ConvertEnergyToTableRowstop<T>(List<T> list)
        {
            List<XRTableRow> rowList = new List<XRTableRow>();
            String[] Text = { "TextResultDate", "TextResult" };
            Type type = typeof(T);

            foreach (T item in list)
            {
                XRTableRow newRow = MakeXrTableRowTemplate();
                foreach (PropertyInfo propertyInfo in type.GetProperties())
                {
                    var obj = propertyInfo.GetValue(item);
                    String cellText = "";
                    bool colorchekck = false;
                    if (propertyInfo.Name.ToString() == "Name")
                    {
                        if (obj != null)
                        {
                            cellText = (String)obj;
                            if (cellText.IndexOf("합계") != -1)
                            {
                                colorchekck = true;
                            }
                        }
                    }
                    else if (propertyInfo.Name.ToString() == "Today")
                    {
                        if (obj != null)
                        {
                            cellText = obj.ToString();
                        }
                    }
                    else if (propertyInfo.Name.ToString() == "Daybefore")
                    {
                        if (obj != null)
                        {
                            cellText = obj.ToString();
                        }
                    }
                    else if (propertyInfo.Name.ToString() == "Dailyusage")
                    {
                        if (obj != null)
                        {
                            cellText = obj.ToString();
                        }
                    }
                    else
                    {
                        if (obj != null)
                        {
                            cellText = obj.ToString();
                        }
                    }
                    XRTableCell newcell = new XRTableCell();
                    newcell.Text = cellText;
                    if (propertyInfo.Name.ToString() == "Name")
                    {
                        newcell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                    }
                    newRow.Cells.Add(newcell);
                    if (colorchekck)
                    {
                        newRow.BackColor = Color.FromArgb(255, 255, 204);
                        colorchekck = false;
                    }
                }
                rowList.Add(newRow);
            }
            return rowList;
        }

        public static XRTable CreateTableFromListWithColumnWidth<T>(List<T> list, float[] columnWidth, String[] exceptions = null)
        {
            XRTable table = CreateTableFromList(list, exceptions);
            AdjustTextSize(table, columnWidth);
            return table;
        }

        public static XRTable CreateTableFromListWithColumnWidthPatrol<T>(List<T> list, float[] columnWidth, String[] exceptions = null)
        {
            XRTable table = CreateTableFromListPatrol(list, exceptions);
            AdjustTextSizePatrol(table, columnWidth);
            return table;
        }

        public static XRTable CreateTableFromList<T>(List<T> list, String[] exceptions = null)
        {
            XRTable table = new XRTable();
            table.LocationF = new PointF(0, 0);
            table.Font = new System.Drawing.Font("맑은 고딕", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            table.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
            table.BeginInit();

            List<XRTableRow> rowListtop = ConvertListToTableRowstop(list, exceptions);
            List<XRTableRow> rowListbottom = ConvertListToTableRowsbottom(list, exceptions);

            for (var i = 0; i < rowListtop.Count; i++)
            {
                table.Rows.Add(rowListtop[i]);
                table.Rows.Add(rowListbottom[i]);
            }
            for (var i = 1; i < rowListtop.Count * 2; i += 2)
            {
                table.Rows[i].TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft;
            }
            table.AdjustSize();
            table.EndInit();
            table.WidthF = 839f;
            return table;
        }

        public static XRTable CreateTableFromListPatrol<T>(List<T> list, String[] exceptions = null)
        {
            XRTable table = new XRTable();
            table.LocationF = new PointF(0, 0);
            table.Font = new System.Drawing.Font("맑은 고딕", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            table.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
            table.BeginInit();

            List<XRTableRow> rowListtop = ConvertListToTableRowstop(list, exceptions);
            List<XRTableRow> rowListbottom = ConvertListToTableRowsbottom(list, exceptions);

            for (var i = 0; i < rowListtop.Count; i++)
            {
                table.Rows.Add(rowListtop[i]);
                //table.Rows.Add(rowListbottom[i]);
            }
            //for (var i = 1; i < rowListtop.Count; i += 2)
            //{
            //    table.Rows[i].TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft;
            //}
            table.AdjustSize();
            table.EndInit();
            table.WidthF = 800f;
            return table;
        }

        public static XRTable CreateTableFromListWithColumnWidthMaterial<T>(List<T> list, float[] columnWidth, String[] exceptions = null)
        {
            XRTable table = CreateTableFromListMaterial(list, exceptions);
            AdjustTextSize(table, columnWidth);
            return table;
        }

        public static XRTable CreateTableFromListMaterial<T>(List<T> list, String[] exceptions = null)
        {
            XRTable table = new XRTable();
            table.LocationF = new PointF(0, 0);
            table.Font = new System.Drawing.Font("맑은 고딕", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            table.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
            table.BeginInit();

            List<XRTableRow> rowListtop = ConvertListToTableRowstop(list, exceptions);

            for (var i = 0; i < rowListtop.Count; i++)
            {
                table.Rows.Add(rowListtop[i]);
            }
            table.AdjustSize();
            table.EndInit();
            table.WidthF = 839f;
            return table;
        }

        private static void AdjustTextSize(XRTable table, float[] columnWidth)
        {
            float[] columnWidthbottom = { 230f, 609f, 839f };
            foreach (XRTableRow row in table.Rows)
            {
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    if (row.Cells.Count > 2)
                    {
                        XRTableCell cell = row.Cells[i];
                        if (columnWidth != null)
                        {
                            cell.SizeF = new SizeF(columnWidth[i], cell.SizeF.Height);
                        }
                    }
                    else
                    {
                        XRTableCell cell = row.Cells[i];
                        if (columnWidth != null)
                        {
                            cell.SizeF = new SizeF(columnWidthbottom[2], cell.SizeF.Height);
                        }
                    }
                }
            }
        }
        private static void AdjustTextSizePatrol(XRTable table, float[] columnWidth)
        {
            float[] columnWidthbottom = { 230f, 609f, 800f };
            foreach (XRTableRow row in table.Rows)
            {
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    if (row.Cells.Count > 2)
                    {
                        XRTableCell cell = row.Cells[i];
                        if (columnWidth != null)
                        {
                            cell.SizeF = new SizeF(columnWidth[i], cell.SizeF.Height);
                        }
                    }
                    else
                    {
                        XRTableCell cell = row.Cells[i];
                        if (columnWidth != null)
                        {
                            cell.SizeF = new SizeF(columnWidthbottom[2], cell.SizeF.Height);
                        }
                    }
                }
            }
        }

        private static void AdjustTextSizeEnergy(XRTable table, float[] columnWidth)
        {
            foreach (XRTableRow row in table.Rows)
            {
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    XRTableCell cell = row.Cells[i];
                    if (columnWidth != null)
                    {
                        cell.SizeF = new SizeF(columnWidth[i], cell.SizeF.Height);
                    }
                }
            }
        }

        private static List<XRTableRow> ConvertListToTableRowstop<T>(List<T> list, String[] exceptions)
        {
            List<XRTableRow> rowList = new List<XRTableRow>();
            Type type = typeof(T);
            String[] Text = { "TextResultDate", "TextResult" };
            foreach (T item in list)
            {
                XRTableRow newRow = MakeXrTableRowTemplate();
                foreach (PropertyInfo propertyInfo in type.GetProperties())
                {
                    if (exceptions != null && exceptions.Contains(propertyInfo.Name)) continue;
                    if (Text.Contains(propertyInfo.Name)) continue;

                    var obj = propertyInfo.GetValue(item);
                    String cellText = null;
                    if (obj != null)
                    {
                        cellText = obj.ToString();
                    }
                    XRTableCell newCell = new XRTableCell();
                    newCell.Text = cellText;
                    newRow.Cells.Add(newCell);
                }
                rowList.Add(newRow);
            }
            return rowList;
        }

        private static List<XRTableRow> ConvertListToTableRowsbottom<T>(List<T> list, String[] exceptions)
        {
            List<XRTableRow> rowList = new List<XRTableRow>();

            Type type = typeof(T);
            String[] Text = { "TextResultDate", "TextResult" };
            foreach (T item in list)
            {
                XRTableRow newRow = MakeXrTableRowTemplate();
                //String cellText = null;
                String[] temptext = new string[1];
                foreach (PropertyInfo propertyInfo in type.GetProperties())
                {
                    if (exceptions != null && exceptions.Contains(propertyInfo.Name)) continue;
                    if (!Text.Contains(propertyInfo.Name)) continue;

                    var obj = propertyInfo.GetValue(item);

                    if (propertyInfo.Name.ToString() == "TextResultDate")
                    {
                        if (obj != null)
                        {
                            DateTime[] TextResultDate = (DateTime[])obj;
                            temptext = new string[TextResultDate.Count()];
                        }
                        else
                        {
                            temptext = null;
                        }
                    }

                    if (propertyInfo.Name.ToString() == "TextResultDate")
                    {
                        DateTime[] TextResultDate = (DateTime[])obj;
                        if (TextResultDate != null)
                        {
                            for (var i = 0; i < TextResultDate.Count(); i++)
                            {
                                temptext[i] = "   " + TextResultDate[i].ToString("yyyy-MM-dd");
                            }
                        }
                        else
                        {
                            //for (var i = 0; i < TextResultDate.Count(); i++)
                            //{
                            //    temptext[i] = "";
                            //}
                        }
                    }
                    else if (propertyInfo.Name.ToString() == "TextResult")
                    {
                        String[] TextResult = (String[])obj;
                        //var seven = 50;
                        if (TextResult != null)
                        {
                            for (var i = 0; i < TextResult.Count(); i++)
                            {
                                //var result = TextResult[i].Replace("\n", " ");       //.replace(/\n/gi, '</p><p>&emsp;&emsp;&emsp;&emsp;&emsp;&ensp;');
                                //temptext[i] += " - " + result + "\r\n";
                                var result = TextResult[i].Replace("\n", "\n                  ");
                                result = result.Replace("?-", " -");
                                temptext[i] += " - " + result + "\r\n";
                                //if (temptext[i].Length > seven)
                                //{
                                //    temptext[i] = factorial(temptext[i]);
                                //}
                            }
                        }
                        else
                        {
                            //for (var i = 0; i < TextResult.Count(); i++)
                            //{
                            //    temptext[i] += "";
                            //}
                        }
                    }
                }

                XRTableCell newCell = new XRTableCell();
                string finaltempstring = "";
                if (temptext != null)
                {
                    for (var i = 0; i < temptext.Length; i++)
                    {
                        finaltempstring += temptext[i];
                    }
                }
                else
                {
                    finaltempstring = "";
                }
                newCell.Text = finaltempstring;
                newCell.Multiline = true;
                newRow.Cells.Add(newCell);
                rowList.Add(newRow);
            }
            return rowList;
        }

        public static string factorial(string temp)
        {
            var seven = 50;
            var forcnt = temp.Length / seven; //몫
            var forcnt1 = temp.Length % seven; //나머지
            if (forcnt == 0)
            {
                return temp;
            }
            else if (forcnt != 0 && forcnt1 != 0)
            {
                return temp.Substring(0, seven) + "\r\n" + factorial(temp.Substring(seven));
            }
            else
            {
                return temp.Substring(0, seven);
            }
        }

        private static XRTableRow MakeXrTableRowTemplate()
        {
            XRTableRow newRow = new XRTableRow();
            newRow.BorderColor = Color.Black;
            newRow.BorderWidth = 1;
            newRow.Borders = DevExpress.XtraPrinting.BorderSide.All;
            newRow.HeightF = 30f;
            return newRow;
        }
    }
}