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.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

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

public class LabelFragment extends Fragment implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private RadioGroup radioGroupPortType;

    private CheckBox checkBoxBypassMode = null;

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

    private int portType = BixolonConst.MPOS_INTERFACE_BLUETOOTH;

    public static LabelFragment newInstance() {
        LabelFragment fragment = new LabelFragment();
        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_label, container, false);

        checkBoxBypassMode = view.findViewById(R.id.checkBoxBypassMode);

        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.btnRFID).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:
                MainActivity.mPosControllerLabelPrinter.checkPrinterStatus();
                break;
            case R.id.btnText:
                printText();
                break;
            case R.id.btnBarcode:
                printBarcode();
                break;
            case R.id.btnImage:
                printImage();
                break;
            case R.id.btnRFID:
                testRFID();
            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;
        }
    }

    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.mPosControllerLabelPrinter.selectCommandMode(isBypass ? BixolonConst.MPOS_COMMAND_MODE_BYPASS : BixolonConst.MPOS_COMMAND_MODE_DEFAULT);
        MainActivity.mPosControllerLabelPrinter.selectInterface(portType, strAddress);
        long ret = MainActivity.mPosControllerLabelPrinter.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.mPosControllerLabelPrinter.isOpen()) {
            MainActivity.mPosControllerLabelPrinter.closeService(0);
        }
    }

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

        // DeviceFont
        MainActivity.mPosControllerLabelPrinter.drawTextDeviceFont("Bixolon Device Font", 50, 50, '1', 1, 1, 0, 0, false, false, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextDeviceFont("Bixolon Device Font", 50, 100, '2', 1, 1, 0, 0, true, false, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextDeviceFont("Bixolon Device Font", 50, 150, '3', 1, 1, 0, 0, false, true, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextDeviceFont("Bixolon Device Font", 50, 200, '4', 1, 1, 0, 0, false, false, true, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextDeviceFont("빅솔론 디바이스 폰트", 50, 250, 'a', 1, 1, 0, 0, false, false, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextDeviceFont("빅솔론 디바이스 폰트", 50, 300, 'b', 1, 1, 0, 0, false, false, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextDeviceFont("빅솔론 디바이스 폰트", 50, 350, 'e', 1, 1, 0, 0, false, false, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextDeviceFont("빅솔론 디바이스 폰트", 50, 400, 'f', 1, 1, 0, 0, false, false, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.printBuffer(1);

        // VectorFont
        MainActivity.mPosControllerLabelPrinter.drawTextVectorFont("Bixolon Vector Font", 50, 50, 'U', 25, 25, 0, 0, false, false, false, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextVectorFont("Bixolon Vector Font", 50, 100, 'U', 25, 25, 0, 0, true, false, false, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextVectorFont("Bixolon Vector Font", 50, 150, 'U', 25, 25, 0, 0, false, true, false, true, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextVectorFont("Bixolon Vector Font", 50, 200, 'U', 25, 25, 0, 0, false, false, true, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextVectorFont("빅솔론 벡터 폰트", 50, 250, 'K', 25, 25, 0, 0, false, false, false, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextVectorFont("빅솔론 벡터 폰트", 50, 300, 'K', 25, 25, 0, 0, false, false, false, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextVectorFont("빅솔론 벡터 폰트", 50, 350, 'K', 25, 25, 0, 0, false, false, false, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextVectorFont("빅솔론 벡터 폰트", 50, 400, 'K', 25, 25, 0, 0, false, false, false, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.printBuffer(1);
    }

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

        MainActivity.mPosControllerLabelPrinter.drawBarcode1D(">C1234567890", 50, 50, 0, 2, 6, 100, 1, 0, 0);
        MainActivity.mPosControllerLabelPrinter.drawBarcodePDF417("PDF417CODE", 50, 200, 30, 5, 0, 0, true, 1, 3, 15, 0);
        MainActivity.mPosControllerLabelPrinter.drawBarcodeQRCode("QRCODE", 50, 350, 4, 2, 'L', 0);
        MainActivity.mPosControllerLabelPrinter.drawBarcodeDataMatrix("DataMatrix", 50, 500, 4, false, 0);
        MainActivity.mPosControllerLabelPrinter.printBuffer(1);
    }

    private void printImage() {
        if (!MainActivity.mPosControllerLabelPrinter.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.mPosControllerLabelPrinter.drawImage(bitmap, 50, 50, 200, 30, true);
        MainActivity.mPosControllerLabelPrinter.drawImage(bitmap, 50, 200, 300, 50, true);
        MainActivity.mPosControllerLabelPrinter.drawImage(bitmap, 50, 350, 400, 70, true);
        MainActivity.mPosControllerLabelPrinter.printBuffer(1);
    }

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

        MainActivity.mPosControllerLabelPrinter.drawTextDeviceFont("RFID Print Test 1", 56, 32, '4', 1, 1, 0, 0, false, true, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.drawTextDeviceFont("RFID Data : A B C D E F G H I J K L", 57, 81, '2', 1, 1, 0, 0, false, false, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.writeRFID(BixolonConst.MPOS_LABEL_RFID_DATA_TYPE_A, 4, 12, "ABCDEFGHIJKL");
        MainActivity.mPosControllerLabelPrinter.drawTextDeviceFont("* Complete *", 59, 200, '4', 1, 1, 0, 0, false, true, false, BixolonConst.MPOS_LABEL_ALIGNMENT_LEFT);
        MainActivity.mPosControllerLabelPrinter.printBuffer(1);
    }

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

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