package com.police.policedatasystem.util;

import android.util.Base64;
import android.widget.Toast;

import com.police.policedatasystem.App;

import java.util.UUID;

public class UiUtils {
    /**
     * 将dp值转换为px值，保证尺寸大小不变
     *
     * @param dpValue dp值
     * @return 转换后的px值
     */
    public static int dp2Px(int dpValue) {
        final float scale = App.getApp().getResources().getDisplayMetrics().density;
        return Math.round((float) dpValue * scale);
    }
    public static String uuid() {
        return UUID.randomUUID().toString();
    }

    public static void toast(String str) {
        App.getApp().getHandler().post(() -> Toast.makeText(App.getApp(), str, Toast.LENGTH_LONG).show());
    }

    public static boolean isEmpty(String str) {
        if (str == null) {
            return true;
        }
        return str.isEmpty();
    }

    public static boolean isNotEmpty(String str) {
        if (str == null) {
            return false;
        }
        return !str.isEmpty();
    }
    public static void ui(){
        App.getHandler().post(new Runnable() {
            @Override
            public void run() {

            }
        });
    }


    public static byte[] base64ToBytes(String base64Str) {
        return Base64.decode(base64Str, Base64.DEFAULT);
    }
    public static String formatTime(String str) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            if (UiUtils.isEmpty(str)) return "";
            if (str.contains("-")) {
                return str;
            }
            if (str.length() == 8) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(str.substring(0, 4)).append("-" + str.substring(4, 6)).append("-" + str.substring(6, 8));
                return stringBuilder.toString();
            }
            if (str.length() > 8) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(str.substring(0, 4))
                        .append("-" + str.substring(4, 6))
                        .append("-" + str.substring(6, 8))
                        .append(" " + str.substring(8, 10))
                        .append(":" + str.substring(10, 12));
                return stringBuilder.toString();
            }
        }
        return "";
    }
}
