package com.jimind.sportdemo.utils;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import java.text.DecimalFormat;

/**
 * 常用工具类
 *
 * @author Any
 */

public class CommonUtils {

    /**
     * 初始化title高度
     *
     * @param layout
     */
    public static void initTitleBar(Context context, ViewGroup layout){
        ViewGroup.LayoutParams layoutParams = layout.getLayoutParams();
        int height = CommonUtils.getStatusHeight(context);
        layoutParams.height = height + CommonUtils.dip2px(context, 60);
        layout.setLayoutParams(layoutParams);
        layout.setPadding(0,height,0,0);
    }

    /**
     * 初始化title高度
     *
     * @param view
     */
    public static void initTitle(Context context, View view){
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        int height = CommonUtils.getStatusHeight(context);
        layoutParams.height = height + CommonUtils.dip2px(context, 60);
        view.setLayoutParams(layoutParams);
        view.setPadding(0,height,0,0);
    }

    /**
     * 利用反射获取状态栏高度+标题栏高度
     *
     * @return
     */
    public static int getStatusHeight(Context context) {
        int result = 0;
        //获取状态栏高度的资源id
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * dp转成px
     *
     * @param context
     * @param dipValue
     * @return
     */
    public static int dip2px(Context context, float dipValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }


    public static int px2dip(Context context, float pxValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 显示软键盘
     *
     * @param context
     * @param mEditText
     */
    public static void showKeyBoard(Context context, EditText mEditText) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    /**
     * 隐藏软键盘
     *
     * @param context
     * @param view
     */
    public static void hideSoftKeyboard(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
        }
    }

    /**
     * 获得屏幕高度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    /**
     * 获得屏幕宽度
     *
     * @param context
     * @return
     */
    public static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }


    /**
     * 保留两位小数
     *
     * @param number double
     * @return String类型的两位小数
     */
    public static String setDoubleTwo(double number) {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(number);
    }


}
