package com.salpa.subject.domain.generator;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.HackLoopTableRenderPolicy;
import com.salpa.common.exception.SystemException;
import com.salpa.subject.domain.data.TableDocumentResponse;
import com.salpa.subject.domain.vo.ColumnVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
@RequiredArgsConstructor
public class WordFileGenerator implements DocumentFileGenerator {

    @Override
    public boolean support(DocumentFileType type) {
        return type == DocumentFileType.WORD;
    }

    @Override
    public void generate(DocumentFileGenerateContext context, OutputStream outputStream) {
        try {
            Map<String, Object> data = new HashMap<>();
            data.put("databaseName",context.getDatabaseDocument().getDatabaseName());
            List<Map<String,Object>> tList=new ArrayList<>();
            List<Map<String, Object>> tables = new ArrayList<>();
            List<TableDocumentResponse> tables1 = context.getDatabaseDocument().getTables();
            for (TableDocumentResponse tableDocumentResponse : tables1) {
                Map<String, Object> table = new HashMap<>();
                table.put("tableName",tableDocumentResponse.getName());
                table.put("tableType",tableDocumentResponse.getType());
                table.put("tableComment",tableDocumentResponse.getComment());
                tables.add(table);
                List<TableDocumentResponse.ColumnDocumentResponse> columns1 = tableDocumentResponse.getColumns();
                List<ColumnVo> columns = new ArrayList<>();
                Map<String,Object> tMap = new HashMap<>();
                for (TableDocumentResponse.ColumnDocumentResponse column : columns1) {
                    ColumnVo columnVo = new ColumnVo();
                    columnVo.setName(column.getName());
                    columnVo.setType(column.getType() + "(" + column.getSize() + ")");
                    if (column.getIsPrimaryKey()) {
                        columnVo.setPrimaryKey("YES");
                    } else {
                        columnVo.setPrimaryKey("NO");
                    }
                    columnVo.setNullable(column.getNullable());
                    columnVo.setAutoIncrement(column.getAutoIncrement());
                    columnVo.setDefaultValue(column.getDefaultValue());
                    columnVo.setComment(column.getComment());
                    columns.add(columnVo);
                }
                tMap.put("tableName",tableDocumentResponse.getName());
                tMap.put("columns",columns);
                tList.add(tMap);
            }
            data.put("tables",tables);
            data.put("tableColumns",tList);
            InputStream inputStream = new ClassPathResource("word.docx").getInputStream();
            HackLoopTableRenderPolicy  policy = new HackLoopTableRenderPolicy();
            Configure config = Configure.newBuilder().bind("tables", policy).bind("columns",policy).build();
            XWPFTemplate template = XWPFTemplate.compile(inputStream,config).render(data);
            template.write(outputStream);
        } catch (IOException e) {
            throw new SystemException("System error");
        }
    }


}
