package cn.com.qmth.examcloud.core.questions.base.json;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;

import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
import cn.com.qmth.examcloud.core.questions.base.CommonUtils;
import cn.com.qmth.examcloud.core.questions.base.enums.QuesUnit;
import freemarker.template.Configuration;
import freemarker.template.Template;

public class JsonImportUtil {

    private static final Logger log = LoggerFactory.getLogger(JsonImportUtil.class);

    private static final String ELEMENT_TYPE_TEXT = "text";

    private static final String ENCODING = "UTF-8";

    private static Configuration CONFIGURATION;

    private static Template QUESTION_HTML;
    static {

        CONFIGURATION = new Configuration(Configuration.VERSION_2_3_25);
        // 设置编码
        CONFIGURATION.setDefaultEncoding(ENCODING);
        // 设置ftl模板路径
        CONFIGURATION.setClassForTemplateLoading(JsonImportUtil.class, "/import_template/");

        try {
            QUESTION_HTML = CONFIGURATION.getTemplate("question_html.ftl", ENCODING);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    public static String questionJson2Html(List<SectionElement> es) {
        StringWriter result = null;
        try {
            result = new StringWriter();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("pList", es);
            QUESTION_HTML.process(map, result);
            return result.toString();
        } catch (Exception e) {
            throw new ExamCloudRuntimeException(e);
        }
    }

    /**
     * 获取段落的所有文本
     *
     * @param p
     * @return
     */
    public static String getPText(List<SectionElement> es) {
        String returnText = "";
        for (SectionElement obj : es) {
            returnText += obj.getValue().trim();
        }
        return returnText.trim();
    }

    /**
     * 校验段落中是否含有公式或图片
     *
     * @param p
     * @return
     */
    public static boolean isText(List<SectionElement> es) {
        for (SectionElement obj : es) {
            if (ELEMENT_TYPE_TEXT.equals(obj.getType())) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

    /**
     * 过滤试题单元标题
     *
     * @param p
     * @param quesUnit
     * @return
     */
    public static List<SectionElement> formatP(List<SectionElement> p, QuesUnit quesUnit) {
        for (SectionElement obj : p) {
            String tmpText = obj.getValue();
            if (quesUnit == QuesUnit.QUES_BODY) {

                // 过滤题干标题
                if (tmpText.matches("^\\d{1,}\\.[\\s\\S]*")) {
                    tmpText = tmpText.replaceFirst("\\d{1,}\\.", "");
                    obj.setValue(tmpText);
                    return p;
                } else if (tmpText.matches("^\\d{1,}$")) {
                    tmpText = tmpText.replaceFirst("\\d{1,}", "");
                    obj.setValue(tmpText);
                } else if (tmpText.contains(".")) {
                    tmpText = tmpText.replaceFirst("\\.", "");
                    obj.setValue(tmpText);
                    return p;
                }

            } else if (quesUnit == QuesUnit.QUES_OPTION) {

                // 过滤选项标题
                if (tmpText.matches("^[A-Z]\\.[\\s\\S]*")) {
                    tmpText = tmpText.replaceFirst("[A-Z]\\.", "");
                    obj.setValue(tmpText);
                    return p;
                } else if (tmpText.matches("^[A-Z]$")) {
                    tmpText = tmpText.replaceFirst("[A-Z]", "");
                    obj.setValue(tmpText);
                } else if (tmpText.contains(".")) {
                    tmpText = tmpText.replaceFirst("\\.", "");
                    obj.setValue(tmpText);
                    return p;
                }

            } else if (quesUnit == QuesUnit.QUES_ANSWER) {
                if (tmpText.contains("答案")) {
                    // 过滤答案标题
                    tmpText = tmpText.replaceFirst("\\[", "").replaceFirst("\\]", "").replaceFirst("答案", "")
                            .replaceFirst("[:|：]", "");
                    obj.setValue(tmpText);
                }
            }
        }
        return p;
    }

    public static String getJsonString(List<SectionElement> p) {
        Gson gson = new Gson();
        return gson.toJson(p);
    }

    public static int getOptionNum(String wordText) {
        if (wordText.matches("^[a-zA-Z]\\.[\\s\\S]*")) {
            Integer num = CommonUtils.characterToNumber(wordText.substring(0, 1));
            return num;
        }
        return -1;
    }
}
