package com.bixolon.sample;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.bixolon.BixolonConst;
import com.bixolon.mpos.config.util.MPosControllerLookup;
import com.bixolon.mpos.print.FontAttribute;

public class PosFragment extends Fragment implements View.OnClickListener, RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener {
    private CheckBox checkBoxBypassMode = null;
    private CheckBox checkBoxAsb = null;
    private CheckBox checkBoxPnp = null;
    private RadioGroup radioGroupPortType;

    private EditText editTextDevID = null;
    private EditText editTextAddress = null;

    private int portType = BixolonConst.MPOS_INTERFACE_BLUETOOTH;

    public static PosFragment newInstance() {
        PosFragment fragment = new PosFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pos, container, false);

        checkBoxBypassMode = view.findViewById(R.id.checkBoxBypassMode);
        checkBoxAsb = view.findViewById(R.id.checkBoxAsb);
        checkBoxPnp = view.findViewById(R.id.checkBoxPnp);
        checkBoxAsb.setOnCheckedChangeListener(this);
        checkBoxPnp.setOnCheckedChangeListener(this);

        radioGroupPortType = view.findViewById(R.id.radioGroupPortType);
        radioGroupPortType.setOnCheckedChangeListener(this);

        editTextDevID = view.findViewById(R.id.editTextDevID);
        editTextAddress = view.findViewById(R.id.editTextAddress);
        editTextAddress.setText("74:F0:7D:00:00:00");

        view.findViewById(R.id.btnGetAddress).setOnClickListener(this);
        view.findViewById(R.id.btnOpen).setOnClickListener(this);
        view.findViewById(R.id.btnClose).setOnClickListener(this);
        view.findViewById(R.id.btnPrinterStatus).setOnClickListener(this);
        view.findViewById(R.id.btnText).setOnClickListener(this);
        view.findViewById(R.id.btnBarcode).setOnClickListener(this);
        view.findViewById(R.id.btnImage).setOnClickListener(this);
        view.findViewById(R.id.btnPageMode).setOnClickListener(this);
        view.findViewById(R.id.btnPrintSample).setOnClickListener(this);
        view.findViewById(R.id.btnPDFSample).setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnGetAddress:
                try {
                    String[] items = null;
                    if (BixolonConst.MPOS_SUCCESS == MPosControllerLookup.refreshDeviceList(portType, 3)) {
                        MPosControllerLookup.MPosDeviceInfo[] deviceInfos = MPosControllerLookup.getDeviceList(portType);
                        if (deviceInfos != null && deviceInfos.length > 0) {
                            items = new String[deviceInfos.length];
                            if (portType == BixolonConst.MPOS_INTERFACE_BLUETOOTH) {
                                for (int i = 0; i < deviceInfos.length; i++) {
                                    items[i] = deviceInfos[i].deviceName + "\n" + deviceInfos[i].macAddress;
                                }
                            } else if (portType == BixolonConst.MPOS_INTERFACE_WIFI) {
                                for (int i = 0; i < deviceInfos.length; i++) {
                                    items[i] = deviceInfos[i].ipAddress;
                                }
                            }

                            final String[] finalItems = items;
                            new AlertDialog.Builder(getContext()).setTitle("Device List")
                                    .setItems(items, new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            String strSelectList = finalItems[which];
                                            String strDeviceInfo = null;
                                            if (portType == BixolonConst.MPOS_INTERFACE_BLUETOOTH) {
                                                String[] splitAddr = strSelectList.split("\n");
                                                strDeviceInfo = splitAddr[1];
                                            } else {
                                                strDeviceInfo = strSelectList;
                                            }
                                            editTextAddress.setText(strDeviceInfo);
                                        }
                                    }).show();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btnOpen:
                openService();
                break;
            case R.id.btnClose:
                closeService();
                break;
            case R.id.btnPrinterStatus:
                if (!MainActivity.mPosControllerPrinter.isOpen()) {
                    Toast.makeText(getContext(), "Device not opened.", Toast.LENGTH_LONG).show();
                    return;
                }
                MainActivity.mPosControllerPrinter.checkPrinterStatus();
                break;
            case R.id.btnText:
                printText();
                break;
            case R.id.btnBarcode:
                printBarcode();
                break;
            case R.id.btnImage:
                printImage();
                break;
            case R.id.btnPageMode:
                printPageMode();
                break;
            case R.id.btnPrintSample:
                printSampleReceipt();
                break;
            case R.id.btnPDFSample:
                printPDF();
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.radioBT:
                editTextAddress.setText("74:F0:7D:00:00:00");
                portType = BixolonConst.MPOS_INTERFACE_BLUETOOTH;
                break;
            case R.id.radioWifi:
                editTextAddress.setText("192.168.0.1");
                portType = BixolonConst.MPOS_INTERFACE_WIFI;
                break;
            case R.id.radioUSB:
                editTextAddress.setText("");
                portType = BixolonConst.MPOS_INTERFACE_USB;
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!MainActivity.mPosControllerPrinter.isOpen()) {
            Toast.makeText(getContext(), "Device not opened.", Toast.LENGTH_LONG).show();
            checkBoxAsb.setChecked(!isChecked);
            return;
        }

