package com.eversec.kit.creator.impl.page;


import com.eversec.common.Kv;

import java.io.File;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.asList;

/**
 * @author: liangxianyou at 2018/8/6 16:34.
 */
public class CtrlKit extends PageKit {
    @Override
    public File getFile(String clazz) {
        return new File(filePath(clazz) + "/listCtrl.js");
    }

    @Override
    public String createStr(String clazz, List<Map> fieldList, Map para) {
        Kv kv = createTplData(clazz);
        kv.set("remark", para.get("remark"));
        kv.set("fileDir", getFileDir(clazz));
        kv.set("fieldList", fieldList);
        kv.set("conditionLabel", getConditionLabel(fieldList, para));
        kv = createFiledLabel(fieldList, kv);

        return engine.getTemplate(tplPath + "front/CtrlTpl.js").renderToString(kv);
    }

    private static List<String> noShow = asList("id","tokenID","truCode","insertUserId","insertTime"
            ,"updateUserName","updateUserId","updateTime","eventdate","status");
    private boolean showColumn(Map map){
        return "1".equalsIgnoreCase(String.valueOf(map.get("column"))) || !noShow.contains(map.get("field")+"");
    }

    protected Kv createFiledLabel(List<Map> fieldList, Kv res){
        if (res == null) res = Kv.of();
        StringBuilder labelBuf = new StringBuilder();
        StringBuilder fieldBuf = new StringBuilder();
        fieldList.forEach(x->{
            if (showColumn(x)){
                labelBuf.append("\""+ x.get("remark1") +"\",");

                if ("2".equals(String.valueOf(x.get("edit")))){
                    fieldBuf.append("\""+ x.get("field") +"||"+ x.get("selects") +"\",");
                }else if ("SELECT".equalsIgnoreCase(String.valueOf(x.get("tag")))){
                    fieldBuf.append("\""+ x.get("field") + "="+ x.get("field") +"\",");
                }else if ("SELECT_EXT".equalsIgnoreCase(String.valueOf(x.get("tag")))){
                    fieldBuf.append("\""+ x.get("field") +"|"+ x.get("selects")  +"\",");
                }else if ("INPUT_DT".equalsIgnoreCase(String.valueOf(x.get("tag")))){
                    fieldBuf.append("\""+ x.get("field") +"=dt\",");
                }

                else if ("FILE_EXT".equalsIgnoreCase(String.valueOf(x.get("tag")))){
                    fieldBuf.append("\""+ x.get("selects") +"\",");
                }else {
                    fieldBuf.append("\""+ x.get("field") +"\",");
                }
            }
        });

        if (labelBuf.length() > 0){
            labelBuf.deleteCharAt(labelBuf.length()-1);
        }
        if (fieldBuf.length() > 0){
            fieldBuf.deleteCharAt(fieldBuf.length()-1);
        }
        res.set("label", labelBuf.toString());
        res.set("field", fieldBuf.toString());
        return res;
    }

    //获取设置了关键字查询的 字段“描述”
    private String getConditionLabel(List<Map> fieldList, Map para) {
        StringBuilder buf = new StringBuilder();
        if (para.get("condition") != null){
            List<String> conditions = asList((String[]) para.get("condition"));
            fieldList.forEach(m->{
                for (String str : conditions) {
                    //字段有大小写转换，这里需要使用 equalsIgnoreCase 进行比较
                    if ((m.get("field")+"").trim().equalsIgnoreCase(str)) {
                        buf.append(m.get("remark1")+"/");
                    }
                }
            });
        }
        if (buf.length() > 0){
            buf.deleteCharAt(buf.length()-1);
        }
        return buf.toString();
    }
}
