package com.zjty.inspect.service.impl;

import com.zjty.inspect.entity.DocumentContent;
import com.zjty.inspect.entity.DocumentPage;
import com.zjty.inspect.entity.TitleHead;
import com.zjty.inspect.enums.DocumentTitleEnum;
import com.zjty.inspect.service.DocumentService;
import com.zjty.inspect.utils.DocumentWordUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.security.cert.X509Certificate;
import java.util.List;

@Service
public class DocumentServiceImpl implements DocumentService {
    @Override
    public String createDocument(DocumentPage documentPage) {
        DocumentWordUtil documentWordUtil=new DocumentWordUtil();
        StringBuilder sb=new StringBuilder();
        //前面部分内容
        String standard = documentWordUtil.standard();
        String enddard = documentWordUtil.enddard();
        //标题内容
        String maintitle = documentWordUtil.Maintitle();
        //标题下的内容
        String standContent = documentWordUtil.content();

        //表格内容

        //页眉
        String header = documentPage.getHeader();
        String s1 = maintitle.replaceAll("#title#", header);
        sb.append(standard);
        sb.append(s1);
        List<TitleHead> titleHeads = documentPage.getTitleHeads();
        if(titleHeads!=null&&titleHeads.size()>0){
            for (TitleHead titleHead : titleHeads) {
                DocumentTitleEnum documentTitleEnum = DocumentTitleEnum.getByCode(titleHead.getParentId());
                if(documentTitleEnum!=null) {
                    String titleContent = documentWordUtil.subheading(documentTitleEnum);
                    //标题名称
                    String name = titleHead.getName();
                    if (!StringUtils.isEmpty(name)) {
                        //标题级别
                        String s = titleContent.replaceAll("#title#", name);
                        sb.append(s);
                    }
                    List<DocumentContent> documentContents = titleHead.getDocumentContents();
                    if (documentContents != null && documentContents.size() > 0) {
                        for (DocumentContent documentContent : documentContents) {
                            if (documentContent != null) {
                                String content = documentContent.getContent();
                                String baseUrl = documentContent.getBaseUrl();
                                String documentTable = documentContent.getDocumentTable();
                                if (!StringUtils.isEmpty(content)) {
                                    String c = standContent;
                                    String s = c.replaceAll("#content#", content);
                                    sb.append(s);
                                }
                                if (!StringUtils.isEmpty(baseUrl)) {

                                }
                                if (!StringUtils.isEmpty(documentTable)) {
                                    sb.append(documentTable);
                                }
                            }
                        }
                    }
                }
            }
        }
        sb.append(enddard);
        return sb.toString();
    }
}
