using Hotline.Application.ExportWord;
using Hotline.Share.Dtos.Early;
using Hotline.Share.Dtos.ExportWord;
using XF.Domain.Dependency;

namespace Hotline.Application.Early
{
    public class EarlyReportProvider : IEarlyReportProvder, IScopeDependency
    {
        public string PushReport(EarlyReportDto earlyReportDtoreportDto)
        {
            string summarize = CreateSummarize(earlyReportDtoreportDto);

            var reportDto = new ReportDto
            {
                CssStyle = "padding-left: 10%;padding-right: 10%;padding-top: 10px;",
                Title = new TitleDto
                {
                    Content = "宜宾市12345热线每日关注",
                    CssStyle = "font-weight:bolder;color: red;font-size: 50px;font-family:宋体; width: 100 %; text-align: center; word-spacing: 10px;letter-spacing: 2px; margin-bottom: 10; line-height: 2; "
                },
                SubTitleDtos = new List<SubTitleDto>
                {
                    new SubTitleDto
                    {
                        CssStyle = "font-weight: bold; font-size: 24px;font-family:方正楷体_GBK;width: 100%;\r\n text-align: center;letter-spacing: 5px;line-height: 1.5;",
                        Content = earlyReportDtoreportDto.ReportName
                    },
                    new SubTitleDto
                    {
                        CssStyle = "font-weight: bold; font-size: 24px;font-family:方正楷体_GBK;width: 100%;\r\n        text-align: center;letter-spacing: 5px;line-height: 1.5;",
                        Content = "（截止"+ earlyReportDtoreportDto.ReportPushTime.ToString("MM月dd日hh时")+"）"
                    },
                    new SubTitleDto
                    {
                        CssStyle = "font-weight: bold; font-size: 20px;font-family:楷体_GB2312;\r\n    width: 100%;letter-spacing: 5px;line-height: 1.5;\r\n    border-bottom:2px solid red;float: left;margin-bottom: 20px;",
                        Children =  new List<SubTitleDto>
                        {
                             new SubTitleDto
                             {
                                 CssStyle = "float: left;",
                                 Content = "市政务服务非公经济局"
                             },
                            new SubTitleDto
                            {
                                CssStyle="float: right;",
                                Content = earlyReportDtoreportDto.ReportPushTime.ToString("yyyy年MM月dd日")
                            }
                        }
                    }
                },
                ContentDto = new ContentDto
                {
                    CssStyle = "font-weight: normal; font-size: 18px;font-family:宋体;width: 100%;letter-spacing: 5px;line-height: 1.5;",
                    TitleDto = new TitleDto
                    {
                        Content = "领导批示：",
                        CssStyle = "font-weight: normal; font-size: 22px;font-family:黑体;width: 100%;letter-spacing: 5px;line-height: 1.5;"
                    },
                    ParagraphDtos = new List<ParagraphDto>
                    {
                        new ParagraphDto
                        {
                            CssStyle = "text-indent: 2rem;",
                            Content = summarize
                        }
                    }
                }
            };
            int index = FillParagraphes(earlyReportDtoreportDto, reportDto);

            var olderOrderName = $"{earlyReportDtoreportDto.ReportPushTime.Month}月{earlyReportDtoreportDto.ReportPushTime.Day}日重点诉求处置情况";

            reportDto.ContentDto.ParagraphDtos.Add(new ParagraphDto
            {
                Content = $"附件1：{olderOrderName}",
                CssStyle = "text-indent: 2rem;",
                SortIndex = ++index
            });

            var orderName = "前期重点问题办结情况";
            reportDto.ContentDto.ParagraphDtos.Add(new ParagraphDto
            {
                Content = $"附件2：{orderName}",
                CssStyle = "text-indent: 2rem;",
                SortIndex = ++index
            });

            reportDto.AttachmentDtos = [];
            AttachmentDto orderAttachmentDto = CreateOrderAttachment(earlyReportDtoreportDto, olderOrderName);
            reportDto.AttachmentDtos.Add(orderAttachmentDto);
            AttachmentDto fileOrderAttachmentDto = CreateFileOrderAttachment(earlyReportDtoreportDto, orderName);
            reportDto.AttachmentDtos.Add(fileOrderAttachmentDto);

            reportDto.BottomDtos = new List<BaseReportDto>
            {
                new BaseReportDto
                {
                    CssStyle = "font-weight: normal; font-size: 18px;font-family:楷体_GB2312;\r\n    width: 100%;letter-spacing: 2px;line-height: 1;\r\n    border-bottom:1px solid #000;float: left;margin-bottom: 20px;",
                    Children = new List<BaseReportDto>
                    {
                        new BaseReportDto
                        {
                            CssStyle = "float: left;",
                            Content = "信息公开属性："
                        },new BaseReportDto
                        {
                            CssStyle = "font-weight: bolder;float: left;",
                            Content = "不予公开"
                        }
                    }
                },
                new BaseReportDto
                {
                    CssStyle = "font-weight: normal; font-size: 18px;font-family:楷体_GB2312;\r\n    width: 100%;letter-spacing: 2px;line-height: 1;\r\n    border-bottom:1px solid #000;float: left;margin-bottom: 20px;",
                    Content = "主送：市长，常务副市长、分管副市长，联系副秘书长。"
                },new BaseReportDto
                {
                    CssStyle="font-weight: normal; font-size: 18px;font-family:楷体_GB2312;\r\n    width: 100%;letter-spacing: 2px;line-height: 1;float: left;margin-bottom: 20px;",
                    Children = new List<BaseReportDto>
                    {
                        new BaseReportDto
                        {
                            CssStyle = "float:left;",
                            Content = "市政务服务非公经济发展局"
                        },
                        new BaseReportDto
                        {
                            CssStyle = "float:right;",
                            Content = $"{earlyReportDtoreportDto.ReportPushTime.ToString()}印发"
                        }
                    }
                }
            };

            return reportDto.RenderHtml();
        }

