package com.miya.print;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.os.IBinder;
import android.os.RemoteException;

import com.miya.print.exception.PrinterException;
import com.miya.print.utils.BytesUtil;
import com.miya.print.utils.ESCUtil;
import com.sunmi.extprinterservice.ExtPrinterService;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

class ShangmiK1Printer extends BasePrinter {

    final static String TAG = ShangmiK1Printer.class.getSimpleName();

    private ServiceConnection connService = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            extPrinterService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            extPrinterService = ExtPrinterService.Stub.asInterface(service);
        }
    };

    @Override
    public boolean init(Context context) {
        return super.init(context);
    }

    @Override
    public boolean release() {
        return super.release();
    }

    private static final String SERVICE＿PACKAGE = "com.sunmi.extprinterservice";
    private static final String SERVICE＿ACTION = "com.sunmi.extprinterservice.PrinterService";

    private ExtPrinterService extPrinterService;

    /**
     * 连接服务
     *
     * @param context context
     */
    @Deprecated
    public void connectPrinterService(Context context) {
        this.context = context.getApplicationContext();
        Intent intent = new Intent();
        intent.setPackage(SERVICE＿PACKAGE);
        intent.setAction(SERVICE＿ACTION);
        context.getApplicationContext().startService(intent);
        context.getApplicationContext().bindService(intent, connService, Context.BIND_AUTO_CREATE);
    }

    /**
     * 断开服务
     *
     * @param context context
     */
    @Deprecated
    public void disconnectPrinterService(Context context) {
        if (extPrinterService != null) {
            context.getApplicationContext().unbindService(connService);
            extPrinterService = null;
        }
    }

    @Deprecated
    public boolean isConnect() {
        return extPrinterService != null;
    }

    @Deprecated
    public void LF() {
        if (extPrinterService == null) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
        }


        try {
            extPrinterService.lineWrap(1);

        } catch (RemoteException e) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
        }
    }

    @Deprecated
    public void cutPage() {
        if (extPrinterService == null) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");

        }


        try {
            extPrinterService.cutPaper(0, 0);

        } catch (RemoteException e) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());

        }
    }

    @Deprecated
    public void setAlignMode(int mode) {
        if (extPrinterService == null) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");

        }


        try {
            extPrinterService.setAlignMode(mode);

        } catch (RemoteException e) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
        }
    }

    /**
     * 设置打印浓度
     */
    private int[] darkness = new int[]{0x0600, 0x0500, 0x0400, 0x0300, 0x0200, 0x0100, 0,
            0xffff, 0xfeff, 0xfdff, 0xfcff, 0xfbff, 0xfaff};

    public void setDarkness(int index) {
        if (extPrinterService == null) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
        }

        int k = darkness[index];
        try {
            extPrinterService.sendRawData(ESCUtil.setPrinterDarkness(k));

        } catch (RemoteException e) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
        }
    }


    /**
     * 初始化打印机
     */
    @Deprecated
    public void initPrinter() {
        if (extPrinterService == null) {
            return;
        }

        try {
            extPrinterService.printerInit();
        } catch (RemoteException e) {

        }
    }

    /**
     * 打印二维码
     */
    @Deprecated
    public void printQr(String data, int modulesize, int errorlevel) {
        if (extPrinterService == null) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");

        }


        try {
            extPrinterService.setAlignMode(1);
            extPrinterService.printQrCode(data, modulesize, errorlevel);
            extPrinterService.lineWrap(2);
        } catch (RemoteException e) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
        }
    }

    /**
     * 打印条形码 symbology条码类型
     */
    @Deprecated
    public void printBarCode(String code, int type, int width, int height, int hriPos) {
        if (extPrinterService == null) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
        }
        try {
            extPrinterService.printBarCode(code, type, width, height, hriPos);
            extPrinterService.lineWrap(2);
        } catch (RemoteException e) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
        }
    }


    /**
     * 打印一维条形码
     * data 条码内容
     * height 条码高度
     * width 条码宽度
     * textposition 条码内容位置
     */
    @Deprecated
    public void printLongBarCode(String data, int height, int width, int textposition) throws RemoteException, IOException {


        if (width < 2 || width > 6) {
            width = 2;
        }
        if (textposition < 0 || textposition > 3) {
            textposition = 0;
        }
        if (height < 1 || height > 255) {
            height = 162;
        }
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        buffer.write(new byte[]{0x1D, 0x48, (byte) textposition,
                0x1D, 0x77, (byte) width, 0x1D, 0x68, (byte) height, 0x0A});

        byte[] barcode;
        barcode = BytesUtil.getBytesFromDecString(data);
        buffer.write(new byte[]{0x1D, 0x6B, 0x49, (byte) (barcode.length + 2), 0x7B, (byte) (0x43)});
        buffer.write(barcode);


        extPrinterService.sendRawData(buffer.toByteArray());
    }

    /**
     * 打印文字
     */
    @Deprecated
    public void printText(String content, float size, boolean isBold, boolean isUnderLine) {
        if (extPrinterService == null) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
        }

        try {
            if (isBold) {
                extPrinterService.sendRawData(ESCUtil.boldOn());
            } else {
                extPrinterService.sendRawData(ESCUtil.boldOff());
            }

            if (isUnderLine) {
                extPrinterService.sendRawData(ESCUtil.underlineWithOneDotWidthOn());
            } else {
                extPrinterService.sendRawData(ESCUtil.underlineOff());
            }

            extPrinterService.printText(content);
        } catch (RemoteException e) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
        }

    }

    /*
     *打印图片
     */
    @Deprecated
    public void printBitmap(Bitmap bitmap) {
        if (extPrinterService == null) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");

        }

        try {
            extPrinterService.setAlignMode(1);
            extPrinterService.printBitmap(bitmap, 0);
            extPrinterService.lineWrap(2);
        } catch (RemoteException e) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
        }
    }

    @Deprecated
    public String updatePrinterState() throws RemoteException {
        if (extPrinterService == null) {
            return "打印服务已断开!";
        }
        int state = extPrinterService.getPrinterStatus();

        if (state == -1) {
            return "打印机脱机或打印服务还未连接打印机";
        }
        if (state == 0) {
            return "正常";
        }
        if (state == 1) {
            return "打印机开盖";
        }
        if (state == 2) {
            return "打印机缺纸";
        }
        if (state == 3) {
            return "打印机即将缺纸";
        }
        if (state == 4) {
            return "打印机过热";
        }
        return "获取状态异常";
    }


    /*
     * 空打三行！
     */
    @Deprecated
    public void print3Line() {
        if (extPrinterService == null) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
        }

        try {
            extPrinterService.lineWrap(3);
        } catch (RemoteException e) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
        }
    }

    @Deprecated
    public void sendRawData(byte[] data) {
        if (extPrinterService == null) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
        }

        try {
            extPrinterService.sendRawData(data);
        } catch (RemoteException e) {
            throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
        }
    }
}
