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 java.util.ArrayList;
import java.util.List;

public class PretreatmentLis extends Pretreatment {
    private String join_symbols = ";:；：";
    protected NegativeEnum[] lis_result = new NegativeEnum[]{NegativeEnum.LIS_RESULT};
    protected NegativeEnum[] lis_name = new NegativeEnum[]{NegativeEnum.LIS_NAME};
    protected NegativeEnum[] lis_type = new NegativeEnum[]{NegativeEnum.LIS_TYPE};

    public List<PreResult> analyze(String content) throws java.io.IOException {
        List<PreResult> preResults = new ArrayList<>();
        LexemePath<Lexeme> lexemes = ParticipleUtil.participle(content);
        for (int i = 0; i < lexemes.size(); i++) {
            Lexeme l = lexemes.get(i);
            if (NlpUtil.isFeature(l.getProperty(), lis_name)) {
                cursor = i;
                PreResult result = new PreResult();
                result.setDetailName(NlpUtil.concept(l, NegativeEnum.LIS_NAME));
                result = createPreResult(lexemes, result, i);
                if (result != null) {
                    preResults.add(result);
                }
            }
        }
        return preResults;
    }

    public PreResult createPreResult(LexemePath<Lexeme> lexemes, Lexeme lexeme, int index) {
        return null;
    }

    public PreResult createPreResult(LexemePath<Lexeme> lexemes, PreResult result, int index) {
        Lexeme leftLexeme;
        //往前查找化验套餐
        int position = index - 1;
        while (position > -1) {
            leftLexeme = lexemes.get(position);
            if (NlpUtil.isFeature(leftLexeme.getProperty(), lis_type)) {
                result.setName(NlpUtil.concept(leftLexeme, NegativeEnum.LIS_TYPE));
                break;
            }
            position--;
        }
        //往后查找化验结果
        int max_find_step = 10;
        if (index ==  lexemes.size() - 1) return null;
        position = index++;
        Lexeme rightLexeme;
        while (position < (index + max_find_step) &&  position < lexemes.size()) {
            rightLexeme = lexemes.get(position);
            //遇上数字查找是否有单位
            if (NlpUtil.isNumberString(rightLexeme)) {
                result.setValue(rightLexeme.getText());
                //继续找单位
                int offset = 1;
                while (offset < max_find_step && position + offset < lexemes.size()) {
                    rightLexeme = lexemes.get(position + offset);
                    if (NlpUtil.isFeature(rightLexeme.getProperty(), nees_time_and_unit)) {
                        result.setUnits(rightLexeme.getText());
                        break;
                    }
                    offset++;
                }
                break;
            } else if (NlpUtil.isFeature(rightLexeme.getProperty(), lis_result)) {  //化验结果
                result.setValue(rightLexeme.getText());
                break;
            }
            position++;
        }
        return result;
    }

    public String findBodyValue(LexemePath<Lexeme> lexemes, Lexeme lexeme, int index) {
        return null;
    }
}
