package com.naz.sdkdemo.utils;

import android.content.Context;
import android.view.WindowManager;

import java.text.SimpleDateFormat;
import java.util.Date;

import androidx.fragment.app.FragmentActivity;

import com.naz.sdkdemo.R;

/**
 * Description:
 * Author: ydd
 * Date:2020/12/7
 */
public class MyUtils {
    /**
     * 格式化显示的时长
     *
     * @param seconds
     * @return
     */
    public static String getDate(long seconds) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd \nHH:mm:ss");
        return dateFormat.format(new Date(seconds*1000));
    }
    public static String getDate2(long seconds) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return dateFormat.format(new Date(seconds*1000));
    }

    public static String formatYYYYMMdd(long seconds) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(new Date(seconds*1000));
    }

    public static int dp2px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }
    /**
     * 设置半透明背景
     */
    public static void   setWindowbgAlpha(FragmentActivity context, float alpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = alpha;// 0.0-1.0
        context.getWindow().setAttributes(lp);
    }

    public static int getStatusBarHeightCompat(Context context) {
        int result = 0;
        int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resId > 0) {
            result = context.getResources().getDimensionPixelOffset(resId);
        }
        if (result <= 0) {
            result = (int)(context.getResources().getDisplayMetrics().density * 25 + 0.5f);
        }
        return result;
    }

    public static int toInt(Object value) {
        return toInt(value, 0);
    }

    public static int toInt(Object value, int fallback) {
        if (null == value) {
            return fallback;
        }
        try {
            return Integer.parseInt(value+"");
        } catch (NumberFormatException e) {
            return fallback;
        }
    }
}
