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.List;

public class PretreatmentPacs extends Pretreatment {
    protected NegativeEnum[] nees_pacs_result = new NegativeEnum[] { NegativeEnum.PACS_RESULT };
    protected NegativeEnum[] nees_pacs_name = new NegativeEnum[] { NegativeEnum.PACS_NAME };
    private String join_symbols = ";:；：";

    public List<PreResult> analyze(String content) throws java.io.IOException {
        List<PreResult> preResultList = super.analyzeDefault(content);
        //pacs除了数值型需要转， 还需要对部分检查结果提取，以便做危机警示
        LexemePath<Lexeme> lexemes = ParticipleUtil.participle(content);

        Lexeme leftLexeme;
        for (int i = 0; i < lexemes.size(); i++) {
            Lexeme l = lexemes.get(i);
            if (NlpUtil.isFeature(l.getProperty(), nees_pacs_result) && i > 0) {
                int c = i - 1;
                while (c > -1) {
                    leftLexeme = lexemes.get(c);
                    if (NlpUtil.isFeature(leftLexeme.getProperty(), nees_pacs_name)) {
                        PreResult result = new PreResult();
                        result.setValue(NlpUtil.concept(l, NegativeEnum.PACS_RESULT));
                        result.setDetailName(NlpUtil.concept(leftLexeme, NegativeEnum.PACS_NAME));
                        result.setUniqueName(NlpUtil.concept(leftLexeme, NegativeEnum.PACS_NAME));
                        preResultList.add(result);
                        break;
                    }
                    c--;
                }
            } else if (NlpUtil.isFeature(l.getProperty(), nees_time_and_unit) && i > 0) {
                PreResult result = data2Object(lexemes, l, i, l.getProperty());
                if (result != null) {
                    preResultList.add(result);
                }
            }
        }
        return preResultList;
    }

    public PreResult createPreResult(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());
        return getPreResultPub(lexemes, result);
    }

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

    public PreResult getPreResultPub(LexemePath<Lexeme> lexemes, PreResult result) {
        //继续往前找辅检明细项
        if (cursor > 0) {
            cursor--;
        }
        Lexeme leftLexeme = lexemes.get(cursor);
        if (join_symbols.contains(leftLexeme.getText())) {
            if (cursor > 0) {
                cursor--;
                leftLexeme = lexemes.get(cursor);
            } else {
                return null;
            }
        }
        if (NlpUtil.isFeature(leftLexeme.getProperty(), new NegativeEnum[] { NegativeEnum.PACS_NAME })) {
            result.setDetailName(NlpUtil.concept(leftLexeme, NegativeEnum.PACS_NAME));
        } else if (NlpUtil.isFeature(leftLexeme.getProperty(), new NegativeEnum[] { NegativeEnum.PACS_NAME })) {
            result.setUniqueName(NlpUtil.concept(leftLexeme, NegativeEnum.PACS_NAME));
        } else {
            return null;
        }
        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 (isNumberString(leftLexeme)) {
            String[] numbersSplit = leftLexeme.getText().split("\\*");
            try {
                if (numbersSplit.length == 2) {
                    return Double.valueOf(numbersSplit[0]) * Double.valueOf(numbersSplit[1]);
                } else if (numbersSplit.length == 3) {
                    return Double.valueOf(numbersSplit[0]) * Double.valueOf(numbersSplit[1])
                            * Double.valueOf(numbersSplit[2]);
                } else {
                    return -1;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return -1;
    }

    public static boolean isNumberString(Lexeme l) {
        if (l == null) {
            return false;
        }
        if (NlpUtil.isFeature(l.getProperty(), new NegativeEnum[] { NegativeEnum.DIGITS })
                && l.getText().indexOf("*") != -1) {
            return true;
        }
        return false;
    }
}
