package cordova.plugin.netcontroll.integration;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.content.Context;

public class NetControllCordovaPluginsIntegration extends CordovaPlugin {
    //Elgin
    public cordova.plugin.netcontroll.integration.ElginM10Terminal elginM10Terminal;
    public cordova.plugin.netcontroll.integration.ElginSmartPay elginSmartPay;

    //Rede
    public cordova.plugin.netcontroll.integration.RedeGPos700 redeGPos700;
    public cordova.plugin.netcontroll.integration.RedeL400 redeL400;

    //SafraPay
    public cordova.plugin.netcontroll.integration.SafraPayIngenico safraPayIngenico;

    //TecToy
    public cordova.plugin.netcontroll.integration.TecToySumniD2Mini tecToySumniD2Mini;

    //Gertec
    public cordova.plugin.netcontroll.integration.GertecSK210 gertecSK210;

    //Vero
    public cordova.plugin.netcontroll.integration.VeroPOS veroPOS;

    //POStech
    public cordova.plugin.netcontroll.integration.POStech POStech;


    //Utils
    // public UtilsImageStore utilsImageStore;

    //Cordova/Java Params
    private Activity mActivity;
    private Context mContext;
    private CordovaInterface mCordova;
    private CordovaWebView webView;

    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);

        this.webView = webView;
        mActivity = cordova.getActivity();
        mContext = cordova.getActivity().getApplicationContext();
        mCordova = cordova;
    }

    private void initializeElginM10Terminal() {
        if (this.elginM10Terminal == null) {
            this.elginM10Terminal = new cordova.plugin.netcontroll.integration.ElginM10Terminal();
            this.elginM10Terminal.initialize(this.cordova, this.webView);
        }
    }

    private void initializeElginSmartPay() {
        if (this.elginSmartPay == null) {
            this.elginSmartPay = new cordova.plugin.netcontroll.integration.ElginSmartPay();
            this.elginSmartPay.initialize(this.cordova, this.webView);
        }
    }

    private void initializeRedeGpos700() {
        if (this.redeGPos700 == null) {
            this.redeGPos700 = new cordova.plugin.netcontroll.integration.RedeGPos700();
            this.redeGPos700.initialize(this.cordova, this.webView);
        }
    }

    private void initializeRedeL400() {
        if (this.redeL400 == null) {
            this.redeL400 = new cordova.plugin.netcontroll.integration.RedeL400();
            this.redeL400.initialize(this.cordova, this.webView);
        }
    }

    private void initializeSafraPayIngenico() {
        if (this.safraPayIngenico == null) {
            this.safraPayIngenico = new cordova.plugin.netcontroll.integration.SafraPayIngenico();
            this.safraPayIngenico.initialize(this.cordova, this.webView);
        }
    }

    private void initializeTecToySumniD2Mini() {
        if (this.tecToySumniD2Mini == null) {
            this.tecToySumniD2Mini = new cordova.plugin.netcontroll.integration.TecToySumniD2Mini();
            this.tecToySumniD2Mini.initialize(this.cordova, this.webView);
        }
    }

    private void initializeGertecSK210() {
        if (this.gertecSK210 == null) {
            this.gertecSK210 = new cordova.plugin.netcontroll.integration.GertecSK210();
            this.gertecSK210.initialize(this.cordova, this.webView);
        }
    }

    private void initializeVeroPOS() {
        if (this.veroPOS == null) {
            this.veroPOS = new cordova.plugin.netcontroll.integration.VeroPOS();
            this.veroPOS.initialize(this.cordova, this.webView);
        }
    }

    private void initializePOStech(){
        if (this.POStech == null) {
            this.POStech = new cordova.plugin.netcontroll.integration.POStech();
            this.POStech.initialize(this.cordova, this.webView);
        }
    }

    // private void initializeUtilsImageStore() {
    //     if (this.utilsImageStore == null) {
    //         this.utilsImageStore = new UtilsImageStore();
    //         this.utilsImageStore.initialize(this.cordova, this.webView);
    //     }
    // }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        try {
            String result = "Sem resultado";
            String[] actionProviderList = action.split("\\.");
            String provider = actionProviderList[0];

            switch (provider.toLowerCase()) {
                case "elgin":
                    String elginProduct = actionProviderList[1];
                    switch (elginProduct.toLowerCase()) {
                        case "m10terminal":
                            this.initializeElginM10Terminal();
                            String elginAction = actionProviderList[2];
                            boolean resultElginExecute = this.elginM10Terminal.execute(elginAction, args, callbackContext);
                            return resultElginExecute;
                        case "elginsmartpay":
                            this.initializeElginSmartPay();
                            String elginSmartPayAction = actionProviderList[2];
                            boolean resultElginSmartPayExecute = this.elginSmartPay.execute(elginSmartPayAction, args, callbackContext);
                            return resultElginSmartPayExecute;
                        default:
                            break;
                    }
                case "rede":
                    String redeProduct = actionProviderList[1];
                    switch (redeProduct.toLowerCase()) {
                        case "gpos700":
                            this.initializeRedeGpos700();
                            String redeGPos700Action = actionProviderList[2];
                            boolean resultRedeGpos700Execute = this.redeGPos700.execute(redeGPos700Action, args, callbackContext);
                            return resultRedeGpos700Execute;
                        case "l400":
                            this.initializeRedeL400();
                            String redeL400Action = actionProviderList[2];
                            boolean resultRedeL400Execute = this.redeL400.execute(redeL400Action, args, callbackContext);
                            return resultRedeL400Execute;
                        default:
                            break;
                    }
                case "safra":
                    String safraProduct = actionProviderList[1];
                    switch (safraProduct.toLowerCase()) {
                        case "safrapayingenico":
                            this.initializeSafraPayIngenico();
                            String safraAction = actionProviderList[2];
                            boolean resultSafraExecute = this.safraPayIngenico.execute(safraAction, args, callbackContext);
                            return resultSafraExecute;
                        default:
                            break;
                    }
                case "tectoy":
                    String tecToyProduct = actionProviderList[1];
                    switch (tecToyProduct.toLowerCase()) {
                        case "tectoysumnid2mini":
                            this.initializeTecToySumniD2Mini();
                            String tecToyAction = actionProviderList[2];
                            boolean resultTecToyExecute = this.tecToySumniD2Mini.execute(tecToyAction, args, callbackContext);
                            return resultTecToyExecute;
                        default:
                            break;
                    }
                case "gertec":
                    String gertecProduct = actionProviderList[1];
                    switch (gertecProduct.toLowerCase()) {
                        case "gertecsk210":
                            this.initializeGertecSK210();
                            String gertecAction = actionProviderList[2];
                            boolean resultGertecExecute = this.gertecSK210.execute(gertecAction, args, callbackContext);
                            return resultGertecExecute;
                        default:
                            break;
                    }
                case "vero":
                    String veroProduct = actionProviderList[1];
                    switch (veroProduct.toLowerCase()) {
                        case "veropos":
                            this.initializeVeroPOS();
                            String veroAction = actionProviderList[2];
                            boolean resultVeroPosExecute = this.veroPOS.execute(veroAction, args, callbackContext);
                            return resultVeroPosExecute;
                        default:
                            break;
                    }
                case "postech":
                    String postechProduct = actionProviderList[1];
                    switch (postechProduct.toLowerCase()) {
                        case "postech":
                            this.initializePOStech();
                            String postechAction = actionProviderList[2];
                            boolean resultPOStechExecute = this.POStech.execute(postechAction, args, callbackContext);
                            return resultPOStechExecute;
                        default:
                            break;
                    }
                // case "utils":
                //     String utilsProduct = actionProviderList[1];
                //     switch (utilsProduct.toLowerCase()) {
                //         case "utilsimagestore":
                //             this.initializeUtilsImageStore();
                //             String utilsimagestoreAction = actionProviderList[2];
                //             boolean resultUtilsImageStoreExecute = this.utilsImageStore.execute(utilsimagestoreAction, args, callbackContext);
                //             return resultUtilsImageStoreExecute;
                //         default:
                //             break;
                //     }
                default:
                    break;
            }
            callbackContext.success(result);
            return true;
        } catch (Exception e) {
            callbackContext.error("NetControllCordovaPluginsIntegration excute error: " + e.toString());
        }
        return false;
    }
}
