package com.neusoft.smart.pos.framework.utils;


import com.neusoft.smart.pos.framework.constants.CommonCode;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import org.springframework.util.StringUtils;

public class ConditionUtils {


    public static boolean vaildBoolean(String bool) {
        return (!StringUtils.isEmpty(bool) && ("false".equalsIgnoreCase(bool) || "true".equalsIgnoreCase(bool)));
    }

    public static boolean vaildInteger(String integer) {
        try {
            Integer.valueOf(integer);
            return true;
        } catch (Exception e) {
        }
        return false;
    }

    public static void vaildInteger(String integer, CommonCode exception) {
        if(!StringUtils.isEmpty(integer) && !vaildInteger(integer)) {
            System.out.println(exception.getMessage());
            throw new BusinessCommonException(exception);
        }
    }

    public static void emptyAllStrings(CommonCode exception, String... val) {
        for (String v: val) {
            if(!StringUtils.isEmpty(v)) {
                return;
            }
        }
        throw new BusinessCommonException(exception);
    }

    public static void emptyAllStrings(SetAction rejected, String... val) {
        for (String v: val) {
            if(!StringUtils.isEmpty(v)) {
                return;
            }
        }
        rejected.doAction(null);
    }

    public static void emptyOneStrings(CommonCode exception, String... val) {
        for (String v: val) {
            if(StringUtils.isEmpty(v)) {
                throw new BusinessCommonException(exception);
            }
        }
    }

    public static <T> void setWithCondition(T val, SetCondition<T> condition, SetAction<T> resolved) {
        setWithCondition(val, condition, resolved, null);
    }

    public static <T> void setWithCondition(T val, SetCondition<T> condition, SetAction<T> resolved, SetAction<T> rejected) {
        if(condition.doCondition(val)) {
            resolved.doAction(val);
        } else {
            if(rejected != null) {
                rejected.doAction(val);
            }
        }
    }

    public interface SetAction<T> {
        void doAction(T val);
    }

    public interface SetCondition<T> {
        boolean doCondition(T val);
    }
}
