package com.sngastro.sngcontacts;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.sngastro.sngcontacts.adapter.ContactArrayAdapter;
import com.sngastro.sngcontacts.contact.ContactInfo;
import com.sngastro.sngcontacts.contact.SelfCertUtils;
import com.squareup.okhttp.OkHttpClient;

import java.util.ArrayList;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.OkClient;
import retrofit.client.Response;

public class ContactListActivity extends AppCompatActivity {

    private static final String TAG = "tag";

    public static final String ENDPOINT = "https://sngcontactinfo.ddns.net:8888";

    ArrayList<ContactInfo> contactList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_list);
        // create a class ContactData with name, phone number

        // add some dummy contact data objects to array

        // display custom view and display name number and a call button

        final ListView listView = (ListView) findViewById(R.id.contactListView);
        contactList = new ArrayList<>();

//        contactList.add(new ContactInfo("Aamir Godil", "(916)783-5816", "cellNumber", "godil.aamir1@gmail.com"));
//        contactList.add(new ContactInfo("Aslam Godil", "(916)783-5816", "(530)263-2478", "aslamgodilmd@yahoo.com"));
//        contactList.add(new ContactInfo("Faraaz Godil", "(916)783-5816", "cellNumber", "emailAddress"));

        OkHttpClient okHttpClient = SelfCertUtils.configureClient(new OkHttpClient());

        RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(okHttpClient)).setEndpoint(ENDPOINT).build();
        ContactHandler handler = restAdapter.create(ContactHandler.class);
        handler.readContacts(new Callback<ArrayList<ContactInfo>>() {

            @Override
            public void success(ArrayList<ContactInfo> contactInfos, Response response) {
                contactList.addAll(contactInfos);
            }

            @Override
            public void failure(RetrofitError error) {
                Log.i(TAG, "failure");
            }

        });

        ListAdapter adapter = new ContactArrayAdapter(this, contactList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ContactInfo contactInfo = (ContactInfo) listView.getItemAtPosition(position);
                Intent i = new Intent(getApplicationContext(), ContactViewActivity.class);

                ArrayList<ContactInfo> list = new ArrayList<ContactInfo>();
                list.add(contactInfo);
                i.putExtra("ContactInfo", list);
                startActivity(i);
            }
        });
        Log.i(TAG, "onListActivityCreate");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.menu_main, menu);
        Log.i(TAG, "onCreateOptionsMenu");
        return true;
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart");
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
    }


    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause");
    }


    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop");
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "onRestart");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState");
    }


    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG, "onRestoreInstanceState");
    }

}

@@ -870,31 +870,21 @@
 s://
-sngcontactinfo.ddns.net
+192.168.1.145
 :888
@@ -930,16 +930,57 @@
 List;%0A%0A%0A
+    private ListAdapter adapter = null;%0A%0A
     @Ove
@@ -1886,24 +1886,90 @@
 Client());%0A%0A
+        adapter = new ContactArrayAdapter(this, contactList);%0A%0A%0A%0A%0A
         Rest
@@ -2397,186 +2397,44 @@
 s);%0A
-            %7D%0A%0A            @Override%0A            public void failure(RetrofitError error) %7B%0A                Log.i(TAG, %22failure%22);%0A            %7D%0A%0A        %7D);%0A%0A        Lis
+%0A                listView.se
 tAdapter
  ada
@@ -2433,101 +2433,27 @@
 pter
- 
+(
 adapter
- = new ContactArrayAdapter(this, contactList);%0A        listView.setAdapter(adapter);%0A
+);%0A        
     
@@ -2532,32 +2532,40 @@
 ) %7B%0A            
+        
 @Override%0A      
@@ -2562,32 +2562,40 @@
 ide%0A            
+ 
+       
 public void onIt
@@ -2657,24 +2657,32 @@
  long id) %7B%0A
+        
             
@@ -2779,16 +2779,24 @@
         
+        
 Intent i
@@ -2875,24 +2875,32 @@
             
+       
+ 
 ArrayList%3CCo
@@ -2959,24 +2959,32 @@
             
+        
 list.add(con
@@ -3010,16 +3010,24 @@
         
+      
+  
 i.putExt
@@ -3067,16 +3067,24 @@
         
+        
 startAct
@@ -3109,21 +3109,200 @@
     
+    
-%7D%0A        %7D);
+    %7D%0A                %7D);%0A%0A%0A%0A            %7D%0A%0A            @Override%0A            public void failure(RetrofitError error) %7B%0A                Log.i(TAG, %22failure%22);%0A            %7D%0A%0A        %7D);%0A%0A
 %0A   

fix for device