package com.iotize.plugin.cordova.zebrarfid;

import android.util.Log;

import androidx.annotation.NonNull;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;

public class ZebraRfid extends CordovaPlugin {

    private static final String TAG = "ZebraRfid";

    @NonNull
    private ZebraRfidAdapter adapter;

    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        this.adapter = new ZebraRfidAdapter(cordova.getContext());
    }

    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) {
        PluginResponse pluginResponse = new PluginResponse(
                action,
                callbackContext
        );
        try {
            ArgsHelper argsHelper = ArgsHelper.fromCordovaCallParameters(args);
            Log.d(TAG, "RFID action \"" + action + "\" with args: " + args);
            if (action.equals("connect")) {
                this.adapter.connect(
                    pluginResponse
                );
                return true;
            }  
            
            if (action.equals("disconnect")) {
                this.adapter.disconnect(
                    pluginResponse
                );
                return true;
            }

            if (action.equals("startScan")) {
                this.adapter.startScan(
                        pluginResponse
                );
                return true;
            }

            if (action.equals("scan")) {
                this.adapter.scan(
                        pluginResponse
                );
                return true;
            }

            if (action.equals("stopScan")) {
                this.adapter.stopScan(
                        pluginResponse
                );
                return true;
            }

            if (action.equals("getTags")) {
                this.adapter.getTags(
                        pluginResponse
                );
                return true;
            }

            if (action.equals("read")) {
                this.adapter.read(
                    pluginResponse,
                    argsHelper.getString("id"), 
                    argsHelper.getInt("offset"), 
                    argsHelper.getInt("count"), 
                    argsHelper.getMemoryBank("memoryBank"),
                    argsHelper.getPassword("password")
                );
                return true;
            }
            if (action.equals("write")) {
                this.adapter.write(
                    pluginResponse, 
                    argsHelper.getString("id"), 
                    argsHelper.getString("data"),
                    argsHelper.getInt("offset"), 
                    argsHelper.getMemoryBank("memoryBank"), 
                    argsHelper.getInt("writeRetries"),
                    argsHelper.getPassword("password")
                );
                return true;
            }

            if (action.equals("writeAccessPassword")) {
                this.adapter.writeAccessPassword(
                        pluginResponse,
                        argsHelper.getString("id"),
                        argsHelper.getPassword("password"),
                        argsHelper.getString("newPassword")
                );
                return true;
            }

            if (action.equals("writeKillPassword")) {
                this.adapter.writeKillPassword(
                        pluginResponse,
                        argsHelper.getString("id"),
                        argsHelper.getPassword("password"),
                        argsHelper.getString("newPassword")
                );
                return true;
            }

            if (action.equals("writeLockSettings")) {
                this.adapter.writeLockSettings(
                    pluginResponse, 
                    argsHelper.getString("id"), 
                    argsHelper.getLockDataField("dataField"), 
                    argsHelper.getLockPrivilege("privilege"),
                        argsHelper.getPassword("password")
                );
                return true;
            }
            throw PluginError.illegalAction(action);
        } catch (Throwable err) {
            pluginResponse.error(
                PluginError.fromError(err)
            );
            return false;
        }
    }

}