package com.secspace.lib.common.compat;

import android.os.Build;
import android.text.TextUtils;

/**
 * @author Lody
 */

public class BuildCompat {

    public static int getPreviewSDKInt() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                return Build.VERSION.PREVIEW_SDK_INT;
            } catch (Throwable e) {
                // ignore
            }
        }
        return 0;
    }

    public static boolean isOreo() {
        return Build.VERSION.SDK_INT > 25 || (Build.VERSION.SDK_INT == 25 && getPreviewSDKInt() > 0);
    }

    public static boolean isPie() {
        return Build.VERSION.SDK_INT > 27 || (Build.VERSION.SDK_INT == 27 && getPreviewSDKInt() > 0);
    }

    public static boolean isSamsung() {
        return "samsung".equalsIgnoreCase(Build.BRAND) || "samsung".equalsIgnoreCase(Build.MANUFACTURER);
    }

    public static boolean isEMUI() {
        if (Build.DISPLAY.toUpperCase().startsWith("EMUI")) {
            return true;
        }
        String property = SystemPropertiesCompat.get("ro.build.version.emui");
        return property != null && property.contains("EmotionUI");
    }

    public static boolean isMIUI() {
        return SystemPropertiesCompat.getInt("ro.miui.ui.version.code", 0) > 0;
    }

    public static boolean isFlyme() {
        return Build.DISPLAY.toLowerCase().contains("flyme");
    }

    public static boolean isOppo() {
        return SystemPropertiesCompat.get("ro.build.version.opporom") != null
                || SystemPropertiesCompat.get("ro.rom.different.version") != null;
    }

    public static boolean is360UI() {
        String property = SystemPropertiesCompat.get("ro.build.uiversion");
        return property != null && property.toUpperCase().contains("360UI");
    }

    public static boolean isLetv() {
        return Build.MANUFACTURER.equalsIgnoreCase("Letv");
    }

    public static boolean isVivo() {
        return SystemPropertiesCompat.get("ro.vivo.os.build.display.id") != null;
    }


    private static ROMType sRomType;

    public static ROMType getROMType() {
        if (sRomType == null) {
            if (isEMUI()) {
                sRomType = ROMType.EMUI;
            } else if (isMIUI()) {
                sRomType = ROMType.MIUI;
            } else if (isFlyme()) {
                sRomType = ROMType.FLYME;
            } else if (isOppo()) {
                sRomType = ROMType.COLOR_OS;
            } else if (is360UI()) {
                sRomType = ROMType._360;
            } else if (isLetv()) {
                sRomType = ROMType.LETV;
            } else if (isVivo()) {
                sRomType = ROMType.VIVO;
            } else if (isSamsung()) {
                sRomType = ROMType.SAMSUNG;
            } else {
                sRomType = ROMType.OTHER;
            }
        }
        return sRomType;
    }

    public enum ROMType {
        EMUI,
        MIUI,
        FLYME,
        COLOR_OS,
        LETV,
        VIVO,
        _360,
        SAMSUNG,

        OTHER
    }

    /**
     * EMUI大于等于5.1
     * EmotionUI_5.1.1
     * EmotionUI_8.0.0
     *
     * @return
     */
    public static boolean emuiOver5_1() {
        String property = SystemPropertiesCompat.get("ro.build.version.emui");
        if (TextUtils.isEmpty(property)) {
            return false;
        }
        String str = property.replace("EmotionUI_", "").replace(".", "");
        int version = Integer.parseInt(str);
        if (version >= 510) {
            return true;
        }
        return false;
    }

    /**
     * EMUI大于等于8.0.0
     *
     * @return
     */
    public static boolean emuiOver8_0_0() {
        String property = SystemPropertiesCompat.get("ro.build.version.emui");
        if (TextUtils.isEmpty(property)) {
            return false;
        }
        String str = property.replace("EmotionUI_", "").replace(".", "");
        int version = Integer.parseInt(str);
        if (version >= 800) {
            return true;
        }
        return false;
    }

    /**
     * EMUI大于等于8.2.0
     *
     * @return
     */
    public static boolean emuiOver8_2_0() {
        String property = SystemPropertiesCompat.get("ro.build.version.emui");
        if (TextUtils.isEmpty(property)) {
            return false;
        }
        String str = property.replace("EmotionUI_", "").replace(".", "");
        int version = Integer.parseInt(str);
        if (version >= 820) {
            return true;
        }
        return false;
    }

    /**
     * EMUI大于等于9.0.0
     *
     * @return
     */
    public static boolean emuiOver9_0_0() {
        String property = SystemPropertiesCompat.get("ro.build.version.emui");
        if (TextUtils.isEmpty(property)) {
            return false;
        }
        String str = property.replace("EmotionUI_", "").replace(".", "");
        int version = Integer.parseInt(str);
        if (version >= 900) {
            return true;
        }
        return false;
    }

    public static boolean emuiOver9_1_0() {
        String property = SystemPropertiesCompat.get("ro.build.version.emui");
        if (TextUtils.isEmpty(property)) {
            return false;
        }
        String str = property.replace("EmotionUI_", "").replace(".", "");
        int version = Integer.parseInt(str);
        if (version >= 910) {
            return true;
        }
        return false;
    }

    /**
     * EMUI大于等于10.0.0
     *
     * @return
     */
    public static boolean emuiOver10_0_0() {
        String property = SystemPropertiesCompat.get("ro.build.version.emui");
        if (TextUtils.isEmpty(property)) {
            return false;
        }
        String str = property.replace("EmotionUI_", "").replace(".", "");
        int version = Integer.parseInt(str);
        if (version >= 1000) {
            return true;
        }
        return false;
    }

}