package com.joe.print.mvp.print.maker;

import android.graphics.Bitmap;

import com.gingersoft.gsa.cloud.print.PrintDataMaker;
import com.gingersoft.gsa.cloud.print.PrintExecutor;
import com.gingersoft.gsa.cloud.print.PrinterWriter;
import com.gingersoft.gsa.cloud.print.PrinterWriterExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Wyh on 2020/1/9.
 * ip打印類
 */
public class PrjPrintMaker implements PrintDataMaker {

    private Map<String, Bitmap> bitmapMap;
    private String ip;
    private int port;

    private PrintExecutor.OnPrjPrintResultListener onPrjPrintResultListener;

    public PrintExecutor.OnPrjPrintResultListener getOnPrjPrintResultListener() {
        return onPrjPrintResultListener;
    }

    public PrjPrintMaker(Map<String, Bitmap> bitmapMap, String ip, int port) {
        this.bitmapMap = bitmapMap;
        this.ip = ip;
        this.port = port;
    }


    @Override
    public List<byte[]> getPrintData() {
        ArrayList<byte[]> data = new ArrayList<>();
        try {
            PrinterWriter printer = new PrinterWriterExecutor(255, 380);
            for (Map.Entry<String, Bitmap> bitmapMap : bitmapMap.entrySet()) {
                ArrayList<byte[]> image1 = printer.getImageByte(bitmapMap.getValue());
                data.addAll(image1);
            }
            printer.printLineFeed(5);
            printer.feedPaperCutPartial();
            data.add(printer.getDataAndReset());
            data.add(printer.getDataAndClose());
            return data;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    @Override
    public String getPrjIds() {
        for (Map.Entry<String, Bitmap> bitmapMap : bitmapMap.entrySet()) {
            return bitmapMap.getKey();
        }
        return "";
    }

    @Override
    public String getIp() {
        return ip;
    }

    @Override
    public int getPort() {
        return port;
    }
}
