package com.cghio.easyjobs;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Message;
import android.widget.ListView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JobsDetails extends Activity {

    public static int JOBS_DETAILS_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jobs_details);
        Bundle extras = getIntent().getExtras();
        if (extras != null && extras.containsKey("JOB_ID")) {
            JOBS_DETAILS_ID = extras.getInt("JOB_ID");
            if (JOBS_DETAILS_ID > 0) {
                getJobDetails();
            }
        }
    }

    private void getJobDetails() {
        if (JOBS_DETAILS_ID == 0 || Home.API_TOKEN.length() == 0 ||Jobs.JOBS_SHOW_URL.length() == 0)
            return;

        try {
            RequestParams params = new RequestParams();
            params.put("token", Home.API_TOKEN);
            AsyncHttpClient client = new AsyncHttpClient();
            String url = Jobs.JOBS_SHOW_URL;
            url = url.replace(":id", JOBS_DETAILS_ID+"");
            client.get(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(final String response) {
                    try {
                        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

                        JSONObject obj = new JSONObject(response);
                        JSONObject job = obj.getJSONObject("job");

                        String[] Keys = new String[]{"name", "script", "id", "created_at",
                                "updated_at", "mean_time"};
                        String[] Names = new String[]{"Name", "Script", "ID", "Created at",
                                "Updated at", "Mean time"};

                        for (int i = 0; i < Keys.length; i++) {
                            Map<String, Object> map = new HashMap<String, Object>();
                            map.put("KEY", Names[i]);
                            map.put("VALUE", job.getString(Keys[i]));
                            data.add(map);
                        }

                        JobsDetailsAdapter adapter = new JobsDetailsAdapter(JobsDetails.this,
                                R.layout.listview_jobs_details_items, data);
                        ListView listview_jobs_details =
                                (ListView) findViewById(R.id.listview_jobs_details);
                        listview_jobs_details.setAdapter(adapter);
                    } catch (JSONException e) {
                        showSimpleErrorDialog(getString(R.string.error_unspecified));
                    }
                }
            });
        } catch (Exception e) {
            showSimpleErrorDialog(getString(R.string.error_unspecified));
        }
    }

    private void showSimpleErrorDialog(String message) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setMessage(message);
        alertDialog.setTitle(R.string.error);
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.ok), (Message) null);
        alertDialog.show();
    }
}

@@ -2504,24 +2504,1718 @@
          %7D%0A%0A
+                        JSONObject interpreter = obj.optJSONObject(%22interpreter%22);%0A%0A                        if (interpreter == null) %7B%0A                            Map%3CString, Object%3E map = new HashMap%3CString, Object%3E();%0A                            map.put(%22KEY%22, %22Interpreter%22);%0A                            map.put(%22VALUE%22, %22(default)%22);%0A                            data.add(map);%0A                        %7D else %7B%0A                            Keys = new String%5B%5D%7B%22path%22, %22upload_script_first%22%7D;%0A                            Names = new String%5B%5D%7B%22Interpreter%22, %22Upload script first?%22%7D;%0A%0A                            for (int i = 0; i %3C Keys.length; i++) %7B%0A                                Map%3CString, Object%3E map = new HashMap%3CString, Object%3E();%0A                                map.put(%22KEY%22, Names%5Bi%5D);%0A                                map.put(%22VALUE%22, interpreter.getString(Keys%5Bi%5D));%0A                                data.add(map);%0A                            %7D%0A                        %7D%0A%0A                        JSONObject server = obj.getJSONObject(%22server%22);%0A%0A                        Keys = new String%5B%5D%7B%22name%22, %22host%22, %22username%22, %22created_at%22,%0A                                %22updated_at%22%7D;%0A                        Names = new String%5B%5D%7B%22Server Name%22, %22Host%22, %22Username%22, %22Server created at%22,%0A                                %22Server updated at%22%7D;%0A%0A                        for (int i = 0; i %3C Keys.length; i++) %7B%0A                            Map%3CString, Object%3E map = new HashMap%3CString, Object%3E();%0A                            map.put(%22KEY%22, Names%5Bi%5D);%0A                            map.put(%22VALUE%22, server.getString(Keys%5Bi%5D));%0A                            data.add(map);%0A                        %7D%0A%0A
             

add more job details;