package com.miya.print.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.util.HashMap;
import java.util.Map;

/**
 * bitmap处理工具类
 */
public class BitmapUtils {

    /**
     * 按指定宽高缩放源bitmap
     *
     * @param origin 源bitmap
     * @param width  指定宽度，传0代表按height的比例进行缩放，比例计算公式：scale = origin.getHeight()/height
     * @param height 指定高度，传0代表按width的比例进行缩放，比例计算公式：scale = origin.getWidth()/width
     * @return width，height都传0代表不缩放，外部调用者注意bitmap回收
     */
    public static Bitmap scaleBitmap(Bitmap origin, int width, int height) {
        if (origin == null) {
            return null;
        }
        int widthOrigin = origin.getWidth();
        int heightOrigin = origin.getHeight();
        Matrix matrix = new Matrix();
        float scaleX = (float) width / (float) widthOrigin;
        float scaleY = (float) height / (float) heightOrigin;
        if (width == 0) {
            scaleX = scaleY;
        }
        if (height == 0) {
            scaleY = scaleX;
        }
        if ((width == 0 && height == 0) || width < 0 || height < 0) {
            //返回原图片
            return origin;
        }
        matrix.preScale(scaleX, scaleY);
        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, widthOrigin, heightOrigin, matrix, false);
//        if (newBM.equals(origin)) {
//            return newBM;
//        }
//        origin.recycle();
        return newBM;
    }

    /**
     * 给源bitmap添加白色左边距，用于打印对齐
     *
     * @param origin 源bitmap
     * @param width  指定左边距宽度
     * @return
     */
    public static Bitmap addWhiteLeftMargin(Bitmap origin, int width) {
        if (origin == null) {
            return null;
        }
        if (width <= 0) {
            return origin;
        }
        Bitmap newBm = Bitmap.createBitmap(width + origin.getWidth(), origin.getHeight(), origin.getConfig());
        Canvas canvas = new Canvas(newBm);
        //绘制左边白色边距
        canvas.drawColor(0xffffffff);
        canvas.drawBitmap(origin, width, 0, null);
        return newBm;
    }

    /**
     * 生成条码bitmap
     *
     * @param content 条码内容
     * @param format  条码类型
     *                *    0 -- UPC-A，
     *                *    1 -- UPC-E，
     *                *    2 -- JAN13(EAN13)，
     *                *    3 -- JAN8(EAN8)，
     *                *    4 -- CODE39，
     *                *    5 -- ITF，
     *                *    6 -- CODABAR，
     *                *    7 -- CODE93，
     *                *    8 -- CODE128
     * @param width 条码宽度
     * @param height 条码高度
     * @return 条码bitmap(可为null）
     */
    public static Bitmap createBarcodeBitmap(String content, int format, int width, int height) {
        if (content == null || content.equals(""))
            return null;
        BarcodeFormat barcodeFormat;
        switch (format) {
            case 0:
                barcodeFormat = BarcodeFormat.UPC_A;
                break;
            case 1:
                barcodeFormat = BarcodeFormat.UPC_E;
                break;
            case 2:
                barcodeFormat = BarcodeFormat.EAN_13;
                break;
            case 3:
                barcodeFormat = BarcodeFormat.EAN_8;
                break;
            case 4:
                barcodeFormat = BarcodeFormat.CODE_39;
                break;
            case 5:
                barcodeFormat = BarcodeFormat.ITF;
                break;
            case 6:
                barcodeFormat = BarcodeFormat.CODABAR;
                break;
            case 7:
                barcodeFormat = BarcodeFormat.CODE_93;
                break;
            case 8:
                barcodeFormat = BarcodeFormat.CODE_128;
                break;
            case 9:
                barcodeFormat = BarcodeFormat.QR_CODE;
                break;
            default:
                barcodeFormat = BarcodeFormat.QR_CODE;
                height = width;
                break;
        }
        MultiFormatWriter qrCodeWriter = new MultiFormatWriter();
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "GBK");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        try {
            BitMatrix encode = qrCodeWriter.encode(content, barcodeFormat, width, height, hints);
            int[] pixels = new int[width * height];
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    if (encode.get(j, i)) {
                        pixels[i * width + j] = 0x00000000;
                    } else {
                        pixels[i * width + j] = 0xffffffff;
                    }
                }
            }
            return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return null;
    }
}
