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);
                        }

                        JSONObject interpreter = obj.optJSONObject("interpreter");

                        if (interpreter == null) {
                            Map<String, Object> map = new HashMap<String, Object>();
                            map.put("KEY", "Interpreter");
                            map.put("VALUE", "(default)");
                            data.add(map);
                        } else {
                            Keys = new String[]{"path", "upload_script_first"};
                            Names = new String[]{"Interpreter", "Upload script first?"};

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

                        JSONObject server = obj.getJSONObject("server");

                        Keys = new String[]{"name", "host", "username", "created_at",
                                "updated_at"};
                        Names = new String[]{"Server Name", "Host", "Username", "Server created at",
                                "Server updated at"};

                        for (int i = 0; i < Keys.length; i++) {
                            Map<String, Object> map = new HashMap<String, Object>();
                            map.put("KEY", Names[i]);
                            map.put("VALUE", server.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();
    }
}

@@ -3537,26 +3537,26 @@
 erver = obj.
-ge
+op
 tJSONObject(
@@ -3563,24 +3563,366 @@
 %22server%22);%0A%0A
+                        if (server == null) %7B%0A                            Map%3CString, Object%3E map = new HashMap%3CString, Object%3E();%0A                            map.put(%22KEY%22, %22Server%22);%0A                            map.put(%22VALUE%22, getString(R.string.no_server));%0A                            data.add(map);%0A                        %7D else %7B%0A    
             
@@ -4019,32 +4019,36 @@
                 
+    
 %22updated_at%22%7D;%0A 
@@ -4062,32 +4062,36 @@
                 
+ 
+   
 Names = new Stri
@@ -4183,16 +4183,20 @@
         
+   
+ 
 %22Server 
@@ -4206,24 +4206,28 @@
 ated at%22%7D;%0A%0A
+    
             
@@ -4298,32 +4298,36 @@
                 
+  
+  
 Map%3CString, Obje
@@ -4359,32 +4359,36 @@
 ing, Object%3E();%0A
+    
                 
@@ -4445,32 +4445,36 @@
                 
+    
 map.put(%22VALUE%22,
@@ -4522,32 +4522,36 @@
                 
+    
 data.add(map);%0A 
@@ -4541,32 +4541,62 @@
  data.add(map);%0A
+                            %7D%0A
                 

fix little bug;