package org.diagbot.nlp.controller;

import org.diagbot.nlp.dao.model.TaggingResult;
import org.diagbot.nlp.feature.FeatureType;
import org.diagbot.nlp.relation.analyze.RelationAnalyze;
import org.diagbot.nlp.relation.analyze.StructureAnalyze;
import org.diagbot.nlp.relation.util.OutputInfo;
import org.diagbot.pub.api.Response;
import org.diagbot.pub.jdbc.MysqlJdbc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName org.diagbot.nlp.controller.RelationExtractionController
 * @Description TODO
 * @Author fyeman
 * @Date 2019/1/17/017 11:21
 * @Version 1.0
 **/
@Controller
@RequestMapping("/relation")
public class RelationExtractionController {

    @RequestMapping({"/extraction"})
    @ResponseBody
    public Response extraction(String content) throws Exception {
        Response response = new Response();
        response.start();
//        RelationAnalyze relationAnalyze = new RelationAnalyze();
//        List<OutputInfo> outputInfos = relationAnalyze.analyze(content, FeatureType.parse("1"));
        StructureAnalyze structureAnalyze = new StructureAnalyze();
        List<OutputInfo> outputInfos = structureAnalyze.extract(content);
        response.setData(outputInfos);
        return response;
    }

    @RequestMapping({"/list"})
    @ResponseBody
    public Response<List<TaggingResult>> list(int pageNum) throws Exception {
        Response<List<TaggingResult>> response = new Response();
        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/nlp-web?useUnicode=true&characterEncoding=UTF-8");
        List<Map<String, String>> data = nlpJdbc.query("re_tagging_result_part",
                new String[]{"sentence_id", "sentence", "entity_1_name", "entity_2_name", "relation", "entity_1_position", "entity_2_position"},
                " where relation = 1 order by sentence_id, sentence limit " + pageNum * 100 + ", 100");

        String sentence = "";
        String s = "";
        String[] position1 = null;
        String[] position2 = null;
        List<TaggingResult> taggingResults = new ArrayList<>();
        for (Map<String, String> map : data) {
            TaggingResult tr = new TaggingResult();
            tr.setSentenceId(map.get("sentence_id"));
            tr.setEntity1Name(map.get("entity_1_name"));
            tr.setEntity2Name(map.get("entity_2_name"));
            tr.setEntity1Position(map.get("entity_1_position"));
            tr.setEntity2Position(map.get("entity_2_position"));
            tr.setRelation(map.get("relation"));

            sentence = map.get("sentence");
            position1 = tr.getEntity1Position().split(",");
            position2 = tr.getEntity2Position().split(",");
            if ("0".equals(position1[0])) {
                s = "<span class=\"label label-success\">";
            } else {
                s = sentence.substring(0, Integer.parseInt(position1[0]))
                        + "<span class=\"label label-success\">";
            }
            s = s + sentence.substring(Integer.parseInt(position1[0]), Integer.parseInt(position1[1]) + 1) + "</span>";
            if (position1[1].equals(position2[0])) {
                if (Integer.parseInt(position2[1]) == sentence.length() - 1) {
                    s = s + "<span class=\"label label-success\">" + sentence.substring(Integer.parseInt(position2[0])) + "</span>";
                } else {
                    s = s + "<span class=\"label label-success\">" + sentence.substring(Integer.parseInt(position2[0]), Integer.parseInt(position2[1]) + 1) + "</span>";
                }
            } else {
                s = s + sentence.substring(Integer.parseInt(position1[1]) + 1, Integer.parseInt(position2[0]));

                if (Integer.parseInt(position2[1]) == sentence.length() - 1) {
                    s = s + "<span class=\"label label-success\">" + sentence.substring(Integer.parseInt(position2[0])) + "</span>";
                } else {
                    s = s + "<span class=\"label label-success\">" + sentence.substring(Integer.parseInt(position2[0]), Integer.parseInt(position2[1]) + 1) + "</span>";
                    s = s + sentence.substring(Integer.parseInt(position2[1]) + 1);
                }
            }

            tr.setSentence(s);
            taggingResults.add(tr);
        }

        response.setData(taggingResults);
        return response;
    }
}