        private static AttachmentDto CreateOrderAttachment(EarlyReportDto earlyReportDtoreportDto, string olderOrderName)
        {
            TableDto orderTableDto = CreateOrderTable(earlyReportDtoreportDto);
            var attachmentDto = new AttachmentDto
            {
                Title = new TitleDto
                {
                    CssStyle = "font-weight: bold; font-size: 18px;font-family:宋体;width: 100%; float:right;",
                    Content = "附件1"
                },
                SubTitleDtos = new List<SubTitleDto>
               {
                   new SubTitleDto
                   {
                       CssStyle = "font-weight: bold; font-size: 24px;font-family:宋体;width: 100%; text-align: center;margin-bottom: 20px;",
                       Content = olderOrderName
                   }
               },
                TableDtos = new List<TableDto>
                {
                   orderTableDto
                }
            };
            return attachmentDto;
        }

        private static AttachmentDto CreateFileOrderAttachment(EarlyReportDto earlyReportDtoreportDto, string olderOrderName)
        {
            TableDto orderTableDto = CreateFileOrderTable(earlyReportDtoreportDto);
            var attachmentDto = new AttachmentDto
            {
                Title = new TitleDto
                {
                    CssStyle = "font-weight: bold; font-size: 18px;font-family:宋体;width: 100%; float:right;",
                    Content = "附件2"
                },
                SubTitleDtos = new List<SubTitleDto>
               {
                   new SubTitleDto
                   {
                       CssStyle = "font-weight: bold; font-size: 24px;font-family:宋体;width: 100%; text-align: center;margin-bottom: 20px;",
                       Content = olderOrderName
                   }
               },
                TableDtos = new List<TableDto>
                {
                   orderTableDto
                }
            };
            return attachmentDto;
        }

