/*
 * *************************************************
 * Copyright (c) 2018 QMTH. All Rights Reserved.
 * Created by Deason on 2018-07-13 13:37:43.
 * *************************************************
 */

package cn.com.qmth.examcloud.core.questions.service.converter;

import cn.com.qmth.examcloud.core.questions.base.CommonUtils;
import cn.com.qmth.examcloud.core.questions.base.converter.model.platform.*;
import cn.com.qmth.examcloud.core.questions.dao.entity.QuesOption;
import cn.com.qmth.examcloud.core.questions.dao.entity.Question;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ConvertQuestion {

    public static BodyVo parseBody(String bodyStr) {
        BodyVo body = new BodyVo();
        if (StringUtils.isBlank(bodyStr)) {
            return body;
        }
        List<SectionVo> sections = splitSections(bodyStr);
        body.setSections(sections);
        return body;
    }

    public static AnswerVo parseAnswer(String answerStr) {
        AnswerVo answer = new AnswerVo();
        if (StringUtils.isBlank(answerStr)) {
            return answer;
        }
        List<SectionVo> sections = splitSections(answerStr);
        answer.setSections(sections);
        return answer;
    }

    public static List<OptionVo> parseOption(Question question) {
        List<OptionVo> options = new ArrayList<>();
        List<QuesOption> oldOptions = question.getQuesOptions();
        if (oldOptions == null || oldOptions.size() == 0) {
            return options;
        }
        for (QuesOption oldOption : oldOptions) {
            OptionVo option = new OptionVo();
            option.setNumber(Integer.valueOf(oldOption.getNumber()));
            option.setCorrect(oldOption.getIsCorrect() == 1 ? true : false);
            BodyVo body = new BodyVo();
            List<SectionVo> sections = splitSections(oldOption.getOptionBody());
            body.setSections(sections);
            option.setBody(body);
            options.add(option);
        }
        return options;
    }

    private static List<SectionVo> splitSections(String str) {
        List<SectionVo> sections = new ArrayList<>();
        String[] rows = str.split("</p>");
        for (String row : rows) {
            List<BlockVo> blocks = splitBlocks(row);
            if (blocks == null || blocks.size() == 0) {
                continue;
            }
            SectionVo section = new SectionVo();
            section.setBlocks(blocks);
            sections.add(section);
        }
        return sections;
    }

    public static List<BlockVo> splitBlocks(String row) {
        //去掉每行里面的<p>,<span>,</span>标签
        row = row.replaceAll("<p>", "").replaceAll("</p>", "")
                .replaceAll("<span>", "").replaceAll("</span>", "")
                .replaceAll("</a>", "");
        List<BlockVo> blocks = new ArrayList<>();
        String[] strList = row.split("<|/>|>");
        for (String str : strList) {
            BlockVo block = new BlockVo();
            if (str.startsWith("img")) {
                //处理图片
                block.setType("image");
                str = "<" + str + ">";
                ParamVo param = new ParamVo();
                //获取图片的路径
                List<String> srcList = splitImgInfo(str, "src");
                if (srcList.size() > 0) {
                    String src = srcList.get(0).replaceAll("src=\"", "").replaceAll("\"", "");
                    block.setValue(src);
                }
                //获取图片的宽度
                List<String> widthList = splitImgInfo(str, "width");
                if (widthList.size() > 0) {
                    String width = widthList.get(0).replaceAll("width=\"", "").replaceAll("\"", "");
                    param.setWidth(width);
                }
                //获取图片的高度
                List<String> heightList = splitImgInfo(str, "height");
                if (heightList.size() > 0) {
                    String height = heightList.get(0).replaceAll("height=\"", "").replaceAll("\"", "");
                    param.setHeight(height);
                }
                block.setParam(param);
                blocks.add(block);
            } else if (str.startsWith("a") && str.contains("id") && str.contains("name")) {
                //处理音频
                block.setType("audio");
                block.setPlayTime(1);
                str = "<" + str + ">";
                block.setValue(CommonUtils.getAttrValue(str, "id"));
                blocks.add(block);
            } else {
                block.setType("text");
                str = str.replace("&nbsp;", "");//消除空格
                str = str.replace("&quot;", "\"");//将&quot;转换成\"
                str = str.replace("&lt;", "<");//将&lt;转换成<
                str = str.replace("&gt;", ">");//将&gt;转换成>
                str = str.replace("&amp;", "&");//将&amp;转换成&
                if (StringUtils.isNotBlank(str)) {
                    block.setValue(str);
                    blocks.add(block);
                }
            }
        }
        return blocks;
    }

    public static List<String> splitImgInfo(String str, String attr) {
        String regex = attr + "=\"(.*?)\"";
        Pattern p = Pattern.compile(regex, Pattern.DOTALL);
        Matcher m = p.matcher(str);
        List<String> list = new ArrayList<>();
        while (m.find()) {
            list.add(m.group());
        }
        return list;
    }

}