package com.neo.plugins.printer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintJob;
import android.print.PrintManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

//import androidx.print.PrintHelper;
import com.getcapacitor.Bridge;

import java.io.InputStream;

import static android.print.PrintAttributes.DUPLEX_MODE_LONG_EDGE;
import static android.print.PrintAttributes.DUPLEX_MODE_NONE;

public class PhotoPrinter {
    private Bridge _bridge;
    private WebView mWebView;
    public PhotoPrinter(Bridge bridge) {
        _bridge = bridge;
    }

    public void print(final String html, final Boolean duplexMode) {
//        PrintHelper printer = new PrintHelper(_bridge.getActivity());
//        printer.setScaleMode(scaleMode);
//        Bitmap bitmap = BitmapFactory.decodeStream(is);
//        printer.printBitmap(jobName, bitmap);

        // Create a WebView object specifically for printing
        _bridge.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
            WebView webView = new WebView(_bridge.getContext());
            webView.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    createWebPrintJob(view, duplexMode);
                    mWebView = null;
                }
            });

            // Generate an HTML document on the fly:
//            String htmlDocument = "<html><body><h1>Test Content</h1><p>Testing, " +
//                    "testing, testing...</p></body></html>";
            webView.loadDataWithBaseURL(null, html, "text/HTML", "UTF-8", null);

            // Keep a reference to WebView object until you pass the PrintDocumentAdapter
            // to the PrintManager
            mWebView = webView;
            }
        });
    }

    private void createWebPrintJob(WebView webView, Boolean duplexMode) {

        // Get a PrintManager instance
        PrintManager printManager = (PrintManager) _bridge.getActivity()
                .getSystemService(Context.PRINT_SERVICE);

        String jobName = "Print";

        // Get a print adapter instance
        PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter(jobName);

        // Create a print job with name and adapter instance
        PrintJob printJob = printManager.print(jobName, printAdapter,
                new PrintAttributes.Builder().setDuplexMode(duplexMode ? DUPLEX_MODE_LONG_EDGE : DUPLEX_MODE_NONE).build());

        // Save the job object for later status checking
//        printJobs.add(printJob);
    }
}