package org.tndata.android.compass.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.tndata.android.compass.R;
import org.tndata.android.compass.model.Action;
import org.tndata.android.compass.task.GetActionTask;
import org.tndata.android.compass.util.ImageLoader;


/**
 * Displays an action after clicking a notification and allows the user to report
 * whether they did it or to cancel or snooze the action.
 *
 * @author Ismael Alonso
 * @version 1.0.0
 */
public class ActionActivity
        extends ActionBarActivity
        implements
                View.OnClickListener,
                GetActionTask.OnActionRetrievedCallback{

    public static final String ACTION_ID_KEY = "action_id";

    //The action in question
    private Action mAction;

    //UI components
    private ImageView mActionImage;
    private TextView mActionTitle;
    private TextView mActionDescription;


    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action);

        //Set the action to null, this indicates that it has not been fetched
        mAction = null;

        //Fetch UI components
        RelativeLayout circleView = (RelativeLayout)findViewById(R.id.action_circle_view);
        mActionImage = (ImageView)findViewById(R.id.action_image);
        mActionTitle = (TextView)findViewById(R.id.action_title);
        mActionDescription = (TextView)findViewById(R.id.action_description);

        //Listeners
        findViewById(R.id.action_later).setOnClickListener(this);
        findViewById(R.id.action_did_it).setOnClickListener(this);

        //Circle view
        GradientDrawable gradientDrawable = (GradientDrawable)circleView.getBackground();
        gradientDrawable.setColor(Color.WHITE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
            circleView.setBackground(gradientDrawable);
        }
        else{
            circleView.setBackgroundDrawable(gradientDrawable);
        }

        int actionId = getIntent().getIntExtra(ACTION_ID_KEY, -1);
        Log.d("ActionActivity", "action: " + actionId);
        new GetActionTask(this).execute(actionId);
    }

    @Override
    protected void onNewIntent(Intent intent){
        super.onNewIntent(intent);
        Log.d("ActionActivity", "onNewIntent");
        int actionId = getIntent().getIntExtra(ACTION_ID_KEY, -1);
        Log.d("ActionActivity", "action: " + actionId);
        new GetActionTask(this).execute(actionId);
    }

    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.action_later:
                later();
                break;

            case R.id.action_did_it:
                didIt();
                break;
        }
    }

    private void later(){
        finish();
    }

    private void didIt(){
        //TODO when api supports it
    }

    @Override
    public void onActionRetrieved(Action action){
        if (action != null){
            mAction = action;

            //Populate UI
            ImageLoader.loadBitmap(mActionImage, mAction.getIconUrl(), false);
            mActionTitle.setText(mAction.getTitle());
            mActionDescription.setText(mAction.getDescription());
        }
    }
}

@@ -407,16 +407,70 @@
 tView;%0A%0A
+import org.tndata.android.compass.CompassApplication;%0A
 import o
@@ -498,16 +498,16 @@
 pass.R;%0A
-
 import o
@@ -600,16 +600,75 @@
 onTask;%0A
+import org.tndata.android.compass.task.GetUserActionsTask;%0A
 import o
@@ -712,16 +712,45 @@
 oader;%0A%0A
+import java.util.ArrayList;%0A%0A
 %0A/**%0A * 
@@ -1078,44 +1078,46 @@
  Get
+User
 Action
+s
 Task.
-OnActionRetrievedCallback
+GetUserActionsListener
 %7B%0A%0A 
@@ -2638,38 +2638,43 @@
 %0A        new Get
+User
 Action
+s
 Task(this).execu
@@ -2668,32 +2668,111 @@
 k(this).execute(
+((CompassApplication)getApplication()).getToken(),%0A                %22action:%22 + 
 actionId);%0A    %7D
@@ -3055,22 +3055,27 @@
  new Get
+User
 Action
+s
 Task(thi
@@ -3085,16 +3085,95 @@
 execute(
+((CompassApplication)getApplication()).getToken(),%0A                %22action:%22 + 
 actionId
@@ -3604,39 +3604,47 @@
 oid 
-onA
+a
 ction
-Retrieved(
+sLoaded(ArrayList%3C
 Action
+%3E
  action
+s
 )%7B%0A 
@@ -3664,16 +3664,20 @@
 tion
- != null
+s.size() %3E 0
 )%7B%0A 
@@ -3699,24 +3699,32 @@
 ion = action
+s.get(0)
 ;%0A%0A         

Modify ActionActivity to use GetUserActionsTask