//package com.medstudy.cordova.bcplayerplugin;
package net.nopattern.cordova.brightcoveplayer;

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

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

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
import android.util.Patterns;
import android.os.Message;
import android.os.Messenger;
import android.view.View;
import android.content.ComponentName;
import android.content.pm.PackageManager;

public class BCPlayerPlugin extends CordovaPlugin {
  private CordovaWebView appView;
  private PluginReceiver receiver;
  private String token = "";
  private String rid = null;

  static final String PLUGIN_CMD = "PLUGIN_CMD";

  @Override
  public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);
    appView = webView;
    receiver = new PluginReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BCPlayerActivity.ACTIVITY_EVENT);
    cordova.getActivity().getApplicationContext().registerReceiver(receiver, intentFilter);
    BCVideoRetriever.setContext(appView.getView().getContext());
  }

  @Override
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if(action.equals("init")) {
      Context context = this.cordova.getActivity().getApplicationContext();
      //enableComponent(context, BCPlayerActivity.class, true);
      token = args.getString(0);
      this.init(callbackContext, "no", "no", "true");
      return true;
    } else if (action.equals("enable")) {
      String resume = args.getString(0);
      this.init(callbackContext, "yes", "no", resume);
      //sendCommand(Cmd.ENABLE, "", "", "", "");
      //Context context = this.cordova.getActivity().getApplicationContext();
      //enableComponent(context, BCPlayerActivity.class, true);
      return true;
    } else if (action.equals("enable-from-notes")) {
      String resume = args.getString(0);
      this.init(callbackContext, "yes", "yes", resume);
      return true;
    } else if (action.equals("disable")) {
      sendCommand(Cmd.DISABLE, "", "", "", "");
      //Context context = this.cordova.getActivity().getApplicationContext();
      //enableComponent(context, BCPlayerActivity.class, false);
      return true;
    } else if (action.equals("load")) {
      rid = args.getString(0);
      sendCommand(Cmd.LOAD, rid, "", "", "");
      return true;
    } else if (action.equals("hide")) {
      sendCommand(Cmd.HIDE, "", "", "", "");
      return true;
    } else if (action.equals("show")) {
      sendCommand(Cmd.SHOW, "", "", "", "");
      return true;
    } else if (action.equals("pause")) {
      sendCommand(Cmd.PAUSE, "", "", "", "");
      return true;
    } else if (action.equals("play")) {
      sendCommand(Cmd.PLAY, "", "", "", "");
      return true;
    } else if (action.equals("seek")) {
      String location = args.getString(0);
      sendCommand(Cmd.SEEK, location, "", "", "");
      return true;
    } else if(action.equals("reposition")) {
      String y = args.getString(1);
      String width = args.getString(2);
      String height = args.getString(3);
      sendCommand(Cmd.REPOSITION, "0", y, width, height);
      return true;
    }

    return false;
  }

  private void sendCommand(Cmd cmd, String data1, String data2, String data3, String data4){
    Log.d("BCPluginActivity", cmd.name());

    Intent intent = new Intent();
    intent.setAction(PLUGIN_CMD);
    intent.putExtra("EXTRA_CMD", cmd.name());
    intent.putExtra("EXTRA_DATA1", data1);
    intent.putExtra("EXTRA_DATA2", data2);
    intent.putExtra("EXTRA_DATA3", data3);
    intent.putExtra("EXTRA_DATA4", data4);
    appView.getView().getContext().sendBroadcast(intent);
  }

  private void enableComponent(Context context, Class<?> componentClass,
                               boolean isEnable) {
    int enableFlag = isEnable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    context.getPackageManager().setComponentEnabledSetting(
            new ComponentName(context, componentClass),
            enableFlag, PackageManager.DONT_KILL_APP);
  }

  private void init(CallbackContext callbackContext, String restart, String fromNotes, String resume) {
    if (token != null && token.length() > 0){
      Context context = this.cordova.getActivity().getApplicationContext();
      Intent intent = new Intent(context, BCPlayerActivity.class);
      intent.putExtra("brightcove-token", token);
      intent.putExtra("brightcove-restart", restart);
      intent.putExtra("brightcove-from-notes", fromNotes);
      intent.putExtra("brightcove-rid", rid);
      intent.putExtra("brightcove-resume", resume);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(intent);

      Intent intentSerivce = new Intent(context, BCPlayerService.class);
      intent.putExtra("brightcove-token", token);
      context.startService(intentSerivce);

      callbackContext.success("Brightcove inited");
    } else{
      callbackContext.error("Empty Brightcove token!");
    }
  }

  private class PluginReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context arg0, Intent arg1) {
      String orgData = arg1.getStringExtra("DATA_BACK");
      String duration = arg1.getStringExtra("DURATION");
      String position = arg1.getStringExtra("POSITION");
      appView.sendJavascript("cordova.fireWindowEvent('" + orgData + "', { 'duration':" + duration + ", 'currentTime': " + position + "})");
    }
  }
}
@@ -936,16 +936,58 @@
  = null;
+%0A  private boolean serviceStarted = false;
 %0A%0A  stat
@@ -5212,24 +5212,62 @@
 y(intent);%0A%0A
+      if(serviceStarted == false) %7B%0A  
       Intent
@@ -5325,32 +5325,34 @@
 e.class);%0A      
+  
 intent.putExtra(
@@ -5377,32 +5377,34 @@
 , token);%0A      
+ 
+ 
 context.startSer
@@ -5423,16 +5423,55 @@
 erivce);
+%0A        serviceStarted = true;%0A      %7D
 %0A%0A      

Create only one background service