package com.ruoyi.common.enums;

import com.ruoyi.common.utils.StringUtils;

//("front:".equalsIgnoreCase(prefix)||"frontApp:".equalsIgnoreCase(prefix)||"h5:".equalsIgnoreCase(prefix)||"wechat:".equalsIgnoreCase(prefix)){
public enum AccessFromType {
    back(0, "WEB后端"),
    front(1, "WEB前端"),
    frontApp(2, "前端APP"),
    h5(3, "h5页面"),
    wechat(4, "微信公众号");

    private Integer value;
    private String remark;

    private AccessFromType(Integer value, String remark) {
        this.value = value;
        this.remark = remark;
    }

    public Integer getValue() {
        return value;
    }

    public String getRemark() {
        return remark;
    }

    public static AccessFromType getByValue(String value) {
        for (AccessFromType o : AccessFromType.values()) {
            if (o.getValue().equals(value)) {
                return o;
            }
        }
        return null;
    }


    public static Boolean isBack(Object value) {
        if(StringUtils.isNull(value)){
            return false;
        }

        if(value instanceof String){
            if (back.name().equalsIgnoreCase(String.valueOf(value).trim())) {
                return true;
            }
            if ((back.name()+":").equalsIgnoreCase(String.valueOf(value).trim())) {
                return true;
            }
        }

        if(value instanceof Long){
            if (back.getValue().intValue()==Integer.valueOf(String.valueOf(value).trim()).intValue()) {
                return true;
            }
        }

        if(value instanceof Integer){
            if (back.getValue().intValue()==Integer.valueOf(String.valueOf(value).trim()).intValue()) {
                return true;
            }
        }

        return false;
    }

    public static Boolean isFront(Object value) {
        if(StringUtils.isNull(value)){
            return false;
        }
        if(value instanceof String){
            if (front.name().equalsIgnoreCase(String.valueOf(value).trim())) {
                return true;
            }
            if ((front.name()+":").equalsIgnoreCase(String.valueOf(value).trim())) {
                return true;
            }
        }

        if(value instanceof Long){
            if (front.getValue().intValue()==Integer.valueOf(String.valueOf(value).trim()).intValue()) {
                return true;
            }
        }

        if(value instanceof Integer){
            if (front.getValue().intValue()==Integer.valueOf(String.valueOf(value).trim()).intValue()) {
                return true;
            }
        }

        return false;
    }

    public static Boolean isFrontApp(Object value) {
        if(StringUtils.isNull(value)){
            return false;
        }
        if(value instanceof String){
            if (frontApp.name().equalsIgnoreCase(String.valueOf(value).trim())) {
                return true;
            }
            if ((frontApp.name()+":").equalsIgnoreCase(String.valueOf(value).trim())) {
                return true;
            }
        }

        if(value instanceof Long){
            if (frontApp.getValue().intValue()==Integer.valueOf(String.valueOf(value).trim()).intValue()) {
                return true;
            }
        }

        if(value instanceof Integer){
            if (frontApp.getValue().intValue()==Integer.valueOf(String.valueOf(value).trim()).intValue()) {
                return true;
            }
        }

        return false;
    }

    public static Boolean isH5(Object value) {
        if(StringUtils.isNull(value)){
            return false;
        }
        if(value instanceof String){
            if (h5.name().equalsIgnoreCase(String.valueOf(value).trim())) {
                return true;
            }
            if ((h5.name()+":").equalsIgnoreCase(String.valueOf(value).trim())) {
                return true;
            }
        }

        if(value instanceof Long){
            if (h5.getValue().intValue()==Integer.valueOf(String.valueOf(value).trim())) {
                return true;
            }
        }

        if(value instanceof Integer){
            if (h5.getValue().intValue()==Integer.valueOf(String.valueOf(value).trim()).intValue()) {
                return true;
            }
        }

        return false;
    }

    public static Boolean isWechat(Object value) {
        if(StringUtils.isNull(value)){
            return false;
        }
        if(value instanceof String){
            if (wechat.name().equalsIgnoreCase(String.valueOf(value).trim())) {
                return true;
            }
            if ((wechat.name()+":").equalsIgnoreCase(String.valueOf(value).trim())) {
                return true;
            }
        }

        if(value instanceof Long){
            if (wechat.getValue().intValue()==Integer.valueOf(String.valueOf(value).trim()).intValue()) {
                return true;
            }
        }

        if(value instanceof Integer){
            if (wechat.getValue().intValue()==Integer.valueOf(String.valueOf(value).trim()).intValue()) {
                return true;
            }
        }

        return false;
    }



    public static Boolean startsWithBack(String value) {
        if(StringUtils.isNull(value)){
            return false;
        }
        if (value.trim().toLowerCase().startsWith(back.name().toLowerCase()+":")) {
            return true;
        }

        return false;
    }

    public static Boolean startsWithFront(String value) {
        if(StringUtils.isNull(value)){
            return false;
        }
        if (value.trim().toLowerCase().startsWith(front.name().toLowerCase()+":")) {
            return true;
        }

        return false;
    }

    public static Boolean startsWithFrontApp(String value) {
        if(StringUtils.isNull(value)){
            return false;
        }
        if (value.trim().toLowerCase().startsWith(frontApp.name().toLowerCase()+":")) {
            return true;
        }

        return false;
    }

    public static Boolean startsWithH5(String value) {
        if(StringUtils.isNull(value)){
            return false;
        }
        if (value.trim().toLowerCase().startsWith(h5.name().toLowerCase()+":")) {
            return true;
        }

        return false;
    }

    public static Boolean startsWithWechat(String value) {
        if(StringUtils.isNull(value)){
            return false;
        }
        if (value.trim().toLowerCase().startsWith(wechat.name().toLowerCase()+":")) {
            return true;
        }

        return false;
    }

    public static Boolean isFrontAccess(Object value) {
        if(StringUtils.isNull(value)){
            return false;
        }
        if(isFront(value)||isFrontApp(value)||isH5(value)){
            return true;
        }

        return false;
    }

    public static void main(String[] args) {
//        System.out.println(hasLoginFrontPermission("backBusiness"));
//        System.out.println(hasLoginFrontPermission("frontStudent"));
        System.out.println(isBack("back"));
        System.out.println(isBack("back:"));
        System.out.println(isBack(0L));
        System.out.println(isBack(0));
        System.out.println(startsWithWechat("wechat:GS00000008"));
        System.out.println(startsWithFront("front:GS00000008"));
        System.out.println(startsWithFrontApp("frontApp:GS00000008"));
    }
}
