/**
   * Sends the registration ID to your server over HTTP, so it can use GCM/HTTP or CCS to send
   * messages to your app. Not needed for this demo since the device sends upstream messages to a
   * server that echoes back the message using the 'from' address in the message.
   */
  private void sendRegistrationIdToBackend(String pn_id) {
    // Your implementation here.
    CoupersObject obj = new CoupersObject(CoupersData.Methods.SAVE_PUSH_NOTIFICATION_ID);
    obj.addParameter(CoupersData.Parameters.USER_ID, app.getUser_id());
    obj.addParameter(CoupersData.Parameters.GCM_ID, pn_id);

    String _tag[] = {Fields.RESULT_CODE};
    obj.setTag(_tag);

    CoupersServer server =
        new CoupersServer(
            obj,
            new CoupersServer.ResultCallback() {
              @Override
              public void Update(
                  ArrayList<HashMap<String, String>> result, String method_name, Exception e) {

                if (e == null) {
                  if (result.size() > 0) {
                    HashMap<String, String> map = result.get(0);
                    // if
                    // (map.get(Fields.RESULT_CODE).equals())
                  }

                } else {
                  // handleError(e);
                  Toast.makeText(a, getString(R.string.coupers_server_error), Toast.LENGTH_LONG)
                      .show();
                }
                // Check result code
              }
            });

    server.execute();
  }
  private void RegisterUser(
      String fb_user_id, String fb_user_name, String fb_user_location, String fb_user_username) {

    CoupersObject obj = new CoupersObject(CoupersData.Methods.LOGIN_FACEBOOK);
    obj.addParameter(CoupersData.Parameters.FACEBOOK_ID, fb_user_id);
    obj.addParameter(CoupersData.Parameters.USER_CITY, fb_user_location);
    obj.addParameter(CoupersData.Parameters.USERNAME, fb_user_username);

    String _tag[] = {CoupersData.Fields.USER_ID, CoupersData.Fields.RESULT_CODE};
    obj.setTag(_tag);

    CoupersServer server =
        new CoupersServer(
            obj,
            new CoupersServer.ResultCallback() {
              @Override
              public void Update(
                  ArrayList<HashMap<String, String>> result, String method_name, Exception e) {

                CompleteRegistration(result);
              }
            });

    server.execute();
  }
  private void getDealLevels(final int location_id, final int deal_id) {
    CoupersObject obj = new CoupersObject(Methods.GET_DEAL_LEVELS);
    obj.addParameter(Parameters.DEAL_ID, String.valueOf(deal_id));
    String _tag[] = {
      Fields.LEVEL_ID,
      Fields.LEVEL_START_AT,
      Fields.LEVEL_SHARE_CODE,
      Fields.LEVEL_REDEEM_CODE,
      Fields.LEVEL_DEAL_LEGEND,
      Fields.LEVEL_DEAL_DESCRIPTION,
      Fields.LEVEL_AWARD_LIMIT
    };
    obj.setTag(_tag);

    CoupersServer server =
        new CoupersServer(
            obj,
            new CoupersServer.ResultCallback() {
              @Override
              public void Update(
                  ArrayList<HashMap<String, String>> result, String method_name, Exception e) {
                // Parse data before loading
                ArrayList<CoupersDealLevel> levels = new ArrayList<CoupersDealLevel>();
                if (result.size() > 0) {

                  for (HashMap<String, String> map : result) {
                    CoupersDealLevel level =
                        new CoupersDealLevel(
                            deal_id,
                            Integer.valueOf(map.get(Fields.LEVEL_ID)),
                            Integer.valueOf(map.get(Fields.LEVEL_START_AT)),
                            map.get(Fields.LEVEL_SHARE_CODE),
                            map.get(Fields.LEVEL_REDEEM_CODE),
                            map.get(Fields.LEVEL_DEAL_LEGEND),
                            map.get(Fields.LEVEL_DEAL_DESCRIPTION));
                    levels.add(level);
                    if (!app.db.exists(level)) {
                      app.db.addDealLevel(level);
                    }
                  }
                }
                if (levels.size() > 0)
                  app.findLocation(location_id).findDeal(deal_id).deal_levels = levels;

                loaded_saved_deals++;
                if (loaded_saved_deals == saved_deals) {
                  first_time = false;
                  initiateLoading();
                }
              }
            });

    server.execute();
  }
  private void sendAccessTokenToBackend(String access_token) {
    // Your implementation here.
    CoupersObject obj = new CoupersObject(Methods.SAVE_FACEBOOK_ACCESS_TOKEN);
    obj.addParameter(Parameters.USER_ID, app.getUser_id());
    obj.addParameter(Parameters.ACCESS_TOKEN, access_token);

    String _tag[] = {Fields.RESULT_CODE};
    obj.setTag(_tag);

    CoupersServer server =
        new CoupersServer(
            obj,
            new CoupersServer.ResultCallback() {
              @Override
              public void Update(
                  ArrayList<HashMap<String, String>> result, String method_name, Exception e) {
                // Check result code
              }
            });

    server.execute();
  }
  private void loadSavedDealsWS() {
    CoupersObject obj = new CoupersObject(CoupersData.Methods.GET_SAVED_DEALS);
    obj.addParameter(CoupersData.Parameters.USER_ID, app.getUser_id());
    String _tag[] = {
      Fields.USER_ID,
      Fields.DEAL_ID,
      Fields.LEVEL_ID,
      Fields.SAVED_DEAL_SHARE_COUNT,
      Fields.FACEBOOK_POST_ID,
      Fields.LOCATION_ID,
      Fields.LOCATION_NAME,
      Fields.LOCATION_LOGO,
      Fields.LOCATION_ADDRESS,
      Fields.LOCATION_CITY,
      Fields.LEVEL_DEAL_LEGEND,
      Fields.CATEGORY_ID,
      Fields.LOCATION_DESCRIPTION,
      Fields.LOCATION_WEBSITE_URL,
      Fields.LOCATION_THUMBNAIL,
      Fields.LOCATION_PHONE_NUMBER1,
      Fields.LOCATION_PHONE_NUMBER2,
      Fields.LOCATION_HOURS_OPERATION1,
      Fields.LATITUDE,
      Fields.LONGITUDE
    };
    obj.setTag(_tag);

    CoupersServer server =
        new CoupersServer(
            obj,
            new CoupersServer.ResultCallback() {
              @Override
              public void Update(
                  ArrayList<HashMap<String, String>> result, String method_name, Exception e) {
                // Parse data before loading
                CoupersLocation new_location = null;

                if (result.size() > 0) {
                  for (HashMap<String, String> map : result) {

                    // First check if location,deal exists in app data
                    CoupersLocation location =
                        app.findLocation(Integer.valueOf(map.get(Fields.LOCATION_ID)));
                    if (location != null) // If found then check if deal exists
                    {
                      CoupersDeal deal =
                          location.findDeal(Integer.valueOf(map.get(Fields.DEAL_ID)));
                      if (deal != null) // If found then check as saved, check current level, update
                      // share count and close
                      {
                        deal.saved_deal = true;
                        deal.current_level_id = Integer.valueOf(map.get(Fields.LEVEL_ID));
                        deal.share_count = Integer.valueOf(map.get(Fields.SAVED_DEAL_SHARE_COUNT));
                        deal.fb_post_id = map.get(Fields.FACEBOOK_POST_ID);
                      } else // Create Deal object and add to location
                      {
                        deal =
                            new CoupersDeal(
                                location.location_id,
                                Integer.valueOf(map.get(Fields.DEAL_ID)),
                                map.get(Fields.DEAL_START_DATE),
                                map.get(Fields.DEAL_END_DATE));
                        deal.saved_deal = true;
                        deal.current_level_id = Integer.valueOf(map.get(Fields.LEVEL_ID));
                        deal.share_count = Integer.valueOf(map.get(Fields.SAVED_DEAL_SHARE_COUNT));
                        deal.fb_post_id = map.get(Fields.FACEBOOK_POST_ID);
                        if (!app.db.exists(deal)) {
                          app.db.addDeal(deal);
                        }
                        location.location_deals.add(deal);
                        // getDealLevels(location.location_id, deal.deal_id);
                      }
                    } else // Location not found, create objects and add to app data
                    {
                      new_location =
                          new CoupersLocation(
                              Integer.valueOf(map.get(CoupersData.Fields.LOCATION_ID)),
                              Integer.valueOf(map.get(CoupersData.Fields.CATEGORY_ID)),
                              map.get(CoupersData.Fields.LOCATION_NAME),
                              map.get(CoupersData.Fields.LOCATION_DESCRIPTION),
                              map.get(CoupersData.Fields.LOCATION_WEBSITE_URL),
                              map.get(CoupersData.Fields.LOCATION_LOGO),
                              map.get(CoupersData.Fields.LOCATION_THUMBNAIL),
                              map.get(CoupersData.Fields.LOCATION_ADDRESS),
                              map.get(CoupersData.Fields.LOCATION_CITY),
                              map.get(CoupersData.Fields.LOCATION_PHONE_NUMBER1),
                              map.get(CoupersData.Fields.LOCATION_PHONE_NUMBER2),
                              Double.valueOf(map.get(CoupersData.Fields.LATITUDE)),
                              Double.valueOf(map.get(CoupersData.Fields.LONGITUDE)));
                      new_location.TopDeal = map.get(CoupersData.Fields.LEVEL_DEAL_LEGEND);
                      new_location.show = true;
                      CoupersDeal new_deal =
                          new CoupersDeal(
                              new_location.location_id,
                              Integer.valueOf(map.get(Fields.DEAL_ID)),
                              map.get(Fields.DEAL_START_DATE),
                              map.get(Fields.DEAL_END_DATE));
                      new_deal.saved_deal = true;
                      new_deal.current_level_id = Integer.valueOf(map.get(Fields.LEVEL_ID));
                      new_deal.share_count =
                          Integer.valueOf(map.get(Fields.SAVED_DEAL_SHARE_COUNT));
                      new_deal.fb_post_id = map.get(Fields.FACEBOOK_POST_ID);
                      new_location.location_deals.add(new_deal);
                      if (!app.db.exists(new_deal)) {
                        app.db.addDeal(new_deal);
                      }
                      if (app.findLocation(new_location.location_id) == null)
                        app.locations.add(new_location);
                      // getDealLevels(new_location.location_id, new_deal.deal_id);

                      if (!app.db.exists(new_location)) {
                        app.db.addLocation(new_location);
                      }
                    }
                  }
                }
                load_step = LOAD_LEVELS;
                initiateLoading();
              }
            });

    server.execute();
  }
  private void loadDealsWS() {
    CoupersObject obj = new CoupersObject(CoupersData.Methods.GET_CITY_DEALS);
    obj.addParameter(CoupersData.Parameters.CITY, user_location);
    String _tag[] = {
      CoupersData.Fields.LOCATION_ID,
      CoupersData.Fields.LOCATION_NAME,
      CoupersData.Fields.LOCATION_LOGO,
      CoupersData.Fields.LOCATION_ADDRESS,
      CoupersData.Fields.LOCATION_CITY,
      CoupersData.Fields.CATEGORY_ID,
      CoupersData.Fields.LATITUDE,
      CoupersData.Fields.LONGITUDE,
      CoupersData.Fields.LOCATION_DESCRIPTION,
      CoupersData.Fields.LOCATION_WEBSITE_URL,
      CoupersData.Fields.LOCATION_THUMBNAIL,
      CoupersData.Fields.LOCATION_PHONE_NUMBER1,
      CoupersData.Fields.LOCATION_PHONE_NUMBER2,
      CoupersData.Fields.LOCATION_HOURS_OPERATION1,
      CoupersData.Fields.LEVEL_DEAL_LEGEND,
      CoupersData.Fields.COUNTDEALS
    };
    obj.setTag(_tag);

    CoupersServer server =
        new CoupersServer(
            obj,
            new CoupersServer.ResultCallback() {
              @Override
              public void Update(
                  ArrayList<HashMap<String, String>> result, String method_name, Exception e) {
                // Parse data before loading

                if (result.size() > 0) {

                  for (HashMap<String, String> map : result) {
                    CoupersLocation mLocation =
                        new CoupersLocation(
                            Integer.valueOf(map.get(CoupersData.Fields.LOCATION_ID)),
                            Integer.valueOf(map.get(CoupersData.Fields.CATEGORY_ID)),
                            map.get(CoupersData.Fields.LOCATION_NAME),
                            map.get(CoupersData.Fields.LOCATION_DESCRIPTION),
                            map.get(CoupersData.Fields.LOCATION_WEBSITE_URL),
                            map.get(CoupersData.Fields.LOCATION_LOGO),
                            map.get(CoupersData.Fields.LOCATION_THUMBNAIL),
                            map.get(CoupersData.Fields.LOCATION_ADDRESS),
                            map.get(CoupersData.Fields.LOCATION_CITY),
                            map.get(CoupersData.Fields.LOCATION_PHONE_NUMBER1),
                            map.get(CoupersData.Fields.LOCATION_PHONE_NUMBER2),
                            Double.valueOf(map.get(CoupersData.Fields.LATITUDE)),
                            Double.valueOf(map.get(CoupersData.Fields.LONGITUDE)));
                    mLocation.TopDeal = map.get(CoupersData.Fields.LEVEL_DEAL_LEGEND);
                    mLocation.CountDeals = Integer.valueOf(map.get(CoupersData.Fields.COUNTDEALS));
                    mLocation.show = true;

                    if (!app.db.exists(mLocation)) {
                      app.db.addLocation(mLocation);
                      app.locations.add(mLocation);
                    }
                  }

                  app.gps_available = false;
                  app.nearby_locations = false;
                  findNearbyLocations(app.locations);
                  load_step = LOAD_DEALS;
                  initiateLoading();
                } else {
                  String city_selection_message;
                  AlertDialog.Builder builder = new AlertDialog.Builder(a);
                  View city_view = a.getLayoutInflater().inflate(R.layout.set_city, null, false);
                  TextView city_text = (TextView) city_view.findViewById(R.id.city_text);
                  final Spinner spin_city = (Spinner) city_view.findViewById(R.id.city_spinner);
                  final SpinnerAdapter spin_adapter = spin_city.getAdapter();

                  if (user_location.equals(NO_LOCATION_AVAILABLE)) {
                    // Location not found, show user list of cities for him to select
                    city_selection_message = getString(R.string.location_missing);
                  } else {
                    // Show identified location and have user confirm.
                    city_selection_message = getString(R.string.confirm_location);
                    int i;
                    for (i = 0; i < spin_adapter.getCount(); i++)
                      if (spin_adapter.getItem(i).equals(user_location)) spin_city.setSelection(i);
                  }

                  city_text.setText(city_selection_message);
                  builder.setView(city_view);
                  builder.setNeutralButton(
                      "OK",
                      new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                          // load all locations from web server
                          user_location = spin_city.getSelectedItem().toString().toLowerCase();
                          PreferenceManager.getDefaultSharedPreferences(a)
                              .edit()
                              .putString("user_location", user_location)
                              .commit();
                          city_selected = true;
                          initiateLoading();
                        }
                      });
                  builder.setTitle(getString(R.string.city_selection_title));
                  AlertDialog dialog = builder.create();
                  dialog.show();
                }
              }
            });

    server.execute();
  }