package com.siberia.plugin;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;

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

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import android.net.nsd.NsdServiceInfo;

import com.siberia.plugin.NsdHelper;

public class Discovery extends CordovaPlugin {

  public static final String TAG = "Discovery";

  NsdHelper mNsdHelper;
  private Handler mHandler;
  // ChatConnection mConnection;
  // CallbackContextHandler ccHandler;

  @Override
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    String serviceName = args.getString(0);
    String serviceType = args.getString(1);
    mNsdHelper = new NsdHelper(cordova.getActivity(), callbackContext, serviceName);
    if (action.equals("initChat")) {
      Log.d(TAG, "initChat");
      // this.initChat(callbackContext);
    }

    else if (action.equals("advertizeChat")) {
      Log.d(TAG, "advertizeChat");
      // this.advertizeChat(callbackContext);
    }

    else if (action.equals("identify")) {
      Log.d(TAG, "identify");
      mNsdHelper.discoverServices(serviceType);
    }

    else if (action.equals("connectChat")) {
      Log.d(TAG, "connectChat");
      // this.connectChat(callbackContext);
    }

    else if (action.equals("sendChatMessage")) {
      Log.d(TAG, "sendChatMessage");
      // String messageString = args.getString(0);
      // this.sendChatMessage(callbackContext, messageString);
    }

    else {
      callbackContext.error(String.format("Discovery - invalid action:", action));
      return false;
    }

    // PluginResult.Status status = PluginResult.Status.NO_RESULT;
    // PluginResult pluginResult = new PluginResult(status);
    // pluginResult.setKeepCallback(true);
    // callbackContext.sendPluginResult(pluginResult);
    return true;
  }

  // private void initChat(CallbackContext callbackContext) {
  //   final CallbackContext cbc = callbackContext;
  //   try {
  //     mHandler = new Handler() {
        
  //       @Override
  //       public void handleMessage(Message msg) {
  //         String type = msg.getData().getString("type");
  //         String message = msg.getData().getString("msg");
  //         JSONObject data = new JSONObject();
          
  //         try {
  //           data.put("type", new String(type));
  //           data.put("data", new String(message));
  //         } catch(JSONException e) {
  //         }

  //         PluginResult result = new PluginResult(PluginResult.Status.OK, data);
  //         result.setKeepCallback(true);
  //         cbc.sendPluginResult(result);
  //       }
  //     };

  //     // mConnection = new ChatConnection(ccHandler);
  //     mConnection = new ChatConnection(mHandler);

  //     // mNsdHelper = new NsdHelper(cordova.getActivity(), ccHandler);
  //     mNsdHelper = new NsdHelper(cordova.getActivity(), mHandler);
  //     mNsdHelper.initializeNsd();

  //   } catch(Exception e) {
  //     callbackContext.error("Error " + e);
  //   }
  // }

  // private void advertizeChat(CallbackContext callbackContext) {
  //   final CallbackContext cbc = callbackContext;

  //   if(mConnection.getLocalPort() > -1) {
  //     mNsdHelper.registerService(mConnection.getLocalPort());
  //   } else {
  //     cbc.error("ServerSocket isn't bound.");
  //   }
  // }

  // private void identify(CallbackContext callbackContext) {
  //   final CallbackContext cbc = callbackContext;
  //   mNsdHelper.discoverServices();
  // }


  // private void connectChat(CallbackContext callbackContext) {
  //   final CallbackContext cbc = callbackContext;
  //   NsdServiceInfo service = mNsdHelper.getChosenServiceInfo();
  //   if (service != null) {
  //     mConnection.connectToServer(service.getHost(),
  //     service.getPort());
  //   } else {
  //     cbc.error("No service to connect to!");
  //   }
  // }

  // private void sendChatMessage(CallbackContext callbackContext, String messageString) {
  //   mConnection.sendMessage(messageString);
  // }

}

@@ -209,192 +209,32 @@
 on;%0A
-import org.json.JSONObject;%0A%0Aimport android.os.Handler;%0Aimport android.os.Message;%0Aimport android.util.Log;%0A%0Aimport android.net.nsd.NsdServiceInfo;%0A%0Aimport com.siberia.plugin.NsdHelper
+%0Aimport android.util.Log
 ;%0A%0Ap
@@ -356,108 +356,8 @@
 per;
-%0A  private Handler mHandler;%0A  // ChatConnection mConnection;%0A  // CallbackContextHandler ccHandler;
 %0A%0A  
@@ -1737,2194 +1737,6 @@
  %7D%0A%0A
-  // private void initChat(CallbackContext callbackContext) %7B%0A  //   final CallbackContext cbc = callbackContext;%0A  //   try %7B%0A  //     mHandler = new Handler() %7B%0A        %0A  //       @Override%0A  //       public void handleMessage(Message msg) %7B%0A  //         String type = msg.getData().getString(%22type%22);%0A  //         String message = msg.getData().getString(%22msg%22);%0A  //         JSONObject data = new JSONObject();%0A          %0A  //         try %7B%0A  //           data.put(%22type%22, new String(type));%0A  //           data.put(%22data%22, new String(message));%0A  //         %7D catch(JSONException e) %7B%0A  //         %7D%0A%0A  //         PluginResult result = new PluginResult(PluginResult.Status.OK, data);%0A  //         result.setKeepCallback(true);%0A  //         cbc.sendPluginResult(result);%0A  //       %7D%0A  //     %7D;%0A%0A  //     // mConnection = new ChatConnection(ccHandler);%0A  //     mConnection = new ChatConnection(mHandler);%0A%0A  //     // mNsdHelper = new NsdHelper(cordova.getActivity(), ccHandler);%0A  //     mNsdHelper = new NsdHelper(cordova.getActivity(), mHandler);%0A  //     mNsdHelper.initializeNsd();%0A%0A  //   %7D catch(Exception e) %7B%0A  //     callbackContext.error(%22Error %22 + e);%0A  //   %7D%0A  // %7D%0A%0A  // private void advertizeChat(CallbackContext callbackContext) %7B%0A  //   final CallbackContext cbc = callbackContext;%0A%0A  //   if(mConnection.getLocalPort() %3E -1) %7B%0A  //     mNsdHelper.registerService(mConnection.getLocalPort());%0A  //   %7D else %7B%0A  //     cbc.error(%22ServerSocket isn't bound.%22);%0A  //   %7D%0A  // %7D%0A%0A  // private void identify(CallbackContext callbackContext) %7B%0A  //   final CallbackContext cbc = callbackContext;%0A  //   mNsdHelper.discoverServices();%0A  // %7D%0A%0A%0A  // private void connectChat(CallbackContext callbackContext) %7B%0A  //   final CallbackContext cbc = callbackContext;%0A  //   NsdServiceInfo service = mNsdHelper.getChosenServiceInfo();%0A  //   if (service != null) %7B%0A  //     mConnection.connectToServer(service.getHost(),%0A  //     service.getPort());%0A  //   %7D else %7B%0A  //     cbc.error(%22No service to connect to!%22);%0A  //   %7D%0A  // %7D%0A%0A  // private void sendChatMessage(CallbackContext callbackContext, String messageString) %7B%0A  //   mConnection.sendMessage(messageString);%0A  // %7D%0A%0A
 %7D%0A

Remove unused imports and commented out code