package com.yutou.jianr_mg.Data;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import androidx.core.app.ActivityCompat;
import android.telephony.TelephonyManager;

import com.yutou.jianr_mg.Application;
import com.yutou.jianr_mg.Tools.RootUtils;
import com.yutou.jianr_mg.Tools.Utils;

import java.io.File;

public class SystemData {
    private int osVersionCode;
    private String osName;
    private String appVersion;
    private boolean isRoot;
    private boolean isInstallClient;
    private String cpuType;
    private String phoneDisplay;
    private String imei;

    public SystemData() {
        if (Utils.getApplicationPackName(Application.application, AppData.magicPackageName)
                || new File( "/sdcard/Android/data/com.huanmeng.zhanjian2/files/MagicBox/").exists()) {
            isInstallClient = true;
        } else {
            isInstallClient = false;
        }
        osVersionCode = Build.VERSION.SDK_INT;
        osName = Build.VERSION.RELEASE;
        try {
            appVersion = Application.application.getPackageManager().getPackageInfo(Application.application.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            appVersion = AppData.version;
        }
        RootUtils.init(AppData.activity);
        isRoot = RootUtils.su();
        cpuType = Build.CPU_ABI;
        phoneDisplay = Build.BRAND;

        TelephonyManager telephonyManager = (TelephonyManager) Application.application.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager != null && ActivityCompat.checkSelfPermission(Application.application, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            imei = telephonyManager.getDeviceId();
        }

    }

    public int getOsVersionCode() {
        return osVersionCode;
    }

    public String getOsName() {
        return osName;
    }

    public String getAppVersion() {
        return appVersion;
    }

    public boolean isRoot() {
        return isRoot;
    }

    public boolean isInstallClient() {
        return isInstallClient;
    }

    public String getCpuType() {
        return cpuType;
    }

    public String getPhoneDisplay() {
        return phoneDisplay;
    }

    public String getImei() {
        return imei;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"osVersionCode\":")
                .append(osVersionCode);
        sb.append(",\"osName\":\"")
                .append(osName).append('\"');
        sb.append(",\"appVersion\":\"")
                .append(appVersion).append('\"');
        sb.append(",\"isRoot\":")
                .append(isRoot);
        sb.append(",\"isInstallClient\":")
                .append(isInstallClient);
        sb.append(",\"cpuType\":\"")
                .append(cpuType).append('\"');
        sb.append(",\"phoneDisplay\":\"")
                .append(phoneDisplay).append('\"');
        sb.append(",\"imei\":\"")
                .append(imei).append('\"');
        sb.append('}');
        return sb.toString();
    }
}
