package com.neptune.modules;

import android.graphics.Bitmap;

import com.neptune.base.NeptuneAppMgmt;
import com.neptune.utils.MyLog;
import com.pax.dal.IPrinter;
import com.pax.dal.entity.EFontTypeAscii;
import com.pax.dal.entity.EFontTypeExtCode;
import com.pax.dal.exceptions.PrinterDevException;

public class Printer {

    private static Printer printer;
    private IPrinter iPrinter;

    private Printer() {
        iPrinter = NeptuneAppMgmt.getDal().getPrinter();
    }

    public static Printer getInstance() {
        if (printer == null) {
            printer = new Printer();
        }
        return printer;
    }

    public void init() throws PrinterDevException {
        try {
            iPrinter.init();
            MyLog.logI("init");
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public String getStatus() throws PrinterDevException {
        try {
            int status = iPrinter.getStatus();
            MyLog.logI("getStatus");
            return statusCode2Str(status);
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }

    }

    public void fontSet(EFontTypeAscii asciiFontType, EFontTypeExtCode cFontType) throws PrinterDevException {
        try {
            iPrinter.fontSet(asciiFontType, cFontType);
            MyLog.logI("fontSet");
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public void spaceSet(byte wordSpace, byte lineSpace) throws PrinterDevException {
        try {
            iPrinter.spaceSet(wordSpace, lineSpace);
            MyLog.logI("spaceSet");
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public void printStr(String str, String charset) throws PrinterDevException {
        try {
            iPrinter.printStr(str, charset);
            MyLog.logI("printStr");
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public void step(int b) throws PrinterDevException {
        try {
            iPrinter.step(b);
            MyLog.logI("setStep");
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public void printBitmap(Bitmap bitmap) throws PrinterDevException {
        try {
            iPrinter.printBitmap(bitmap);
            MyLog.logI("printBitmap");
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public String start() throws PrinterDevException {
        try {
            int res = iPrinter.start();
            MyLog.logI("start");
            return statusCode2Str(res);
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public void leftIndents(short indent) throws PrinterDevException {
        try {
            iPrinter.leftIndent(indent);
            MyLog.logI("leftIndent");
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public int getDotLine() throws PrinterDevException {
        try {
            int dotLine = iPrinter.getDotLine();
            MyLog.logI("getDotLine");
            return dotLine;
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public void setGray(int level) throws PrinterDevException {
        try {
            iPrinter.setGray(level);
            MyLog.logI("setGray");
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public void setDoubleWidth(boolean isAscDouble, boolean isLocalDouble) throws PrinterDevException {
        try {
            iPrinter.doubleWidth(isAscDouble, isLocalDouble);
            MyLog.logI("doubleWidth");
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public void setDoubleHeight(boolean isAscDouble, boolean isLocalDouble) throws PrinterDevException {
        try {
            iPrinter.doubleHeight(isAscDouble, isLocalDouble);
            MyLog.logI("doubleHeight");
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }

    }

    public void setInvert(boolean isInvert) throws PrinterDevException {
        try {
            iPrinter.invert(isInvert);
            MyLog.logI("setInvert");
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }

    }

    public String cutPaper(int mode) throws PrinterDevException {
        try {
            iPrinter.cutPaper(mode);
            MyLog.logI("cutPaper");
            return "cut paper successful";
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public String getCutMode() throws PrinterDevException {
        String resultStr = "";
        try {
            int mode = iPrinter.getCutMode();
            MyLog.logI("getCutMode");
            switch (mode) {
                case 0:
                    resultStr = "Only support full paper cut";
                    break;
                case 1:
                    resultStr = "Only support partial paper cutting ";
                    break;
                case 2:
                    resultStr = "support partial paper and full paper cutting ";
                    break;
                case -1:
                    resultStr = "No cutting knife,not support";
                    break;
                default:
                    break;
            }
            return resultStr;
        } catch (PrinterDevException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public String statusCode2Str(int status) {
        String res = "";
        switch (status) {
            case 0:
                res = "Success ";
                break;
            case 1:
                res = "Printer is busy ";
                break;
            case 2:
                res = "Out of paper ";
                break;
            case 3:
                res = "The format of print data packet error ";
                break;
            case 4:
                res = "Printer malfunctions ";
                break;
            case 8:
                res = "Printer over heats ";
                break;
            case 9:
                res = "Printer voltage is too low";
                break;
            case 240:
                res = "Printing is unfinished ";
                break;
            case 252:
                res = " The printer has not installed font library ";
                break;
            case 254:
                res = "Data package is too long ";
                break;
            default:
                break;
        }
        return res;
    }
}
