package com.yassirh.digitalocean.ui;

import java.util.List;

import com.yassirh.digitalocean.R;
import com.yassirh.digitalocean.model.Image;
import com.yassirh.digitalocean.model.Region;
import com.yassirh.digitalocean.utils.ApiHelper;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class ImageAdapter extends BaseAdapter {
    
    private List<Image> data;
    private boolean showRegion;
    private static LayoutInflater inflater=null;
    
    public ImageAdapter(Activity activity, List<Image> data) {
        this.data = data;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public ImageAdapter(Activity activity, List<Image> data, boolean showRegions) {
        this.data = data;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.showRegion = showRegions;
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return data.get(position);
    }

    public long getItemId(int position) {
    	return data.get(position).getId();
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.image_list_row, parent, false);
        
        final Image image = data.get(position);
        
        if(image.getId() == 0){
        	vi = inflater.inflate(R.layout.image_list_header, parent, false);
        	TextView listHeaderTextView = (TextView)vi.findViewById(R.id.listHeaderTextView);
        	listHeaderTextView.setText(image.getName());
        	vi.setOnClickListener(null);
        }else{
        	if(vi.findViewById(R.id.nameTextView) == null){
	        	vi = inflater.inflate(R.layout.image_list_row, parent, false);
	        }
	        TextView nameTextView = (TextView)vi.findViewById(R.id.nameTextView);
	        ImageView distroImageView = (ImageView)vi.findViewById(R.id.distroImageView);
	        TextView visibilityTextView = (TextView)vi.findViewById(R.id.visibilityTextView);
            distroImageView.setImageResource(ApiHelper.getImageLogo(image.getName(), image.getDistribution(), "active"));
	        nameTextView.setText(image.getName());
	        String visibility;
	        if(!image.isPublic()) {
                visibility = vi.getResources().getString(R.string.private_visibility);
            } else{
                visibility = vi.getResources().getString(R.string.public_visibility);
            }
            visibilityTextView.setText(visibility);
            if(showRegion) {
                TextView regionsTextView = (TextView) vi.findViewById(R.id.regionsTextView);
                regionsTextView.setText(image.getRegions());
            }
        }
        return vi;
    }
}
@@ -35,208 +35,8 @@
 i;%0A%0A
-import java.util.List;%0A%0Aimport com.yassirh.digitalocean.R;%0Aimport com.yassirh.digitalocean.model.Image;%0Aimport com.yassirh.digitalocean.model.Region;%0Aimport com.yassirh.digitalocean.utils.ApiHelper;%0A%0A
 impo
@@ -96,56 +96,8 @@
 xt;%0A
-import android.support.v4.app.FragmentActivity;%0A
 impo
@@ -286,16 +286,198 @@
 tView;%0A%0A
+import com.yassirh.digitalocean.R;%0Aimport com.yassirh.digitalocean.model.Image;%0Aimport com.yassirh.digitalocean.utils.ApiHelper;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 %0Apublic 
@@ -2931,49 +2931,580 @@
     
-regionsTextView.setText(image.getRegions(
+String%5B%5D regions = image.getRegions().split(%22;%22);%0A                StringBuilder regionsStringBuilder = new StringBuilder();%0A                List%3CString%3E distinctRegions = new ArrayList%3CString%3E();%0A                for(String region : regions)%7B%0A                    if(!distinctRegions.contains(region)) %7B%0A                        distinctRegions.add(region);%0A                        regionsStringBuilder.append(region).append(%22,%22);%0A                    %7D%0A                %7D%0A                regionsTextView.setText(regionsStringBuilder.substring(0,regionsStringBuilder.length()-1
 ));%0A

Stop showing regions duplicate on images