package com.openxc.openxcdiagnostic.diagnostic;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.openxc.messages.DiagnosticRequest;
import com.openxc.openxcdiagnostic.R;

/**
 * 
 * Manager for storing favorite requests. 
 * 
 */
public class DiagnosticFavoritesAlertManager {

    private AlertDialog mAlert;
    private DiagnosticActivity mContext;
    
    public DiagnosticFavoritesAlertManager(DiagnosticActivity context) {
        mContext = context;
    }
        
    public void showAlert() {

        LinearLayout favoritesLayout = (LinearLayout) mContext.getLayoutInflater().inflate(R.layout.diagfavoritesalert, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle(mContext.getResources().getString(R.string.favorites_alert_label));
        builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            }
        });
        builder.setView(favoritesLayout);
        mAlert = builder.create();
        refreshLayout(favoritesLayout);
        mAlert.show();
    }
    
    private void refreshLayout(LinearLayout favoritesLayout) {
        clearLayout(favoritesLayout);
        fillLayout(favoritesLayout);
        mAlert.setView(favoritesLayout);
    }
    
    private void fillLayout(LinearLayout favoritesLayout) {
        for (DiagnosticRequest req : DiagnosticFavoritesManager.getFavorites()) {
            createAndAddRow(favoritesLayout, req);
        }
    }
    
    private void clearLayout(LinearLayout layout) {
        layout.removeAllViews();
    }
    
    private void createAndAddRow(final LinearLayout favoritesLayout, final DiagnosticRequest req) {
        
        LinearLayout row = (LinearLayout) mContext.getLayoutInflater().inflate(R.layout.diagfavoritestablerow, null);
        ((TextView) row.findViewById(R.id.favoritesRowLabel)).setText(req.getName() == null ? "PLACEHOLDER" : req.getName());
        
        Button selectButton =  (Button) row.findViewById(R.id.favoritesRowSelectButton);
        selectButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mAlert.cancel();
                mContext.populateFields(req);
            }
        });
        
        Button deleteButton =  (Button) row.findViewById(R.id.favoritesRowDeleteButton);
        deleteButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DiagnosticFavoritesManager.removeFavoriteRequest(req);
                refreshLayout(favoritesLayout);
            }
        });

        favoritesLayout.addView(row);
    }
    
}

@@ -1243,39 +1243,36 @@
 eate();%0A        
-refresh
+fill
 Layout(favorites
@@ -1318,198 +1318,8 @@
    %0A
-    private void refreshLayout(LinearLayout favoritesLayout) %7B%0A        clearLayout(favoritesLayout);%0A        fillLayout(favoritesLayout);%0A        mAlert.setView(favoritesLayout);%0A    %7D%0A    %0A
     
@@ -1532,104 +1532,8 @@
    %0A
-    private void clearLayout(LinearLayout layout) %7B%0A        layout.removeAllViews();%0A    %7D%0A    %0A
     
@@ -1637,24 +1637,24 @@
        %0A
-
         
 LinearLa
@@ -1637,32 +1637,38 @@
        %0A        
+final 
 LinearLayout row
@@ -2535,22 +2535,8 @@
     
-refreshLayout(
 favo
@@ -2538,32 +2538,47 @@
  favoritesLayout
+.removeView(row
 );%0A            %7D

Improve efficiency of favorite deletion.