using System.Drawing;
using GxPress.Service.Interface.Doc;
using Spire.Doc;
using Spire.Doc.Documents;
using GxPress.Service.Interface;
using System.Threading.Tasks;
using GxPress.Result.App.Flow;
using GxPress.Common.Tools;
using System;
using System.Collections.Generic;
using System.Linq;

namespace GxPress.Service.Implement.Doc
{
    public class DocService : IDocService
    {
        private readonly IFlowService _flowService;
        public DocService(IFlowService flowService)
        {
            _flowService = flowService;
        }
        public async Task<string> MakeDoc(int flowId, int userId)
        {
            //获取Flow
            var flowDto = await _flowService.GetFlowResult(flowId, userId);
            //创建一个Document实例
            Document doc = new Document();

            //添加一个section
            Section sec = doc.AddSection();
            //添加三个段落并写入文字
            Paragraph para1 = sec.AddParagraph();
            para1.AppendText(flowDto.Name);

            //将第一段作为标题，设置标题格式
            ParagraphStyle style1 = new ParagraphStyle(doc);
            style1.Name = "titleStyle";
            style1.CharacterFormat.Bold = true;
            style1.CharacterFormat.TextColor = Color.Black;
            style1.CharacterFormat.FontName = "楷体";
            style1.CharacterFormat.FontSize = 18f;
            doc.Styles.Add(style1);
            para1.ApplyStyle("titleStyle");
            //设置其余两个段落的格式
            ParagraphStyle style2 = new ParagraphStyle(doc);
            style2.Name = "paraStyle";
            style2.CharacterFormat.FontName = "楷体";
            style2.CharacterFormat.FontSize = 15f;
            doc.Styles.Add(style2);
            //设置三个段落的对齐方式
            para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
            //设置段首缩进
            // para2.Format.FirstLineIndent = 30f;
            //设置段落之间的间距
            para1.Format.AfterSpacing = 15f;
            //创建表格
            var teable = CreateDocHeadTable(sec, flowDto);
            //流程
            // CreateDocFlowTable(teable, flowDto);
            //保存文档
            var ticks = DateTime.Now.Ticks.ToString();
            doc.SaveToFile($"wwwroot/cache/doc/{ticks}.docx", FileFormat.Docx2013);
            return $"/cache/doc/{ticks}.docx";
        }
        /// <summary>
        /// 创建表格
        /// </summary>
        /// <param name="section"></param>
        /// <returns></returns>
        private Table CreateDocHeadTable(Section section, FlowResult flowDto)
        {
            var site = new Dictionary<string, string>();
            site.Add("审批标号", flowDto.No);
            site.Add("所在部门", flowDto.DepartmentName);
            site.Add("提交时间", Convert.ToDateTime(flowDto.CreatedDate).ToString("yyyy-MM-dd HH:mm:ss"));
            Table table = section.AddTable(true);
            table.ResetCells(site.Count + flowDto.FormFields.Count + 2 + flowDto.Nodes.Count + 1, 4);
            TableRow row = table.Rows[0];
            row.IsHeader = true;
            int i = 0;
            foreach (var item in site)
            {
                //合并单元格
                table.ApplyHorizontalMerge(i, 0, 1);
                var para = table[i, 0].AddParagraph();
                var TR1 = para.AppendText(item.Key);
                //合并单元格
                table.ApplyHorizontalMerge(i, 2, 3);
                para = table[i, 2].AddParagraph();
                TR1 = para.AppendText(item.Value);
                i++;
            }
            foreach (var item in flowDto.FormFields)
            {
                table.ApplyHorizontalMerge(i, 0, 1);
                var TR2 = table[i, 0].AddParagraph().AppendText(item.Label);
                TR2.CharacterFormat.FontName = "Arial";
                TR2.CharacterFormat.FontSize = 10;
                table.ApplyHorizontalMerge(i, 2, 3);
                TR2 = table[i, 2].AddParagraph().AppendText(item.Value);
                TR2.CharacterFormat.FontName = "Arial";
                TR2.CharacterFormat.FontSize = 10;
                i++;
            }
            table.ApplyHorizontalMerge(i, 0, 3);
            var TR = table[i, 0].AddParagraph().AppendText("审核流程");
            TR.CharacterFormat.FontName = "Arial";
            TR.CharacterFormat.FontSize = 14;
            i++;
            //审核流程
            TR = table[i, 0].AddParagraph().AppendText("姓名");
            TR = table[i, 1].AddParagraph().AppendText("状态");
            TR = table[i, 2].AddParagraph().AppendText("备注");
            TR = table[i, 3].AddParagraph().AppendText("时间");
            i++;
            foreach (var item in flowDto.Nodes)
            {
                var TR3 = table[i, 0].AddParagraph().AppendText(item.Name);
                TR3.CharacterFormat.FontName = "Arial";
                TR3.CharacterFormat.FontSize = 10;
                TR3 = table[i, 1].AddParagraph().AppendText(item.Action);
                TR3.CharacterFormat.FontName = "Arial";
                TR3.CharacterFormat.FontSize = 10;
                TR3 = table[i, 2].AddParagraph().AppendText(item.Message);
                TR3.CharacterFormat.FontName = "Arial";
                TR3.CharacterFormat.FontSize = 10;
                TR3 = table[i, 3].AddParagraph().AppendText(Convert.ToDateTime(item.CreatedDate).ToString("yyyy-MM-dd HH:mm:ss"));
                TR3.CharacterFormat.FontName = "Arial";
                TR3.CharacterFormat.FontSize = 10;
                i++;
            }

            TR = table[i, 0].AddParagraph().AppendText("抄送人");
            TR.CharacterFormat.FontName = "Arial";
            TR.CharacterFormat.FontSize = 10;
            table.ApplyHorizontalMerge(i, 1, 3);
            TR = table[i, 0].AddParagraph().AppendText(StringUtils.ObjectCollectionToString(flowDto.CarbonCopyUsers.Select(n => n.Name), "丶"));
            TR.CharacterFormat.FontName = "Arial";
            TR.CharacterFormat.FontSize = 10;
            return table;
        }

    }
}