package net.mabako.steamgifts.tasks;

import android.content.Intent;
import android.util.Log;

import net.mabako.steamgifts.activities.WriteCommentActivity;

import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public abstract class EditCommentTask extends AjaxTask<Activity> {
    private static final String TAG = EditCommentTask.class.getSimpleName();

    private final Comment comment;
    private final String newText;

    public EditCommentTask(Activity activity, String xsrfToken, String newText, Comment comment) {
        super(activity, activity, xsrfToken, "comment_edit");

        this.newText = newText;
        this.comment = comment;

    protected void addExtraParameters(Connection connection) {"allow_replies", "1")
                .data("comment_id", String.valueOf(comment.getId()))
                .data("description", newText);

    protected void onPostExecute(Connection.Response response) {
        Activity activity = getFragment();
        if (response != null && response.statusCode() == 200) {
            try {
                Log.v(TAG, "Response to JSON request: " + response.body());
                JSONObject root = new JSONObject(response.body());

                boolean success = "success".equals(root.getString("type"));
                if (success) {
                    Document commentHtml = Jsoup.parse(root.getString("comment"));

                    // Save the content of the edit state for a bit & remove the edit state from being rendered.
                    Element editState =" textarea[name=description]").first();
                    Element desc =".comment__description").first();

                    if (editState == null)
                        Log.d(TAG, "edit state is null?");
                    comment.setEditableContent(editState == null ? null : editState.text());

                    Intent data = new Intent();
                    data.putExtra("edited-comment", comment);
                    activity.setResult(WriteCommentActivity.COMMENT_EDIT_SENT, data);

            } catch (JSONException e) {
                Log.e(TAG, "Failed to parse JSON object", e);

    protected abstract void onFail();