package com.rf.psychological.utils;


import com.rf.psychological.enums.UserRole;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zzf
 * @description:
 * @date 2021/1/28 10:43
 */
public class Utils {

    /**
     * 计算X的N次方
     * @param x 常数
     * @param n 指数
     * @return
     */
    public static int x_n(int x ,int n ){
        int result = 0;
        if(n==0){
            return 1 ;
        }else{
            result = x_n(x,n/2);
            result = result * result;
            if(n%2 != 0 ){
                result = x * result;
            }
        }
        return result;
    }

    /**
     * 计算对数函数
     * @param a 底数
     * @param n 真数
     * @param scale 小数位数
     * @return
     */
    public static double loga_n(int a ,int n,int scale) {
        double result = Math.log(n) / Math.log(a);
        return new BigDecimal(result).setScale(scale, RoundingMode.HALF_UP).doubleValue();
    }

    // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
    public static String GetImageStr(String imgFilePath) {
        byte[] data = null;

        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgFilePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 对字节数组Base64编码
        return new String(Base64.getEncoder().encode(data));// 返回Base64编码过的字节数组字符串
    }

    /**
     * zzf  roleType  to roleName
     * @param roleType
     * @return
     */
    public static String roleType2roleName(String roleType){
        String roleName = "";
        switch  (roleType){

            case "3":
                roleName = UserRole.SUPERADMIN.getName();
                break;
            case "2":
                roleName = UserRole.ADMIN.getName();
                break;
            case "1":
                roleName = UserRole.COMMON.getName();
                break;
        }
        return roleName;
    }

    /**
     * 日期时间戳转换
     *
     * @param dateStr yyyy-MM-dd
     * @param flag  Constant  搜索关键字  日期转换标志
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static Date date2time(String dateStr,String flag) throws ParseException {
        if(StringUtils.isBlank(dateStr))
            return null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(dateStr);
        if(flag.equals("start")){
            return date;
        }else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE,1);
            date = calendar.getTime();
        }
        return date;
    }


    // 对字节数组字符串进行Base64解码并生成图片
    public static boolean GenerateImage(String imgStr, String imgFilePath) {
        if (imgStr == null) // 图像数据为空
            return false;
        try {
            // Base64解码
            byte[] bytes = Base64.getDecoder().decode(imgStr);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(bytes);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    /**
     * @Title: unicodeDecode
     * @Description: unicode解码
     * @param sourceString
     * @return
     */
    public static String unicodeDecode(String sourceString) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(sourceString);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            sourceString = sourceString.replace(matcher.group(1), ch + "");
        }
        return sourceString;
    }

    /**
     * @Title: unicodeEncode
     * @Description: unicode编码
     * @param string
     * @return
     */
    public static String unicodeEncode(String string) {
        char[] utfBytes = string.toCharArray();
        String unicodeBytes = "";
        for (int i = 0; i < utfBytes.length; i++) {
            String hexB = Integer.toHexString(utfBytes[i]);
            if (hexB.length() <= 2) {
                hexB = "00" + hexB;
            }
            unicodeBytes = unicodeBytes + "\\u" + hexB;
        }
        return unicodeBytes;
    }

    /**
     * 两个时间差的分钟数
     * @param dateFrom 开始时间
     * @param dateEnd 结束时间
     */
    public static long minuteDiff(Date dateFrom , Date dateEnd){
        long dateLong = dateFrom.getTime()/1000/60;
        long dateLong1 = dateEnd.getTime()/1000/60;
        return  (dateLong-dateLong1);
    }

}
