package com.xydl.cac.comparator;


import com.xydl.cac.exception.BusinessException;

import java.util.Date;
import java.util.LinkedHashMap;

public class FloatCompare extends Comparator {

    private float IgnoreValueN = -0.00001f;
    private float IgnoreValue = 0.00001f;

    @Override
    public boolean compare(Object source, String threshold, Date lastDTime) {
        if (source != null) {
            float s1;
            if (source instanceof Number) {
                s1 = (float) source;
            } else {
                s1 = Float.valueOf(source.toString());
            }

            if ("BTW".equalsIgnoreCase(operator)) {
                String[] strs = threshold.split(",");
                float t1 = Float.parseFloat(strs[0]);
                float t3 = Float.parseFloat(strs[1]);
                if (t1 <= s1 && s1 <= t3) {
                    return true;
                }
            } else if ("NTW".equalsIgnoreCase(operator)) {
                String[] strs = threshold.split(",");
                float t1 = Float.parseFloat(strs[0]);
                float t3 = Float.parseFloat(strs[1]);
                if (t1 > s1 || s1 > t3) {
                    return true;
                }
            } else {
                float t2 = Float.parseFloat(threshold);
                float dif = s1 - t2;

                if ("EQU".equalsIgnoreCase(operator)) {
                    if (IgnoreValueN < dif && dif < IgnoreValue) {
                        return true;
                    }
                } else if ("NEQ".equalsIgnoreCase(operator)) {
                    if (IgnoreValueN > dif || dif > IgnoreValue) {
                        return true;
                    }
                } else if ("LSS".equalsIgnoreCase(operator)) {
                    if (IgnoreValueN > dif) {
                        return true;
                    }
                } else if ("LEQ".equalsIgnoreCase(operator)) {
                    if (IgnoreValue > dif) {
                        return true;
                    }
                } else if ("GTR".equalsIgnoreCase(operator)) {
                    if (dif > IgnoreValue) {
                        return true;
                    }
                } else if ("GEQ".equalsIgnoreCase(operator)) {
                    if (dif > IgnoreValueN) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public LinkedHashMap<String, String> supportedOperator() {
        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        map.put("GTR", "大于");
        map.put("GEQ", "大等于");
        map.put("LSS", "小于");
        map.put("LEQ", "小等于");
        map.put("BTW", "介于A-B之间");
        map.put("NTW", "在A-B之外");
        return map;
    }

    @Override
    public void valid(String operator, String threshold) throws Exception {
        if ("BTW".equalsIgnoreCase(operator)) {
            if (!threshold.contains(",")) {
                throw new BusinessException("阈值不正确");
            }
        } else if ("NTW".equalsIgnoreCase(operator)) {
            if (!threshold.contains(",")) {
                throw new BusinessException("阈值不正确");
            }
        }
    }

}
