package com.thebluealliance.androidclient.accounts;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.appspot.tba_dev_phil.tbaMobile.TbaMobile;
import com.appspot.tba_dev_phil.tbaMobile.model.ModelsMobileApiMessagesBaseResponse;
import com.appspot.tba_dev_phil.tbaMobile.model.ModelsMobileApiMessagesSubscriptionMessage;
import com.google.gson.JsonArray;
import com.google.gson.JsonPrimitive;
import com.thebluealliance.androidclient.Constants;
import com.thebluealliance.androidclient.datafeed.Database;
import com.thebluealliance.androidclient.gcm.GCMAuthHelper;
import com.thebluealliance.androidclient.helpers.MyTBAHelper;
import com.thebluealliance.androidclient.models.Subscription;

import java.io.IOException;

/**
 * File created by phil on 8/2/14.
 */
public class AddUpdateUserSubscription extends AsyncTask<String, Void, AddUpdateUserSubscription.Result> {

    protected enum Result {
        ADDED, NOTUPDATED, ERROR
    }

    private Context context;
    private boolean added;

    public AddUpdateUserSubscription(Context context) {
        this.context = context;
    }

    @Override
    protected Result doInBackground(String... params) {
        String modelKey = params[0];
        JsonArray notifications = new JsonArray();
        for (int i = 1; i < params.length; i++) {
            notifications.add(new JsonPrimitive(params[i]));
        }
        String user = AccountHelper.getSelectedAccount(context);
        String key = MyTBAHelper.createKey(user, modelKey);
        Database.Subscriptions table = Database.getInstance(context).getSubscriptionsTable();
        added = !table.exists(key);
        TbaMobile service = AccountHelper.getAuthedTbaMobile(context);
        ModelsMobileApiMessagesSubscriptionMessage request = new ModelsMobileApiMessagesSubscriptionMessage();
        request.setModelKey(modelKey);
        request.setDeviceKey(GCMAuthHelper.getRegistrationId(context));
        request.setSettings(notifications.toString());

        Log.d(Constants.LOG_TAG, "Adding subscription");
        try {
            ModelsMobileApiMessagesBaseResponse response = service.subscriptions().add(request).execute();
            if (response.getCode() == 200) {
                table.add(new Subscription(user, modelKey, notifications.toString()));
                return Result.ADDED;
            }else if(response.getCode() == 304) {
                Log.d(Constants.LOG_TAG, "Subscription not modified");
                return Result.NOTUPDATED;
            }else{
                Log.w(Constants.LOG_TAG, "Code " + response.getCode() + " while adding subscription.\n" + response.getMessage());
                return Result.ERROR;
            }
        } catch (IOException e) {
            Log.e(Constants.LOG_TAG, "IO Exception while adding subscription");
            e.printStackTrace();
            return Result.ERROR;
        }

    }

    @Override
    protected void onPostExecute(Result result) {
        super.onPostExecute(result);
        String text;
        if (result == Result.ADDED) {
            text = "Subscription "+(added?"added":"updated");
        } else {
            text = "Error adding subscription";
        }
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }
}

@@ -3068,26 +3068,33 @@
  String text
+ = null
 ;%0A
-
         if (
@@ -3196,16 +3196,42 @@
  %7D else 
+if(result == Result.ERROR)
 %7B%0A      
@@ -3274,32 +3274,63 @@
 ion%22;%0A        %7D%0A
+        if(text != null) %7B%0A    
         Toast.ma
@@ -3375,20 +3375,30 @@
 RT).show();%0A
+        %7D%0A
     %7D%0A%7D%0A

Fix rogue toast