package org.diagbot.nlp.rule.pretreat;

import org.diagbot.nlp.participle.ParticipleUtil;
import org.diagbot.nlp.participle.word.Lexeme;
import org.diagbot.nlp.participle.word.LexemePath;
import org.diagbot.nlp.rule.module.PreResult;
import org.diagbot.nlp.util.NegativeEnum;
import org.diagbot.nlp.util.NlpUtil;
import org.springframework.util.StringUtils;

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

/**
 * Created by louhr on 2019/8/31.
 */
public abstract class Pretreatment {
    protected NegativeEnum[] nees_time_and_unit = new NegativeEnum[]{NegativeEnum.EVENT_TIME, NegativeEnum.UNIT};

    protected int cursor = 0;

    protected int max_back_search = 3;

    public abstract List<PreResult> analyze(String content) throws java.io.IOException;

    abstract PreResult createPreResult(LexemePath<Lexeme> lexemes, Lexeme lexeme, int index);

    abstract String findBodyValue(LexemePath<Lexeme> lexemes, Lexeme lexeme, int index);

    protected List<PreResult> analyzeDefault(String content) throws java.io.IOException{
        LexemePath<Lexeme> lexemes = ParticipleUtil.participle(content);

        List<PreResult> preResultList = new ArrayList<>();
        for (int i = 0; i < lexemes.size(); i++) {
            Lexeme l = lexemes.get(i);
            if (NlpUtil.isFeature(l.getProperty(), nees_time_and_unit)) {
                PreResult result = data2Object(lexemes, l, i, l.getProperty());
                if (result != null) {
                    preResultList.add(result);
                }
            }
        }
        return preResultList;
    }

    protected PreResult data2Object(LexemePath<Lexeme> lexemes, Lexeme lexeme, int index, String property) {
        if (index < 2) {
            return null;
        }
        return createPreResult(lexemes, lexeme, index);
    }

    public PreResult createDefaultPreResult(LexemePath<Lexeme> lexemes, Lexeme lexeme, int index) {
        double value = findNumberValue(lexemes, lexeme, index);
        if (value == -1) return null;
        //继续往前找本体
        String text = findBodyValue(lexemes, lexeme, index);
        if (StringUtils.isEmpty(text)) {
            return null;
        }
        PreResult result = new PreResult();
        result.setValue(String.valueOf(value));
        result.setUnits(lexeme.getText());
        result.setDetailName(text);
        result.setUniqueName(text);
        return result;
    }

    protected double findNumberValue(LexemePath<Lexeme> lexemes, Lexeme lexeme, int index) {
        if (index < 1) return -1;
        cursor = index - 1;
        Lexeme leftLexeme = lexemes.get(cursor);
        if ("×".equals(leftLexeme.getText())) {
            if  (cursor <= 0) return -1;
            cursor--;
            leftLexeme = lexemes.get(cursor);
        }
        if (NlpUtil.isNumberString(leftLexeme)) {
            return NlpUtil.numberText2value(leftLexeme);
        }
        return -1;
    }


}
