package com.sdk.tspl.demo;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.shockwave.pdfium.PdfDocument;
import com.shockwave.pdfium.PdfiumCore;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by NO on 2017/9/14.
 */

public class Utility {
    public  static void checkBlueboothPermission(Activity context, String permission, String[] requestPermissions, Callback callback){
        if (Build.VERSION.SDK_INT >= 23) {
            //校验是否已具有模糊定位权限
            if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(context, requestPermissions, 100);
            } else {
                //具有权限
                callback.permit();
                return;
            }
        } else {
            //系统不高于6.0直接执行
            callback.pass();
        }
    }
    public interface Callback {
        /**
         * API>=23 允许权限
         */
        void permit();


        /**
         * API<23 无需授予权限
         */
        void pass();
    }

    public static void show(final Context context, final String message){
        ((Activity)context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
            }
        });

    }

    //file:PDF文件，
    //pageNumber:需要生成的页数，例如："1,2-3",
    //pageWidth:需要生成的宽度
    public static List<Bitmap> pdfToBitmap(Context context, File file, String pageNumber, int pageWidth) {
        ArrayList<Bitmap> ListBitmap = new ArrayList<Bitmap>();
        try {
            ArrayList<String> list = new ArrayList<String>();
            ParcelFileDescriptor parcelFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
            PdfiumCore pdfiumCore = new PdfiumCore(context);
            PdfDocument pdfDocument = pdfiumCore.newDocument(parcelFileDescriptor);
            int pages = pdfiumCore.getPageCount(pdfDocument);
            String[] split = pageNumber.split(",");
            for (int i = 0; i < split.length; i++) {
                String[] split2 = split[i].split("-");
                if (split2.length == 2) {
                    Integer bigPage = Integer.valueOf(split2[1]);
                    Integer smallPage = Integer.valueOf(split2[0]);
                    int page = bigPage - smallPage;
                    for (int j = 0; j < page + 1; j++) {
                        if (!list.contains("" + (smallPage + j))) {
                            list.add("" + (smallPage + j));
                        }
                    }
                } else {
                    if (!list.contains(split2[0])) {
                        list.add(split2[0]);
                    }
                }
            }
            int width = pageWidth;
            int height = 864;
            int dpi = 200;
            if (list.size() == 0)
                return ListBitmap;
            for (int pageIndex = 0; pageIndex < list.size(); pageIndex++) {
                int page = Integer.valueOf(list.get(pageIndex));
                if (0 < page && page <= pages) {
                    pdfiumCore.openPage(pdfDocument, page - 1);
                    int pageWidthPoint = pdfiumCore.getPageWidthPoint(pdfDocument, page - 1);
                    int pageHeightPoint = pdfiumCore.getPageHeightPoint(pdfDocument, page - 1);
                    double bili = width / (double) pageWidthPoint;
                    height = (int) (pageHeightPoint * bili);
                    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                    pdfiumCore.renderPageBitmap(pdfDocument, bitmap, page - 1, 0, 0, width, height);
                    ListBitmap.add(bitmap);
                }
            }
            pdfiumCore.closeDocument(pdfDocument);
        } catch (Exception e) {
            return ListBitmap;
        }
        return ListBitmap;
    }
}