        switch (buttonView.getId()) {
            case R.id.checkBoxAsb:
                MainActivity.mPosControllerPrinter.asbEnable(isChecked);
                break;
            case R.id.checkBoxPnp:
                MainActivity.mPosControllerPrinter.setPnpEnable(isChecked);
                break;
        }
    }

    private void openService() {
        String strId = editTextDevID.getText().toString();
        String strAddress = editTextAddress.getText().toString();
        if (strId.isEmpty() || strAddress.isEmpty()) {
            return;
        }

        boolean isBypass = checkBoxBypassMode.isChecked();
        int devId = Integer.parseInt(strId);

        MainActivity.mPosControllerPrinter.setTimeout(5);
        MainActivity.mPosControllerPrinter.selectCommandMode(isBypass ? BixolonConst.MPOS_COMMAND_MODE_BYPASS : BixolonConst.MPOS_COMMAND_MODE_DEFAULT);
        MainActivity.mPosControllerPrinter.selectInterface(portType, strAddress);
        long ret = MainActivity.mPosControllerPrinter.openService(devId, getContext());
        if (ret != BixolonConst.MPOS_SUCCESS) {
            Toast.makeText(getContext(), "Device open fail : " + ret, Toast.LENGTH_LONG).show();
            return;
        }
    }

    private void closeService() {
        if (MainActivity.mPosControllerPrinter.isOpen()) {
            MainActivity.mPosControllerPrinter.closeService(0);
        }
    }

    private void printText() {
        if (!MainActivity.mPosControllerPrinter.isOpen()) {
            Toast.makeText(getContext(), "Device not opened.", Toast.LENGTH_LONG).show();
            return;
        }

        MainActivity.mPosControllerPrinter.setTransaction(BixolonConst.MPOS_PRINTER_TRANSACTION_IN);

        FontAttribute fontAttribute = new FontAttribute();
        fontAttribute.init();
        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_LEFT);
        MainActivity.mPosControllerPrinter.printText("Align left\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_CENTER);
        MainActivity.mPosControllerPrinter.printText("Align center\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_RIGHT);
        MainActivity.mPosControllerPrinter.printText("Align right\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setFontType(BixolonConst.MPOS_FONT_TYPE_A);
        MainActivity.mPosControllerPrinter.printText("Font A\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setFontType(BixolonConst.MPOS_FONT_TYPE_B);
        MainActivity.mPosControllerPrinter.printText("Font B\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setFontType(BixolonConst.MPOS_FONT_TYPE_C);
        MainActivity.mPosControllerPrinter.printText("Font C\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setBold(true);
        MainActivity.mPosControllerPrinter.printText("Text bold\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setUnderline(1);
        MainActivity.mPosControllerPrinter.printText("Text underline\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setReverse(true);
        MainActivity.mPosControllerPrinter.printText("Text reverse\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setFontSize(0x00);
        MainActivity.mPosControllerPrinter.printText("Font size 1x1(Default)\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setFontSize(0x11);
        MainActivity.mPosControllerPrinter.printText("Font size 2x2\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setFontSize(0x22);
        MainActivity.mPosControllerPrinter.printText("Font size 3x3\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setFontSize(0x33);
        MainActivity.mPosControllerPrinter.printText("Font size 4x4\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setFontSize(0x44);
        MainActivity.mPosControllerPrinter.printText("Font size 5x5\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setFontSize(0x55);
        MainActivity.mPosControllerPrinter.printText("Font size 6x6\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setFontSize(0x66);
        MainActivity.mPosControllerPrinter.printText("Font size 7x7\n", fontAttribute);

        fontAttribute.init();
        fontAttribute.setFontSize(0x77);
        MainActivity.mPosControllerPrinter.printText("Font size 8x8\n\n\n\n\n\n\n", fontAttribute);

        // MainActivity.mPosControllerPrinter.cutPaper(BixolonConst.MPOS_PRINTER_FEEDCUT);

        MainActivity.mPosControllerPrinter.setTransaction(BixolonConst.MPOS_PRINTER_TRANSACTION_OUT);
    }

    private void printBarcode() {
        if (!MainActivity.mPosControllerPrinter.isOpen()) {
            Toast.makeText(getContext(), "Device not opened.", Toast.LENGTH_LONG).show();
            return;
        }

        FontAttribute fontAttribute = new FontAttribute();

        MainActivity.mPosControllerPrinter.setTransaction(BixolonConst.MPOS_PRINTER_TRANSACTION_IN);

        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_LEFT);
        MainActivity.mPosControllerPrinter.printText("UPCA\n", fontAttribute);
        MainActivity.mPosControllerPrinter.print1dBarcode("012345678905", BixolonConst.MPOS_BARCODE_UPCA, 3, 130, BixolonConst.MPOS_ALIGNMENT_LEFT, BixolonConst.MPOS_BARCODE_TEXT_NONE);

        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_CENTER);
        MainActivity.mPosControllerPrinter.printText("UPCE\n", fontAttribute);
        MainActivity.mPosControllerPrinter.print1dBarcode("012345678905", BixolonConst.MPOS_BARCODE_UPCE, 2, 150, BixolonConst.MPOS_ALIGNMENT_CENTER, BixolonConst.MPOS_BARCODE_TEXT_ABOVE);

        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_RIGHT);
        MainActivity.mPosControllerPrinter.printText("EAN13\n", fontAttribute);
        MainActivity.mPosControllerPrinter.print1dBarcode("4711234567899", BixolonConst.MPOS_BARCODE_EAN13, 4, 170, BixolonConst.MPOS_ALIGNMENT_RIGHT, BixolonConst.MPOS_BARCODE_TEXT_BELOW);

        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_LEFT);
        MainActivity.mPosControllerPrinter.printText("EAN8\n", fontAttribute);
        MainActivity.mPosControllerPrinter.print1dBarcode("47112346", BixolonConst.MPOS_BARCODE_EAN8, 5, 190, BixolonConst.MPOS_ALIGNMENT_LEFT, BixolonConst.MPOS_BARCODE_TEXT_NONE);

        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_CENTER);
        MainActivity.mPosControllerPrinter.printText("CODE39\n", fontAttribute);
        MainActivity.mPosControllerPrinter.print1dBarcode("ANDY0123", BixolonConst.MPOS_BARCODE_CODE39, 3, 210, BixolonConst.MPOS_ALIGNMENT_CENTER, BixolonConst.MPOS_BARCODE_TEXT_ABOVE);

        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_RIGHT);
        MainActivity.mPosControllerPrinter.printText("ITF\n", fontAttribute);
        MainActivity.mPosControllerPrinter.print1dBarcode("1234567895", BixolonConst.MPOS_BARCODE_ITF, 3, 230, BixolonConst.MPOS_ALIGNMENT_RIGHT, BixolonConst.MPOS_BARCODE_TEXT_BELOW);

        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_LEFT);
        MainActivity.mPosControllerPrinter.printText("CODABAR\n", fontAttribute);
        MainActivity.mPosControllerPrinter.print1dBarcode("A1234567B", BixolonConst.MPOS_BARCODE_CODABAR, 4, 250, BixolonConst.MPOS_ALIGNMENT_LEFT, BixolonConst.MPOS_BARCODE_TEXT_NONE);

        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_CENTER);
        MainActivity.mPosControllerPrinter.printText("CODE93\n", fontAttribute);
        MainActivity.mPosControllerPrinter.print1dBarcode("12341555", BixolonConst.MPOS_BARCODE_CODE93, 2, 100, BixolonConst.MPOS_ALIGNMENT_CENTER, BixolonConst.MPOS_BARCODE_TEXT_ABOVE);

        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_RIGHT);
        MainActivity.mPosControllerPrinter.printText("CODE128\n", fontAttribute);
        MainActivity.mPosControllerPrinter.print1dBarcode("12345", BixolonConst.MPOS_BARCODE_CODE128, 3, 200, BixolonConst.MPOS_ALIGNMENT_RIGHT, BixolonConst.MPOS_BARCODE_TEXT_BELOW);

        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_LEFT);
        MainActivity.mPosControllerPrinter.printText("QrCode\n", fontAttribute);
        MainActivity.mPosControllerPrinter.printQrCode("http://www.bixolon.com", BixolonConst.MPOS_BARCODE_QRCODE_MODEL1, BixolonConst.MPOS_ALIGNMENT_LEFT, 2, BixolonConst.MPOS_QRCODE_ECC_LEVEL_L);
        MainActivity.mPosControllerPrinter.printQrCode("http://www.bixolon.com", BixolonConst.MPOS_BARCODE_QRCODE_MODEL2, BixolonConst.MPOS_ALIGNMENT_LEFT, 3, BixolonConst.MPOS_QRCODE_ECC_LEVEL_L);
        MainActivity.mPosControllerPrinter.printQrCode("http://www.bixolon.com", BixolonConst.MPOS_BARCODE_QRCODE_MODEL1, BixolonConst.MPOS_ALIGNMENT_LEFT, 4, BixolonConst.MPOS_QRCODE_ECC_LEVEL_L);
        MainActivity.mPosControllerPrinter.printQrCode("http://www.bixolon.com", BixolonConst.MPOS_BARCODE_QRCODE_MODEL2, BixolonConst.MPOS_ALIGNMENT_LEFT, 5, BixolonConst.MPOS_QRCODE_ECC_LEVEL_L);
        MainActivity.mPosControllerPrinter.printQrCode("http://www.bixolon.com", BixolonConst.MPOS_BARCODE_QRCODE_MODEL1, BixolonConst.MPOS_ALIGNMENT_LEFT, 6, BixolonConst.MPOS_QRCODE_ECC_LEVEL_L);

        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_CENTER);
        MainActivity.mPosControllerPrinter.printText("DataMatrix\n", fontAttribute);
        MainActivity.mPosControllerPrinter.printDataMatrix("Hello Sample", BixolonConst.MPOS_ALIGNMENT_CENTER, 2);
        MainActivity.mPosControllerPrinter.printDataMatrix("Hello Sample", BixolonConst.MPOS_ALIGNMENT_CENTER, 3);

        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_RIGHT);
        MainActivity.mPosControllerPrinter.printText("GS1 Databar\n", fontAttribute);
        MainActivity.mPosControllerPrinter.printGS1Databar("123456789", BixolonConst.MPOS_BARCODE_GS1_RSS14STACKEDOMNIDIRECTIONAL, BixolonConst.MPOS_ALIGNMENT_RIGHT, 2);
        MainActivity.mPosControllerPrinter.printGS1Databar("123456789", BixolonConst.MPOS_BARCODE_GS1_RSS14STACKED, BixolonConst.MPOS_ALIGNMENT_RIGHT, 3);
        MainActivity.mPosControllerPrinter.printGS1Databar("123456789", BixolonConst.MPOS_BARCODE_GS1_RSS14STACKEDOMNIDIRECTIONAL, BixolonConst.MPOS_ALIGNMENT_RIGHT, 4);

        fontAttribute.setAlignment(BixolonConst.MPOS_ALIGNMENT_LEFT);
        MainActivity.mPosControllerPrinter.printText("PDF417\n", fontAttribute);
        MainActivity.mPosControllerPrinter.printPDF417("http://www.bixolon.com", BixolonConst.MPOS_BARCODE_PDF417, BixolonConst.MPOS_ALIGNMENT_LEFT, BixolonConst.MPOS_PDF417_COLUMN_AUTO, BixolonConst.MPOS_PDF417_ROW_AUTO, 2, 4, 0x30);
        MainActivity.mPosControllerPrinter.printPDF417("http://www.bixolon.com", BixolonConst.MPOS_BARCODE_PDF417_SIMPLIFIED, BixolonConst.MPOS_ALIGNMENT_LEFT, BixolonConst.MPOS_PDF417_COLUMN_AUTO, BixolonConst.MPOS_PDF417_ROW_AUTO, 3, 5, 0x30);

        MainActivity.mPosControllerPrinter.cutPaper(BixolonConst.MPOS_PRINTER_FEEDCUT);

        MainActivity.mPosControllerPrinter.setTransaction(BixolonConst.MPOS_PRINTER_TRANSACTION_OUT);
    }

    private void printImage() {
        if (!MainActivity.mPosControllerPrinter.isOpen()) {
            Toast.makeText(getContext(), "Device not opened.", Toast.LENGTH_LONG).show();
            return;
        }

        BitmapDrawable drawable = (BitmapDrawable) getResources().getDrawable(R.drawable.bixolonlogo);
        Bitmap bitmap = drawable.getBitmap();

        MainActivity.mPosControllerPrinter.printBitmap(bitmap, 200, BixolonConst.MPOS_ALIGNMENT_LEFT, 30, true, true);
        MainActivity.mPosControllerPrinter.printBitmap(bitmap, 300, BixolonConst.MPOS_ALIGNMENT_CENTER, 50, true, true);
        MainActivity.mPosControllerPrinter.printBitmap(bitmap, 400, BixolonConst.MPOS_ALIGNMENT_RIGHT, 70, true, true);

        //MainActivity.mPosControllerPrinter.printBitmap("/sdcard/bixolonlogo.png", 200, BixolonConst.MPOS_ALIGNMENT_LEFT, 30, true, true);
        MainActivity.mPosControllerPrinter.cutPaper(BixolonConst.MPOS_PRINTER_FEEDCUT);
    }

    private void printPageMode() {
        if (!MainActivity.mPosControllerPrinter.isOpen()) {
            Toast.makeText(getContext(), "Device not opened.", Toast.LENGTH_LONG).show();
            return;
        }

        FontAttribute fontAttribute = new FontAttribute();
        fontAttribute.init();

        MainActivity.mPosControllerPrinter.setPageMode(BixolonConst.MPOS_PRINTER_PAGEMODE_IN);
        MainActivity.mPosControllerPrinter.setPagemodePrintArea(0, 0, 576, 600);
        MainActivity.mPosControllerPrinter.setPagemodeDirection(BixolonConst.MPOS_PRINTER_PD_0);
        MainActivity.mPosControllerPrinter.printText("MPOS_PRINTER_PD_0", fontAttribute);
        MainActivity.mPosControllerPrinter.setPagemodeDirection(BixolonConst.MPOS_PRINTER_PD_LEFT90);
        MainActivity.mPosControllerPrinter.printText("MPOS_PRINTER_PD_LEFT90", fontAttribute);
        MainActivity.mPosControllerPrinter.setPagemodeDirection(BixolonConst.MPOS_PRINTER_PD_RIGHT90);
        MainActivity.mPosControllerPrinter.printText("MPOS_PRINTER_PD_RIGHT90", fontAttribute);
        MainActivity.mPosControllerPrinter.setPagemodeDirection(BixolonConst.MPOS_PRINTER_PD_180);
        MainActivity.mPosControllerPrinter.printText("MPOS_PRINTER_PD_180", fontAttribute);
        MainActivity.mPosControllerPrinter.setPageMode(BixolonConst.MPOS_PRINTER_PAGEMODE_OUT);
        MainActivity.mPosControllerPrinter.cutPaper(BixolonConst.MPOS_PRINTER_FEEDCUT);

        MainActivity.mPosControllerPrinter.setPageMode(BixolonConst.MPOS_PRINTER_PAGEMODE_IN);
        MainActivity.mPosControllerPrinter.setPagemodePrintArea(0, 0, 576, 700);
        MainActivity.mPosControllerPrinter.setPagemodeDirection(BixolonConst.MPOS_PRINTER_PD_RIGHT90);

        MainActivity.mPosControllerPrinter.setPagemodePosition(10, 50);
        BitmapDrawable drawable = (BitmapDrawable) getResources().getDrawable(R.drawable.bixolonlogo);
        Bitmap bitmap = drawable.getBitmap();
        MainActivity.mPosControllerPrinter.printBitmap(bitmap, 350, BixolonConst.MPOS_ALIGNMENT_CENTER, 50, true, false);

        MainActivity.mPosControllerPrinter.setPagemodePosition(30, 110);
        String fontAttributes = EscapeSequence.Normal + EscapeSequence.Font_A + EscapeSequence.Double_high;
        MainActivity.mPosControllerPrinter.printText(fontAttributes + "mPOS print test", null);

        MainActivity.mPosControllerPrinter.setPagemodePosition(30, 130);
        fontAttributes = EscapeSequence.Normal + EscapeSequence.Font_A + EscapeSequence.Bold + EscapeSequence.Scale_1_time_horizontally + EscapeSequence.Scale_1_time_vertically;
        MainActivity.mPosControllerPrinter.printText(fontAttributes + "pagemode test", null);

        MainActivity.mPosControllerPrinter.setPagemodePosition(30, 150);
        fontAttributes = EscapeSequence.Normal + EscapeSequence.Font_A;
        MainActivity.mPosControllerPrinter.printText(fontAttributes + "rotate left 90", null);

        MainActivity.mPosControllerPrinter.setPagemodePosition(30, 190);
        MainActivity.mPosControllerPrinter.print1dBarcode("1234567890", BixolonConst.MPOS_BARCODE_CODE128, 2, 60, BixolonConst.MPOS_ALIGNMENT_LEFT, BixolonConst.MPOS_BARCODE_TEXT_BELOW);

        MainActivity.mPosControllerPrinter.setPagemodePosition(400, 150);
        MainActivity.mPosControllerPrinter.printQrCode("http://www.bixolon.com", BixolonConst.MPOS_BARCODE_QRCODE_MODEL1, BixolonConst.MPOS_ALIGNMENT_LEFT, 4, BixolonConst.MPOS_QRCODE_ECC_LEVEL_H);
        MainActivity.mPosControllerPrinter.setPageMode(BixolonConst.MPOS_PRINTER_PAGEMODE_OUT);
        MainActivity.mPosControllerPrinter.cutPaper(BixolonConst.MPOS_PRINTER_FEEDCUT);
    }

    private void printSampleReceipt() {
        if (!MainActivity.mPosControllerPrinter.isOpen()) {
            Toast.makeText(getContext(), "Device not opened.", Toast.LENGTH_LONG).show();
            return;
        }

        String data = "";
        data = "Comp: TICKET\n" +
                "Walton, KT12 3BS\n" +
                "Tel: 01932 901 155\n" +
                "123-456-789\n" +
                "VAT No. 123456789\n\n";

        String fontAttributes = "";
        MainActivity.mPosControllerPrinter.setTransaction(BixolonConst.MPOS_PRINTER_TRANSACTION_IN);

        fontAttributes = EscapeSequence.Normal + EscapeSequence.Center + EscapeSequence.Bold + EscapeSequence.Underline + EscapeSequence.Double_high_and_wide;
        MainActivity.mPosControllerPrinter.printText(fontAttributes + "TICKET\n\n", null);
        MainActivity.mPosControllerPrinter.printQrCode("http://www.bixolon.com", BixolonConst.MPOS_BARCODE_QRCODE_MODEL1, BixolonConst.MPOS_ALIGNMENT_CENTER, 8, BixolonConst.MPOS_QRCODE_ECC_LEVEL_H);
        fontAttributes = EscapeSequence.Normal + EscapeSequence.Center + EscapeSequence.Bold;
        MainActivity.mPosControllerPrinter.printText(fontAttributes + data, null);
        fontAttributes = EscapeSequence.Normal + EscapeSequence.Left_justify + EscapeSequence.Bold;
        MainActivity.mPosControllerPrinter.printText(fontAttributes + "Sale:       " + "19-05-2017 16:19:43\n", null);

        data = "Gate:       " + "Xcover kiosk\n" +
                "Operator:   " + "Rob\n" +
                "Order Code: " + "263036991\n";
        fontAttributes = EscapeSequence.Normal + EscapeSequence.Left_justify;
        MainActivity.mPosControllerPrinter.printText(fontAttributes + data, null);
        fontAttributes = EscapeSequence.Normal + EscapeSequence.Left_justify + EscapeSequence.Underline;
        MainActivity.mPosControllerPrinter.printText(fontAttributes + "Qty Price  Item     Total\n", null);
        fontAttributes = EscapeSequence.Normal + EscapeSequence.Left_justify;
        MainActivity.mPosControllerPrinter.printText(fontAttributes + " 1  $8.00  PARKING  $8.00\n", null);

        data = "Total (inc VAT):  " + "  $8.00\n" +
                "VAT amount (20%): " + "  $1.33\n" +
                "CARD payment:     " + "  $8.00\n" +
                "Change due:       " + "  $0.00\n\n";
        fontAttributes = EscapeSequence.Normal + EscapeSequence.Right_justify;
        MainActivity.mPosControllerPrinter.printText(fontAttributes + data, null);

        data = "Thank you for your purchase!\n" +
                "Enjoy the show!\n" +
                "Next year visit\n" +
                "www.bixolon.com\n" +
                "to buy discounted tickets.\n";
        fontAttributes = EscapeSequence.Normal + EscapeSequence.Center + EscapeSequence.Bold;
        MainActivity.mPosControllerPrinter.printText(fontAttributes + data, null);

        MainActivity.mPosControllerPrinter.cutPaper(BixolonConst.MPOS_PRINTER_FEEDCUT);
        MainActivity.mPosControllerPrinter.setTransaction(BixolonConst.MPOS_PRINTER_TRANSACTION_OUT);
    }

    private void printPDF() {
        if (!MainActivity.mPosControllerPrinter.isOpen()) {
            Toast.makeText(getContext(), "Device not opened.", Toast.LENGTH_LONG).show();
            return;
        }

        if (BixolonConst.MPOS_SUCCESS == MainActivity.mPosControllerPrinter.setPDFLicenseKey("PDF_STD_01234567")) {
            String pdfFile = "file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.pdf";
            Uri uri = Uri.parse(pdfFile);
            MainActivity.mPosControllerPrinter.printPDFFile(uri, 576, 0, 0, 0, 128, 1, 1);
        }
    }
}
