package org.apache.cordova.USBPrinter;

import android.app.Activity;
import android.Manifest;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.widget.Toast;
import java.util.HashMap;
import java.util.Iterator;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Created by LuoWen on 2015/10/27.
 */
public class USBPrinter extends CordovaPlugin {
    UsbManager manager;
    HashMap<String, UsbDevice> deviceList;
    Iterator<UsbDevice> deviceIterator;
    UsbDevice usbDevice;
    String message;
    UsbPrinterUtil mUtil;
    PrinterUtil printerUtil;
    CallbackContext callbackContext;

    public void initPrintUtil () {
      try {
        this.printerUtil = new PrinterUtil(this.cordova.getActivity(), usbDevice, this.callbackContext);
      } catch (IOException e) {}
    }

    class InitPrintUtil implements UsbPrinterUtil.OnUsbPermissionCallback {
      public void onUsbPermissionEvent(UsbDevice dev, boolean granted) {
        try {
            USBPrinter.this.printerUtil = new PrinterUtil(USBPrinter.this.cordova.getActivity(), USBPrinter.this.usbDevice, USBPrinter.this.callbackContext);
        } catch (IOException e) {}
      }
    }

    public boolean InitPrinter (CallbackContext callbackContext) {
      this.manager = (UsbManager)this.cordova.getActivity().getSystemService(Context.USB_SERVICE);
      this.mUtil = new UsbPrinterUtil(this.cordova.getActivity());
      this.deviceList = manager.getDeviceList();
      this.deviceIterator = deviceList.values().iterator();

      do {
        this.usbDevice = deviceIterator.next();
      } while (!mUtil.isUsbPrinterDevice(this.usbDevice) && deviceIterator.hasNext());

      if (this.manager.hasPermission(this.usbDevice)) {
        this.initPrintUtil();
      } else {
        this.message = usbDevice.getDeviceName();
        InitPrintUtil callback = new InitPrintUtil();
        this.mUtil.requestPermission(usbDevice, new InitPrintUtil());
      }
      return true;
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
            throws JSONException {
        this.callbackContext = callbackContext;

        if (action.equals("showPrinterList")) {
            callbackContext.success(message);
        } else if (action.equals("print")) {
            try {
              this.printerUtil.printString(args.getString(0));
              return true;
            } catch (UnsupportedEncodingException e) {
              return false;
            }
        } else if (action.equals("init")) {
            InitPrinter(callbackContext);
            return true;
        } else if (action.equals("reset")) {
             this.printerUtil.resetInit();
             return true;
        } else if (action.equals("changeLine")) {
            this.printerUtil.changeLine();
            return true;
        } else if (action.equals("selectAlignment")) {
            switch (args.getString(0)) {
              case "left":
                this.printerUtil.selectAlignment(PrinterUtil.ALIGNMENT.LEFT);
                break;
              case "center":
                this.printerUtil.selectAlignment(PrinterUtil.ALIGNMENT.CENTER);
                break;
              case "right":
                this.printerUtil.selectAlignment(PrinterUtil.ALIGNMENT.RIGHT);
                break;
              default:
                this.printerUtil.selectAlignment(PrinterUtil.ALIGNMENT.CENTER);
              }
              return true;
        } else if (action.equals("selectBold")) {
            this.printerUtil.selectBold (args.getBoolean(0));
            return true;
        } else if (action.equals("doubleFontSize")) {
            this.printerUtil.doubleFontSize (args.getBoolean(0), args.getBoolean(1));
            return true;
        } else if (action.equals("cutPaper")) {
            this.printerUtil.cutPaper();
            return true;
        } else if (action.equals("walkPaper")) {
            this.printerUtil.walkPaper(args.getInt(0));
            return true;
        } else if (action.equals("setLineSpace")) {
            this.printerUtil.setLineSpace(args.getInt(0));
            return true;
        } else if (action.equals("zoomFontSize")) {
            this.printerUtil.zoomFontSize(args.getInt(0), args.getInt(1));
            return true;
        } else if (action.equals("clear")) {
            this.printerUtil.clear();
            return true;
        } else if (action.equals("send")) {
            try {
              this.printerUtil.send();
              return true;
            } catch (IOException e) {
              return false;
            }
        } else if (action.equals("printQRCode")) {
            this.printerUtil.printQRCode(args.getString(0), args.getInt(1), args.getBoolean(2));
            return true;
        } else if (action.equals("reversePrint")) {
            this.printerUtil.reversePrint();
            return true;
        }
        return false;
    }

}
