package org.adaptlab.chpir.android.participanttracker;

import org.adaptlab.chpir.android.models.Participant;
import org.adaptlab.chpir.android.models.ParticipantProperty;
import org.adaptlab.chpir.android.participanttracker.Receivers.InstrumentListReceiver;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ParticipantDetailFragment extends Fragment {
    private static final String TAG = "ParticipantDetailFragment";
    public final static String EXTRA_PARTICIPANT_ID = 
            "org.adaptlab.chpir.participanttracker.participantdetailfragment.participant_id";
    
    private Participant mParticipant;
    private LinearLayout mParticipantPropertiesContainer;
    private Button mNewSurveyButton;
    private static Activity mActivity;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        
        if (savedInstanceState != null) {
            mParticipant = Participant.findById(savedInstanceState.getLong(EXTRA_PARTICIPANT_ID));
        } else {
            Long participantId = getActivity().getIntent().getLongExtra(EXTRA_PARTICIPANT_ID, -1);
            if (participantId == -1) return;

            mParticipant = Participant.findById(participantId);
        }
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_participant_detail, parent,
                false);       
        
        mParticipantPropertiesContainer = (LinearLayout) v.findViewById(R.id.participant_properties_container);
        
        for (ParticipantProperty participantProperty : mParticipant.getParticipantProperties()) {
            TextView textView = new TextView(getActivity());
            textView.setText(participantProperty.getProperty().getLabel() + ": " + participantProperty.getValue());
            mParticipantPropertiesContainer.addView(textView);
        }
        
        mNewSurveyButton = (Button) v.findViewById(R.id.new_survey_button);
        mNewSurveyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent();
                i.setAction(InstrumentListReceiver.GET_INSTRUMENT_LIST);
                getActivity().getApplicationContext().sendBroadcast(i);       
            }
        });
        
        return v;
    }
    
    public static void displayInstrumentPicker(String[] instrumentTitleList, final long[] instrumentIdList) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        builder.setTitle("Choose something");
        builder.setSingleChoiceItems(instrumentTitleList, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {             
                Intent i = new Intent();
                i.setAction(InstrumentListReceiver.START_SURVEY);
                i.putExtra(InstrumentListReceiver.START_SURVEY_INSTRUMENT_ID, instrumentIdList[which]);
                mActivity.sendBroadcast(i);
                mActivity.finish();
            }
        }); 
        
        builder.show();
    }
}

@@ -950,45 +950,207 @@
     
-%0A    private Participant mParticipant
+public final static String EXTRA_PARTICIPANT_METADATA =%0A            %22org.adaptlab.chpir.android.survey.metadata%22;%0A    %0A    private Participant mParticipant;%0A    private static String sParticipantUUID
 ;%0A  
@@ -1270,17 +1270,17 @@
 ctivity 
-m
+s
 Activity
@@ -1398,33 +1398,33 @@
 State);%0A        
-m
+s
 Activity = getAc
@@ -1579,24 +1579,79 @@
 IPANT_ID));%0A
+            sParticipantUUID = mParticipant.getUUID();%0A
         %7D el
@@ -1864,16 +1864,71 @@
 antId);%0A
+            sParticipantUUID = mParticipant.getUUID();%0A
         
@@ -3307,17 +3307,17 @@
 Builder(
-m
+s
 Activity
@@ -3790,33 +3790,119 @@
                 
-m
+i.putExtra(EXTRA_PARTICIPANT_METADATA, getParticipantMetadataJSON());%0A                s
 Activity.sendBro
@@ -3928,17 +3928,17 @@
         
-m
+s
 Activity
@@ -4014,10 +4014,149 @@
 ;%0A    %7D%0A
+    %0A    private static String getParticipantMetadataJSON() %7B%0A        return %22%7B%5C%22participant_uuid%5C%22: %5C%22%22 + sParticipantUUID + %22%5C%22%7D%22;%0A    %7D%0A
 %7D%0A

Add metadata json object when launching new survey