package com.openxc.openxcdiagnostic.diagnostic.output;

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.Command;
import com.openxc.messages.CommandResponse;
import com.openxc.messages.DiagnosticRequest;
import com.openxc.messages.DiagnosticResponse;
import com.openxc.messages.VehicleMessage;
import com.openxc.openxcdiagnostic.R;
import com.openxc.openxcdiagnostic.diagnostic.DiagnosticActivity;
import com.openxc.openxcdiagnostic.diagnostic.ResponseDetailsAlertManager;
import com.openxc.openxcdiagnostic.diagnostic.pair.CommandPair;
import com.openxc.openxcdiagnostic.diagnostic.pair.DiagnosticPair;
import com.openxc.openxcdiagnostic.diagnostic.pair.Pair;
import com.openxc.openxcdiagnostic.util.Formatter;
import com.openxc.openxcdiagnostic.util.Toaster;
import com.openxc.openxcdiagnostic.util.Utilities;

public class OutputRow {

    private LinearLayout mView;
    private OutputTableManager mTableManager;
    private VehicleMessage mResponse;
    private VehicleMessage mRequest;
    private DiagnosticActivity mContext;

    public OutputRow(DiagnosticActivity context,
            OutputTableManager tableManager, VehicleMessage req,
            VehicleMessage resp) {

        mView = (LinearLayout) context.getLayoutInflater().inflate(R.layout.diagoutputrow, null);
        mTableManager = tableManager;
        mResponse = resp;
        mRequest = req;
        mContext = context;

        initButtons();
        fillOutputResponseTable();
        setTimestamp();
    }
    
    public void setPair(Pair pair) {
        mRequest = pair.getRequest();
        mResponse = pair.getResponse();
        fillOutputResponseTable();
        setTimestamp();
    }
    
    private void setTimestamp() {
        String timestampString;
        if (mResponse.getTimestamp() != null) {
            timestampString = Utilities.epochTimeToTime(mResponse.getTimestamp());
        } else {
            timestampString = "0:00";
        }
        ((TextView) mView.findViewById(R.id.outputRowTimestamp)).setText(timestampString);
    }

    private void initButtons() {

        ((Button) mView.findViewById(R.id.outputMoreButton))
        .setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ResponseDetailsAlertManager.show(mContext, mRequest, mResponse);
            }
        });

        ((Button) mView.findViewById(R.id.responseDeleteButton))
        .setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mTableManager.removeRow(OutputRow.this);
            }
        });

        ((Button) mView.findViewById(R.id.outputResendButton))
        .setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String message;
                if (Utilities.isDiagnosticRequest(mRequest)) {
                    message = "Resending Request.";
                    mContext.send(mRequest);
                } else if (Utilities.isCommand(mRequest)) {
                    message = "Resending Command.";
                    mContext.send(mRequest);
                } else {
                    message = "Cannot be resent...no request/command found.";
                }
                Toaster.showToast(mContext, message);
            }
        });
    }
    
    private void setOutputInfoFormat() {
        LinearLayout infoTable = (LinearLayout) mView.findViewById(R.id.outputInfo);
        int layoutId;
        if (Utilities.isDiagnosticResponse(mResponse)) {
            layoutId = R.layout.diagresponseoutputinfo;
        } else {
            layoutId = R.layout.commandresponseoutputinfo;
        }
        LinearLayout newView = (LinearLayout) mContext.getLayoutInflater().inflate(layoutId, null);
        infoTable.removeAllViews();
        infoTable.addView(newView);
    }

    private void fillOutputResponseTable() {
        
        setOutputInfoFormat();
        LinearLayout infoTable = (LinearLayout) mView.findViewById(R.id.outputInfo);
            if (Utilities.isDiagnosticResponse(mResponse)) {
                DiagnosticResponse resp = (DiagnosticResponse) mResponse;
                ((TextView) infoTable.findViewById(R.id.busValue)).setText(Formatter.getBusOutput(resp));
                ((TextView) infoTable.findViewById(R.id.idValue)).setText(Formatter.getIdOutput(resp));
                ((TextView) infoTable.findViewById(R.id.modeValue)).setText(Formatter.getModeOutput(resp));
                ((TextView) infoTable.findViewById(R.id.pidValue)).setText(Formatter.getPidOutput(resp));
                ((TextView) infoTable.findViewById(R.id.successValue)).setText(Formatter.getSuccessOutput(resp));
                ((TextView) infoTable.findViewById(R.id.payloadValue)).setText(Formatter.getPayloadOutput(resp));
                ((TextView) infoTable.findViewById(R.id.valueValue)).setText(Formatter.getValueOutput(resp));
            } else if (Utilities.isCommandResponse(mResponse)){
                CommandResponse cmdResponse = (CommandResponse) mResponse;
                ((TextView) infoTable.findViewById(R.id.commandLabel)).setText( 
                        Formatter.getCommandOutput(cmdResponse));
                ((TextView) infoTable.findViewById(R.id.commandValue)).setText( 
                        Formatter.getMessageOutput(cmdResponse));
            }
    }

    /*private void fillOutputTableWithSuccessDetails(LinearLayout responseTable,
            DiagnosticResponse resp) {
        createAndAddRowToOutput(responseTable, "payload", Formatter.getPayloadOutput(resp), resp);
        createAndAddRowToOutput(responseTable, "value", Formatter.getValueOutput(resp), resp);
    }

    private void fillOutputTableWithFailureDetails(LinearLayout responseTable,
            DiagnosticResponse resp) {
        createAndAddRowToOutput(responseTable, "neg. resp. code", 
                Formatter.getOutputTableResponseCodeOutput(resp), resp);
    }*/

    public LinearLayout getView() {
        return mView;
    }

    public Pair getPair() {
        if (Utilities.isDiagnosticResponse(mResponse)) {
            return new DiagnosticPair(mRequest == null ? null : (DiagnosticRequest) mRequest,
                    (DiagnosticResponse) mResponse);
        }
        return new CommandPair(mRequest == null ? null : (Command) mRequest, (CommandResponse) mResponse);
    }

}

@@ -49,16 +49,43 @@
 utput;%0A%0A
+import android.os.Handler;%0A
 import a
@@ -1192,24 +1192,70 @@
 ty mContext;
+%0A    private Handler mHandler = new Handler();
 %0A%0A    public
@@ -1726,16 +1726,22 @@
 setPair(
+final 
 Pair pai
@@ -1741,24 +1741,103 @@
 air pair) %7B%0A
+        mHandler.post(new Runnable() %7B%0A            public void run() %7B%0A        
         mReq
@@ -1858,24 +1858,32 @@
 tRequest();%0A
+        
         mRes
@@ -1910,32 +1910,40 @@
 onse();%0A        
+ 
+       
 fillOutputRespon
@@ -1953,32 +1953,40 @@
 able();%0A        
+ 
+       
 setTimestamp();%0A
@@ -1977,32 +1977,58 @@
 setTimestamp();%0A
+            %7D%0A        %7D);%0A
     %7D%0A    %0A    p

Use a runnable to update table.