package com.pennapps.labs.pennmobile.adapters;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.pennapps.labs.pennmobile.R;
import com.pennapps.labs.pennmobile.classes.Person;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class DirectoryAdapter extends ArrayAdapter<Person> {
    private final LayoutInflater inflater;

    public DirectoryAdapter(Context context, List<Person> persons) {
        super(context, R.layout.directory_list_item, persons);
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = inflater.inflate(R.layout.directory_list_item, parent, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }
        Person person = getItem(position);
        final Person currentPerson = person;

        holder.contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_INSERT);
                intent.setType(ContactsContract.Contacts.CONTENT_TYPE);

                String name = currentPerson.getName();
                intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
                intent.putExtra(ContactsContract.Intents.Insert.PHONE, currentPerson.phone);
                intent.putExtra(ContactsContract.Intents.Insert.EMAIL, currentPerson.email);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                getContext().startActivity(intent);
            }
        });

        holder.tvName.setText(person.getName());
        holder.tvAffiliation.setText(person.affiliation);

        if (person.email.length() == 0) {
            holder.tvEmail.setVisibility(View.GONE);
        } else {
            holder.tvEmail.setText(person.email);
            holder.tvEmail.setPaintFlags(holder.tvEmail.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
            holder.tvEmail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String uri = "mailto:" + currentPerson.email;
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(uri));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    v.getContext().startActivity(intent);
                }
            });
        }

        if (person.phone.length() == 0) {
            holder.tvPhone.setVisibility(View.GONE);
        } else {
            holder.tvPhone.setText(person.phone);
            holder.tvPhone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String uri = "tel:" + currentPerson.phone;
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse(uri));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    v.getContext().startActivity(intent);
                }
            });
        }

        holder.star.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToggleButton star = (ToggleButton) v;
                boolean starred = star.isChecked();
                if (starred) {
                    System.out.println("Starred " + currentPerson.getName());
                } else {
                    System.out.println("Unstarred " + currentPerson.getName());
                }
            }
        });

        return view;
    }

    static class ViewHolder {
        @InjectView(R.id.tv_person_name) TextView tvName;
        @InjectView(R.id.tv_person_affiliation) TextView tvAffiliation;
        @InjectView(R.id.tv_person_email) TextView tvEmail;
        @InjectView(R.id.tv_person_phone) TextView tvPhone;
        @InjectView(R.id.star_contact) ToggleButton star;
        @InjectView(R.id.contact_icon) ImageView contact;

        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }

}

@@ -123,46 +123,133 @@
 oid.
-graphics.Paint;%0Aimport android.net.Uri
+content.SharedPreferences;%0Aimport android.graphics.Paint;%0Aimport android.net.Uri;%0Aimport android.preference.PreferenceManager
 ;%0Aim
@@ -677,19 +677,67 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0Aimpo
@@ -2630,16 +2630,17 @@
 tFlags()
+ 
 %7C Paint.
@@ -3840,131 +3840,711 @@
     
-holder.star.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B
+SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(view.getContext());%0A        Set%3CString%3E starredContacts = sharedPref.getStringSet(%22starred%22, new HashSet%3CString%3E());%0A        holder.star.setChecked(starredContacts.contains(currentPerson.getName()));%0A        holder.star.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(v.getContext());%0A                Set%3CString%3E starredContacts = sharedPref.getStringSet(%22starred%22, new HashSet%3CString%3E());%0A                SharedPreferences.Editor editedPreferences = sharedPref.edit();
 %0A   
@@ -4642,24 +4642,86 @@
 sChecked();%0A
+                String currentName = currentPerson.getName();%0A
             
@@ -4837,114 +4837,417 @@
     
-%7D else %7B%0A                    System.out.println(%22Unstarred %22 + currentPerson.getName());%0A                %7D
+    if (currentName != null) %7B%0A                        starredContacts.add(currentName);%0A                    %7D%0A                %7D else %7B%0A                    System.out.println(%22Unstarred %22 + currentPerson.getName());%0A                    starredContacts.remove(currentName);%0A                %7D%0A                editedPreferences.putStringSet(%22starred%22, starredContacts);%0A                editedPreferences.apply();
 %0A   

Save starred and draw new old ones