package com.leeskies.capacitorpdfprinter;

import android.content.Context;
import android.util.Log;

import com.example.tscdll.TscWifiActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
import com.getcapacitor.PluginCall;

public class Printer {
    private TscWifiActivity tsc = new TscWifiActivity();
    public void print(int port, String IPAddress, String uri) {
        tsc.openport(IPAddress, port);
//        tsc.printPDFbyFile()
//        tsc.printPDFbyFile()
        String flag = tsc.printPDFbyPath(uri, 0, 100, 300);

    }
    public void printPdf(int port, String IPAddress, String base64String, int x, int y, int dpi, PluginCall call) {
        tsc.openport(IPAddress, port);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            byte[] pdfBytes = Base64.getDecoder().decode(base64String);
            File tempFile = null;
            try {
                tempFile = File.createTempFile("tempPDF", ".pdf");
                try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                    fos.write(pdfBytes);
                }
//                    tsc.setup(75, 50, 5 , 8 , )
                    String result = tsc.printPDFbyFile(tempFile, x, y, dpi);

                    if (result.equals("-1")) call.reject(result + " Print operation failed");
                    Log.d("Printer", "Print result: " + result);
                } catch (Exception e) {
                    e.printStackTrace();
                    call.reject("Printer", "Error creating temporary file" + e.getMessage());
                } finally {
                    if (tempFile != null && tempFile.exists()) {
                        tempFile.delete();
                    }
                    tsc.clearbuffer();
                    tsc.closeport(port);
                }

        }
    }
    public void printPdf(int port, String IPAddress, String base64String, int x, int y, int width, int height, int dpi, PluginCall call) {
        tsc.openport(IPAddress, port);
        int speed = 6;
        int density = 8; // middle
        int sensor = 0; // default - by gap
        int gap = 30; // in mm. change as you like
        int shift = 0; // default. IDK
        tsc.setup(width, height, speed, density, sensor, gap, shift);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            byte[] pdfBytes = Base64.getDecoder().decode(base64String);
            File tempFile = null;
            try {
                tempFile = File.createTempFile("tempPDF", ".pdf");
                try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                    fos.write(pdfBytes);
                }
//                    tsc.setup(75, 50, 5 , 8 , )
                String result = tsc.printPDFbyFile(tempFile, x, y, dpi);

                if (result.equals("-1")) call.reject(result + " Print operation failed");
                Log.d("Printer", "Print result: " + result);
            } catch (Exception e) {
                e.printStackTrace();
                call.reject("Printer", "Error creating temporary file" + e.getMessage());
            } finally {
                if (tempFile != null && tempFile.exists()) {
                    tempFile.delete();
                }
                tsc.clearbuffer();
                tsc.closeport(port);
            }

        }
    }

}
