package com.datarecovery.master.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;

import androidx.annotation.DrawableRes;
import androidx.core.content.res.ResourcesCompat;

import com.atmob.common.runtime.ContextUtil;
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 java.util.EnumMap;
import java.util.Map;

public class QrCodeUtil {
    private static final int WHITE = 0xFFFFFFFF;

    private static final int BLACK = 0xFF000000;

    public static Bitmap generateQrCode(String qrCodeData, int size, @DrawableRes int logoRes) throws WriterException {
        Map<EncodeHintType, Object> hints = null;
        String encoding = guessAppropriateEncoding(qrCodeData);
        if (encoding != null) {
            hints = new EnumMap<>(EncodeHintType.class);
            hints.put(EncodeHintType.CHARACTER_SET, encoding);
        }
        BitMatrix bitMatrix = new MultiFormatWriter().encode(
                qrCodeData,
                BarcodeFormat.QR_CODE,
                size,
                size,
                hints
        );
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        int[] pixels = new int[width * height];
        int offsetX = -1, offsetY = -1, resultW = -1, resultH = -1;
        for (int y = 0; y < height; y++) {
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                boolean isBlack = bitMatrix.get(x, y);
                pixels[offset + x] = isBlack ? BLACK : WHITE;
                if (isBlack) {
                    if (offsetX == -1) {
                        offsetX = x;
                    }
                    if (offsetY == -1) {
                        offsetY = y;
                    }
                    if (y == offsetY) {
                        resultW = x - offsetX;
                    }
                    if (x == offsetX) {
                        resultH = y - offsetY;
                    }
                }
            }
        }
        int offset = offsetY * width + offsetX;
        Bitmap bitmap = Bitmap.createBitmap(resultW, resultH, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, offset, width, 0, 0, resultW, resultH);

        if (logoRes != 0) {
            try {
                Drawable logoDrawable = ResourcesCompat.getDrawable(ContextUtil.getContext().getResources(), logoRes, null);
                if (logoDrawable != null) {
                    Bitmap resultBitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(resultBitmap);
                    Matrix matrix = new Matrix();
                    matrix.setScale(size / (float) resultW, size / (float) resultH);
                    canvas.drawBitmap(bitmap, matrix, null);
                    int logoSize = (int) (size / 4.3f);
                    logoDrawable.setBounds(size / 2 - logoSize / 2, size / 2 - logoSize / 2,
                            size / 2 + logoSize / 2, size / 2 + logoSize / 2);
                    logoDrawable.draw(canvas);
                    canvas.setBitmap(null);
                    bitmap.recycle();
                    return resultBitmap;
                }
            } catch (Exception ignore) {
            }
        }
        return bitmap;
    }

    private static String guessAppropriateEncoding(CharSequence contents) {
        // Very crude at the moment
        for (int i = 0; i < contents.length(); i++) {
            if (contents.charAt(i) > 0xFF) {
                return "UTF-8";
            }
        }
        return null;
    }
}