        private static TableDto CreateFileOrderTable(EarlyReportDto earlyReportDtoreportDto)
        {
            var tableDto = new TableDto
            {
                CssStyle = "border-collapse: collapse;width: 100%;"
            };
            tableDto.HeadDto.RowDtos.Add(new RowDto
            {
                ColumnDtos = new List<ColumnDto>
                {
                    new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;width: 10%;text-align: center;",
                            Content = "序号",
                        },
                    new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;width: 10%;text-align: center;",
                            Content = "期刊",
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;width: 10%;text-align: center;",
                            Content = "诉求来源",
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;width: 20%;text-align: center;",
                            Content = "问题类型",
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;width: 25%;text-align: center;",
                            Content = "诉求内容",
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;width: 25%;text-align: center;",
                            Content = "答复情况",
                        }
                }
            });
            var number = 1;
            earlyReportDtoreportDto.ReportFiledOrderDetails.ForEach(item =>
            {
                tableDto.BodyDto.RowDtos.Add(new RowDto
                {
                    CssStyle = "border: solid 1px #000;",
                    ColumnDtos = new List<ColumnDto>
                    {
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;text-align: center;",
                            Content = number.ToString(),
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;text-align: center;",
                            Content = item.ReportName,
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;text-align: center;",
                            Content = item.OneHotSportName,
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;text-align: center;",
                            Content = item.HotSportName,
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;",
                            Content = item.Content,
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;",
                            Content = item.ActualOpinion,
                        }
                    }
                });
                number++;
            });
            return tableDto;
        }

        private static TableDto CreateOrderTable(EarlyReportDto earlyReportDtoreportDto)
        {
            var tableDto = new TableDto
            {
                CssStyle = "border-collapse: collapse;width: 100%;"
            };
            tableDto.HeadDto.RowDtos.Add(new RowDto
            {
                ColumnDtos = new List<ColumnDto>
                {
                    new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;width: 10%;text-align: center;",
                            Content = "序号",
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;width: 20%;text-align: center;",
                            Content = "诉求来源",
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;width: 20%;text-align: center;",
                            Content = "问题类型",
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;width: 25%;text-align: center;",
                            Content = "诉求内容",
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;width: 25%;text-align: center;",
                            Content = "办理情况",
                        }
                }
            });
            var number = 1;
            earlyReportDtoreportDto.ReportOrderDetails.ForEach(item =>
            {
                tableDto.BodyDto.RowDtos.Add(new RowDto
                {
                    CssStyle = "border: solid 1px #000;",
                    ColumnDtos = new List<ColumnDto>
                    {
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;text-align: center;",
                            Content = number.ToString(),
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;text-align: center;",
                            Content = item.OneHotSportName,
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;text-align: center;",
                            Content = item.HotSportName,
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;",
                            Content = item.Content,
                        },
                        new ColumnDto
                        {
                            CssStyle = "border: solid 1px #000;",
                            Content = item.ActualOpinion,
                        }
                    }
                });
                number++;
            });
            return tableDto;
        }

        private static int FillParagraphes(EarlyReportDto earlyReportDtoreportDto, ReportDto reportDto)
        {
            var index = 1;
            earlyReportDtoreportDto.ReportHotDetails.ForEach(item =>
            {
                reportDto.ContentDto.ParagraphDtos.Add(new ParagraphDto
                {
                    titleDto = new TitleDto
                    {
                        Content = $"{ToChineseNumber(index)}、{item.HotName}问题",
                        CssStyle = "text-indent: 2rem;font-weight: bolder;",
                    },
                    Content = $"今日收到{item.HotName}问题{item.Count}件，主要涉及:{item.Content}",
                    CssStyle = "text-indent: 2rem;",
                    SortIndex = index
                });
                index++;
            });
            return index;
        }

        private static string CreateSummarize(EarlyReportDto earlyReportDtoreportDto)
        {
            var hotDetails = new List<string>();
            earlyReportDtoreportDto.ReportHotDetails.ForEach(item =>
            {
                hotDetails.Add($"{item.HotName}{item.Count}件");
            });
            var areaDetails = new List<string>();
            earlyReportDtoreportDto.ReportAreaDetails.ForEach(item =>
            {
                areaDetails.Add($"{item.AreaName}{item.Count}件");
            });
            var summarize = $"{earlyReportDtoreportDto.ReportPushTime.ToString("yyyy年MM月dd日 HH时mm分ss秒")}，市12345热线共受理重点领域问题{earlyReportDtoreportDto.ReportHotDetails.Sum(x => x.Count)}件，其中，{string.Join("、", hotDetails)}。从区域分布来看，{string.Join("，", areaDetails)}。";
            return summarize;
        }

        private static readonly string[] ChineseNumbers =
             {
        "零", "一", "二", "三", "四",
        "五", "六", "七", "八", "九", "十"
    };

        public static string ToChineseNumber(int number)
        {
            if (number < 0 || number > 10)
            {
                throw new ArgumentOutOfRangeException(nameof(number), "输入数字必须在 0-10 范围内");
            }
            return ChineseNumbers[number];
        }
    }
}
