package com.tzld.piaoquan.growth.common.model.bo;

import com.alibaba.fastjson.JSONObject;
import com.tzld.piaoquan.growth.common.common.enums.FieshuTableColumnDataTypeEnum;
import lombok.Data;

import java.util.List;

@Data
public class FeishuTableDTO {
    private Header header;
    private List<Element> elements;

    @Data
    public static class Element {
        private String tag;
        private JSONObject text;
        private long page_size;
        private String row_height;
        private HeaderStyle header_style;
        private List<Column> columns;
        private List<JSONObject> rows;
    }

    @Data
    public static class Column {
        private String name;
        private String display_name;
        private String data_type;
        private String horizontal_align;
        private String vertical_align;
        private String width;
        private String format;
        private String dateFormat;
    }

    @Data
    public static class HeaderStyle {
        private String text_align;
        private String text_size;
        private String background_style;
        private String text_color;
        private boolean bold;
        private long lines;
    }

    @Data
    public static class Header {
        private String template;
        private Title title;
    }

    @Data
    public static class Title {
        private String content;
        private String tag;
    }

    public static FeishuTableDTO createTable(String title, List<Column> columns, List<JSONObject> rows, boolean atAll) {
        String tableStr = "{\n" +
                "    \"header\":\n" +
                "    {\n" +
                "        \"template\": \"blue\",\n" +
                "        \"title\":\n" +
                "        {\n" +
                "            \"content\": \"" + title + "\",\n " +
                "            \"tag\": \"plain_text\"\n" +
                "        }\n" +
                "    },\n" +
                "    \"elements\":\n" +
                "    [\n" +
                "        {\n" +
                "            \"tag\": \"table\",\n" +
                "            \"page_size\": " + rows.size() + ",\n" +
                "            \"row_height\": \"low\",\n" +
                "            \"header_style\":\n" +
                "            {\n" +
                "                \"text_align\": \"left\",\n" +
                "                \"text_size\": \"normal\",\n" +
                "                \"background_style\": \"none\",\n" +
                "                \"text_color\": \"grey\",\n" +
                "                \"bold\": true,\n" +
                "                \"lines\": 1\n" +
                "            },\n" +
                "            \"columns\":\n" +
                JSONObject.toJSONString(columns) +
                "            ,\n" +
                "            \"rows\":\n" +
                JSONObject.toJSONString(rows) +
                "        }\n" +
                "    ]\n" +
                "}";
        FeishuTableDTO result =  JSONObject.parseObject(tableStr, FeishuTableDTO.class);
        if (atAll) {
            FeishuTableDTO.Element atAllElement = new FeishuTableDTO.Element();
            atAllElement.setTag("div");
            JSONObject atAllElementText = new JSONObject();
            atAllElementText.put("content", "<at id=all></at>");
            atAllElementText.put("tag", "lark_md");
            atAllElement.setText(atAllElementText);
            result.getElements().add(atAllElement);
        }
        return result;
    }

    public static Column createFeishuColumns(
            String dataType,
            String columnName,
            String displayName,
            String numberFormat) {

        String width = "auto";
        String verticalAlign = "top";
        String horizontalAlign = "left";
        Column column = new Column();

        if (FieshuTableColumnDataTypeEnum.TEXT.getType().equals(dataType)) {
            column.setName(columnName);
            column.setDisplay_name(displayName);
            column.setWidth(width);
            column.setData_type(dataType);
            column.setVertical_align(verticalAlign);
            column.setHorizontal_align(horizontalAlign);
        } else if (FieshuTableColumnDataTypeEnum.LARK_MD.getType().equals(dataType)) {
            column.setName(columnName);
            column.setDisplay_name(displayName);
            column.setData_type(dataType);
        } else if (FieshuTableColumnDataTypeEnum.NUMBER.getType().equals(dataType)) {
            column.setName(columnName);
            column.setDisplay_name(displayName);
            column.setData_type(dataType);
            column.setFormat(numberFormat);
            column.setWidth(width);
        } else if (FieshuTableColumnDataTypeEnum.DATE.getType().equals(dataType)) {
            column.setName(columnName);
            column.setDisplay_name(displayName);
            column.setData_type(dataType);
            column.setDateFormat("YYYY/MM/DD");
        } else if (FieshuTableColumnDataTypeEnum.OPTIONS.getType().equals(dataType)) {
            column.setName(columnName);
            column.setDisplay_name(displayName);
            column.setData_type(dataType);
        }

        return column;
    }
}

