package com.hi.music.player.helper;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;

import com.hi.music.player.MusicApplication;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;

public class CommonUtils {

    private static String TAG = "----MUSIC---------";

    private static String TAG_ERROR = "----ERROR MUSIC---------";

    public static void LogMsg(String msg) {
        Log.d(TAG, msg);
    }

    public static void LogErrorMsg(String msg) {
        Log.e(TAG_ERROR, msg);
    }

    public static JSONObject toJsonObject(ResponseBody body) {
        try {
            String string = body.string();
            return new JSONObject(string);
        } catch (IOException | JSONException exception) {
            return null;
        }
    }


    public static int dpToPx(int dp) {
        return Math.round(dp * (MusicApplication.myApplication.getResources().getDisplayMetrics().xdpi / DisplayMetrics.DENSITY_DEFAULT));
    }
    public static int pxToDp(int px) {
        return Math.round(px / (MusicApplication.myApplication.getResources().getDisplayMetrics().xdpi / DisplayMetrics.DENSITY_DEFAULT));
    }

    // 获取导航栏高度的方法
    public int getNavigationBarHeight(Context context) {
        int navigationBarHeight = 0;
        int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            navigationBarHeight = context.getResources().getDimensionPixelSize(resourceId);
        }
        return navigationBarHeight;
    }

    public static  int getStatusBarHeight(Context context) {
        int statusBarHeight = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }
}
