package com.android.common.utils;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.util.Locale;
import java.util.UUID;

public class DeviceUtils {

    public static  int getScreenWidth(Context context){
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    public static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    public static float getScreenDensity(Context context) {
        return context.getResources().getDisplayMetrics().density;
    }

    public static int dip2px(Context context, float dp) {
        final float scale = getScreenDensity(context);
        return (int) (dp * scale + 0.5);
    }

    public static String getMacAddress(Context context){
        String mac = "00:00:00:00:00:00";
        if (context == null) {
            return mac;
        }
        final WifiManager wifi = (WifiManager) context.getApplicationContext()
                .getSystemService(Context.WIFI_SERVICE);
        if (wifi == null) {
            return mac;
        }
        WifiInfo info = null;
        try {
            info = wifi.getConnectionInfo();
        } catch (Exception e) {
        }
        if (info == null) {
            return null;
        }
        mac = info.getMacAddress();
        if (!TextUtils.isEmpty(mac)) {
            mac = mac.toUpperCase(Locale.ENGLISH);
        } else {
            mac = "00:00:00:00:00:00";
        }
        return mac;
    }

    public static String getUUID(Context context){
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId, androidId;
        try{
            deviceId =tm.getDeviceId();
        }catch (Exception e){
            deviceId = "1";
            e.printStackTrace();
        }
        androidId =Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        UUID deviceUuid = new UUID(androidId.hashCode(), ((long)deviceId.hashCode() << 32));
        String uniqueId = deviceUuid.toString();
        LogHelper.d("czy"," deviceId= "+deviceId+" androidId="+androidId+" uuid="+uniqueId);
        return uniqueId;
    }
}
