package com.hh.lib_base_print.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Build;
import android.text.TextPaint;
import android.util.Base64;
import android.util.Log;

import androidx.core.content.res.ResourcesCompat;


import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.hh.lib_base_print.R;
import com.hh.lib_base_print.bean.PrintContentBean;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class BitmapUtils {



    public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
        Bitmap newbm = null;
        if (bm != null) {
            // 获得图片的宽高
            int width = bm.getWidth();
            int height = bm.getHeight();
            // 计算缩放比例
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            // 取得想要缩放的matrix参数
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            // 得到新的图片
            newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        }
        return newbm;
    }

    public static Bitmap textPositionToBitmap(Context context,String text,int textSize,int xPosition,int yPosition){


        TextPaint textPaint = new TextPaint();
        // 80
        textPaint.setTextSize(80);
        textPaint.setColor(Color.BLACK);

        Typeface typeface = ResourcesCompat.getFont(context, R.font.songti);
        textPaint.setTypeface(typeface);// 采用自定义的字体
//        int width = (int) Math.ceil(textPaint.measureText(text));
//        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
//        int height = (int) Math.ceil(Math.abs(fontMetrics.bottom) + Math.abs(fontMetrics.top));

        // bitmap的width=1678 height=2397
        Bitmap bitmap = Bitmap.createBitmap(2000,2000, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        canvas.drawColor(Color.WHITE);
//        canvas.drawText(text,0,Math.abs(fontMetrics.ascent),textPaint);

        canvas.drawText(text,xPosition,yPosition,textPaint);

        canvas.save();

        return bitmap;

    }


    /**
     * 字符串转换成Bitmap类型
     * @param string
     * @return
     */
    public static Bitmap stringToBitmap(String string) {

        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
                    bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }
    public static String createPictureDirAndFile() {

        String logcatDir = getLogcatDir();
        String fileName = createFileName();
        String filePath = logcatDir + File.separator + fileName;

        return filePath;
    }


    public static String getLogcatDir() {
        //获取外存路径
        String externalStoragePath = PathUtils.getExternalAppFilesPath();//   /storage/emulated/0/Android/data/com.hh.arome/files
//        String externalStoragePath = PathUtils.getExternalStoragePath(); // /storage/emulated/0
        //命名存储目录
        String logPath = externalStoragePath + File.separator + "picture";
//        String logPath = externalStoragePath;
        //判断目录是否存在，不存在则判断是否建立成功
        FileUtils.createOrExistsDir(logPath);
        return logPath;
    }
    public static String createFileName() {
        String fileName = "";
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.CHINA);

        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);

        String curDate = dateFormat.format(calendar.getTime());
        //品牌+时间
        String model = Build.MODEL;
        fileName = model + "_" + curDate+"_"+ hour + ".png";

//        fileName = "a1.png";

        return fileName;
    }


    /**
     * 将Bitmap转成本地图片
     * @param path 保存为本地图片的地址
     * @param bitmap 要转化的Bitmap
     */
    public static void saveImage(String path, Bitmap bitmap){

        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
            bitmap.compress(Bitmap.CompressFormat.JPEG,80,bos);
            bos.flush();
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
