package com.miya.print.utils;

import android.os.Build;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * cpu相关工具类
 *
 * @author pupu
 * @time 2018/10/24
 */
public class CpuUtils {

    private CpuUtils() {
        throw new UnsupportedOperationException("不允许外部创建实例");
    }

    /**
     * 获取cpu支持的架构类型
     *
     * @return 支持的架构字符串
     */
    public static List<String> getCpuSupportAbis() {
        String[] abis;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            abis = Build.SUPPORTED_ABIS;
        } else {
            abis = new String[]{Build.CPU_ABI, Build.CPU_ABI2};
        }
        List<String> list = new ArrayList<>();
        for (String tmp : abis) {
            list.add(tmp);
        }
        return list;
    }

    /**
     * 是否是arm架构
     *
     * @return
     */
    public static boolean isArm() {
        boolean isArm = false;
        for (String tmp : getCpuSupportAbis()) {
            if (tmp.contains("armeabi")) {
                //是arm架构
                isArm = true;
                break;
            }
        }
        return isArm;
    }

    /**
     * 是否是x86架构
     *
     * @return
     */
    public static boolean isX86() {
        boolean isX86 = false;
        for (String tmp : getCpuSupportAbis()) {
            if (tmp.contains("x86")) {
                isX86 = true;
                break;
            }
        }
        return isX86;
    }

    /**
     * [获取cpu类型和架构]
     *
     * @return 三个参数类型的数组，第一个参数标识是不是ARM架构，第二个参数标识是V6还是V7架构，第三个参数标识是不是neon指令集
     */
    public static Object[] getCpuArchitecture() {
        Object[] mArmArchitecture = new Object[3];
//        if ((Integer) mArmArchitecture[1] != -1) {
//            return mArmArchitecture;
//        }
        try {
            InputStream is = new FileInputStream("/proc/cpuinfo");
            InputStreamReader ir = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(ir);
            try {
                String nameProcessor = "Processor";
                String nameFeatures = "Features";
                String nameModel = "model name";
                String nameCpuFamily = "cpu family";
                while (true) {
                    String line = br.readLine();
                    String[] pair = null;
                    if (line == null) {
                        break;
                    }
                    pair = line.split(":");
                    if (pair.length != 2)
                        continue;
                    String key = pair[0].trim();
                    String val = pair[1].trim();
                    if (key.compareTo(nameProcessor) == 0) {
                        String n = "";
                        for (int i = val.indexOf("ARMv") + 4; i < val.length(); i++) {
                            String temp = val.charAt(i) + "";
                            if (temp.matches("\\d")) {
                                n += temp;
                            } else {
                                break;
                            }
                        }
                        mArmArchitecture[0] = "ARM";
                        mArmArchitecture[1] = Integer.parseInt(n);
                        continue;
                    }

                    if (key.compareToIgnoreCase(nameFeatures) == 0) {
                        if (val.contains("neon")) {
                            mArmArchitecture[2] = "neon";
                        }
                        continue;
                    }

                    if (key.compareToIgnoreCase(nameModel) == 0) {
                        if (val.contains("Intel")) {
                            mArmArchitecture[0] = "INTEL";
                            mArmArchitecture[2] = "atom";
                        }
                        continue;
                    }

                    if (key.compareToIgnoreCase(nameCpuFamily) == 0) {
                        mArmArchitecture[1] = Integer.parseInt(val);
                        continue;
                    }
                }
            } finally {
                br.close();
                ir.close();
                is.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return mArmArchitecture;
    }
}
