/**
 * Hubroid - A GitHub app for Android
 *
 * Copyright (c) 2011 Eddie Ringle.
 *
 * Licensed under the New BSD License.
 */

package net.idlesoft.android.apps.github.activities;

import net.idlesoft.android.apps.github.R;

import org.idlesoft.libraries.ghapi.APIAbstract.Response;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CreateIssue extends BaseActivity {
    private static class CreateIssueTask extends AsyncTask<Void, Void, Integer> {
        public CreateIssue activity;

        @Override
        protected Integer doInBackground(final Void... params) {
            final String title = ((TextView) activity.findViewById(R.id.et_create_issue_title))
                    .getText().toString();
            final String body = ((TextView) activity.findViewById(R.id.et_create_issue_body))
                    .getText().toString();
            if (!title.equals("") && !body.equals("")) {
                final Response createResp = activity.mGApi.issues.open(activity.mRepositoryOwner,
                        activity.mRepositoryName, title, body);
                if (createResp.statusCode == 201) {
                    try {
                        activity.mIssueJson = new JSONObject(createResp.resp)
                                .getJSONObject("issue");
                    } catch (final JSONException e) {
                        e.printStackTrace();
                    }
                }
                return createResp.statusCode;
            }
            return null;
        }

        @Override
        protected void onPostExecute(final Integer result) {
            if (result.intValue() == 201 && activity.mIssueJson != null) {
                final Intent i = new Intent(activity, SingleIssue.class);
                i.putExtra("repo_owner", activity.mRepositoryOwner);
                i.putExtra("repo_name", activity.mRepositoryName);
                i.putExtra("json", activity.mIssueJson.toString());

                activity.mProgressDialog.dismiss();
                activity.startActivity(i);
                activity.finish();
            } else {
                Toast.makeText(activity, "Error creating issue.", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        protected void onPreExecute() {
            activity.mProgressDialog = ProgressDialog.show(activity, "Please Wait...",
                    "Creating issue...");
        }
    }

    private CreateIssueTask mCreateIssueTask;

    private JSONObject mIssueJson;

    private ProgressDialog mProgressDialog;

    private String mRepositoryName;

    private String mRepositoryOwner;

    @Override
    public void onCreate(final Bundle icicle) {
        super.onCreate(icicle, R.layout.create_issue);

        setupActionBar();

        final Bundle extras = getIntent().getExtras();
        if (extras != null) {
            if (extras.containsKey("repo_owner")) {
                mRepositoryOwner = extras.getString("repo_owner");
            } else {
                mRepositoryOwner = mUsername;
            }
            if (extras.containsKey("repo_name")) {
                mRepositoryName = extras.getString("repo_name");
            }
        } else {
            mRepositoryOwner = mUsername;
        }

        mCreateIssueTask = (CreateIssueTask) getLastNonConfigurationInstance();
        if (mCreateIssueTask == null) {
            mCreateIssueTask = new CreateIssueTask();
        }
        mCreateIssueTask.activity = this;

        if (mCreateIssueTask.getStatus() == AsyncTask.Status.RUNNING) {
            mProgressDialog = ProgressDialog.show(CreateIssue.this, "Please Wait...",
                    "Creating issue...", true);
        }

        ((TextView) findViewById(R.id.tv_page_title)).setText("New Issue");

        ((Button) findViewById(R.id.btn_create_issue_submit))
                .setOnClickListener(new OnClickListener() {
                    public void onClick(final View v) {
                        if (mCreateIssueTask.getStatus() == AsyncTask.Status.FINISHED) {
                            mCreateIssueTask = new CreateIssueTask();
                            mCreateIssueTask.activity = CreateIssue.this;
                        }
                        if (mCreateIssueTask.getStatus() == AsyncTask.Status.PENDING) {
                            mCreateIssueTask.execute();
                        }
                    }
                });
    }

    @Override
    public void onPause() {
        if ((mProgressDialog != null) && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
        super.onPause();
    }

    @Override
    public void onRestoreInstanceState(final Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState.containsKey("titleText")) {
            ((EditText) findViewById(R.id.et_create_issue_title)).setText(savedInstanceState
                    .getString("titleText"));
        }
        if (savedInstanceState.containsKey("bodyText")) {
            ((EditText) findViewById(R.id.et_create_issue_body)).setText(savedInstanceState
                    .getString("bodyText"));
        }
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        return mCreateIssueTask;
    }

    @Override
    public void onSaveInstanceState(final Bundle savedInstanceState) {
        savedInstanceState.putString("titleText",
                ((EditText) findViewById(R.id.et_create_issue_title)).getText().toString());
        savedInstanceState.putString("bodyText",
                ((EditText) findViewById(R.id.et_create_issue_body)).getText().toString());
        super.onSaveInstanceState(savedInstanceState);
    }
}

@@ -1799,33 +1799,44 @@
 e;%0A            %7D
-%0A
+ else %7B%0A    
             retu
@@ -1842,13 +1842,25 @@
 urn 
-null;
+-1;%0A            %7D
 %0A   
@@ -1942,24 +1942,73 @@
 r result) %7B%0A
+            activity.mProgressDialog.dismiss();%0A%0A
             
@@ -2353,60 +2353,8 @@
 );%0A%0A
-                activity.mProgressDialog.dismiss();%0A
     
@@ -2423,24 +2423,185 @@
 y.finish();%0A
+            %7D else if (result.intValue() == -1) %7B%0A                Toast.makeText(activity, %22Title and body fields cannot be blank.%22, Toast.LENGTH_SHORT).show();%0A
             
@@ -2672,17 +2672,44 @@
 g issue.
-%22
+ Error %22 + result.intValue()
 , Toast.

Fix NullPointerException; Check if issue fields are blank