package in.joind;

/*
 * Comment activity. This activity will handle both Events comments and talk comments.
 */

import android.app.Activity;
import android.content.Intent;
import in.joind.R;
import android.view.MenuItem;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Toast;

public class AddComment extends JIActivity implements OnClickListener {
    ProgressDialog saveDialog;
    private String commentType;
    private JSONObject talkJSON;
    private JSONObject eventJSON;
    private String lastError = "";

    final public static int CODE_COMMENT = 1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Allow ActionBar 'up' navigation
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // Set layout
        setContentView(R.layout.addcomment);

        // Get comment type from the intent scratch board
        this.commentType = getIntent().getStringExtra("commentType");
        setTitle(String.format(getString(R.string.activityAddCommentTitle), this.commentType));

        EditText te = (EditText) findViewById(R.id.CommentText);
        te.setText("");
        te.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                JIActivity.setCommentHistory(s.toString());
            }

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
        });


        // Are we commenting on an event?
        if (this.commentType.compareTo("event") == 0) {
            // Get event information
            try {
                this.eventJSON = new JSONObject(getIntent().getStringExtra("eventJSON"));
            } catch (JSONException e) {
                android.util.Log.e(JIActivity.LOG_JOINDIN_APP, "No event passed to activity", e);
            }

            // Set rating bar to invisible since it is not used while
            // commenting on events
            View v;
            v = (View) findViewById(R.id.CommentRatingBar);
            v.setVisibility(View.GONE);
            v = (View) findViewById(R.id.TextViewRating);
            v.setVisibility(View.GONE);
        }

        // Add handler to the buttons
        Button button;
        button = (Button) findViewById(R.id.ButtonAddCommentCancel);
        button.setOnClickListener(this);
        button = (Button) findViewById(R.id.ButtonAddCommentSend);
        button.setOnClickListener(this);
    }

    // Called when button is clicked
    public void onClick(View v) {
        // Did we click cancel button?
        if (v == findViewById(R.id.ButtonAddCommentCancel)) {
            // Cancel, do nothing. No comment will be posted
            this.finish();
        }

        // Did we click send button?
        if (v == findViewById(R.id.ButtonAddCommentSend)) {
            // Show progress dialog
            saveDialog = ProgressDialog.show(this, getString(R.string.generalPleaseWait), getString(R.string.activityAddCommentSendingComment), true);

            // Create new thread, otherwise the progress dialog does not show
            new Thread() {
                public void run() {
                    // Send comment and fetch result
                    final boolean result = sendComment();

                    // Dismiss the dialog
                    saveDialog.dismiss();

                    // Must be done if we want to display the toast on screen. This must be done by a thread that can update the UI
                    runOnUiThread(new Runnable() {
                        public void run() {
                            // Display result from sendcomment()
                            Toast toast = Toast.makeText(getApplicationContext(), result ? getString(R.string.generalSuccessPostComment) : lastError, Toast.LENGTH_LONG);
                            toast.show();

                            // If successful, close this activity and return
                            if (result) {
                                Intent resultIntent = new Intent();
                                setResult(Activity.RESULT_OK, resultIntent);
                                finish();
                            }
                        }
                    });
                }
            }.start();
        }
    }

    // Sendcomment() will do 2 things. Sending comments for talks AND sending comments
    // for events.
    public boolean sendComment() {
        String url;

        // Display progress bar
        displayProgressBar(true);

        lastError = "";

        // Get information from the layout
        RatingBar ratingbar = (RatingBar) findViewById(R.id.CommentRatingBar);
        int rating = (int) ratingbar.getRating();
        EditText tmp1 = (EditText) findViewById(R.id.CommentText);
        String comment = tmp1.getText().toString();

        JSONObject data = new JSONObject();

        try {
            data.put("comment", comment);
        } catch (JSONException e) {
            // do nothing
        }

        // Are we sending a talk comment?
        if (this.commentType.compareTo("talk") == 0) {
            try {
                this.talkJSON = new JSONObject(getIntent().getStringExtra("talkJSON"));
            } catch (JSONException e) {
                android.util.Log.e(JIActivity.LOG_JOINDIN_APP, "No talk passed to activity", e);
            }

            // Talk comments have ratings
            try {
                data.put("rating", rating);
            } catch (JSONException e) {
                // do nothing
            }

            url = this.talkJSON.optString("comments_uri");

        } else {
            // We are sending an event comment
            try {
                this.eventJSON = new JSONObject(getIntent().getStringExtra("eventJSON"));
            } catch (JSONException e) {
                android.util.Log.e(JIActivity.LOG_JOINDIN_APP, "No event passed to activity", e);
            }

            url = this.eventJSON.optString("comments_uri");
        }

        // Send data to joind.in API
        JIRest rest = new JIRest(AddComment.this);
        int result = rest.requestToFullURI(url, data, JIRest.METHOD_POST);
        if (result != JIRest.OK) {
            lastError = rest.getError();
        }

        // Remove progress bar
        displayProgressBar(false);

        return result == JIRest.OK;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

@@ -494,24 +494,56 @@
 get.Button;%0A
+import android.widget.CheckBox;%0A
 import andro
@@ -5310,24 +5310,156 @@
 oString();%0A%0A
+//        CheckBox tmp2 = (CheckBox) findViewById(R.id.CommentPrivate);%0A//        int privateComment = tmp2.isChecked() ? 1 : 0;%0A//%0A
         JSON
@@ -6156,32 +6156,194 @@
 %0A            %7D%0A%0A
+            // and a private status%0A            try %7B%0A                data.put(%22private%22, privateComment);%0A            %7D catch (JSONException e) %7B%0A            %7D%0A%0A
             url 

Allow private comment submission.