package org.adaptlab.chpir.android.participanttracker;

import org.adaptlab.chpir.android.models.Participant;
import org.adaptlab.chpir.android.models.ParticipantType;
import org.adaptlab.chpir.android.models.Property;

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.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NewParticipantFragment extends Fragment {
    private static final String TAG = "NewParticipantFragment";
    public final static String EXTRA_PARTICIPANT_TYPE_ID = 
            "org.adaptlab.chpir.participanttracker.newparticipantfragment.participant_type_id";
    
    private Participant mParticipant;
    private ParticipantType mParticipantType;
    private LinearLayout mParticipantPropertiesContainer;
    
    private TextView mParticipantTitle;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        if (savedInstanceState != null) {
            mParticipantType = ParticipantType.findById(savedInstanceState.getLong(EXTRA_PARTICIPANT_TYPE_ID));
        } else {
            Long participantTypeId = getActivity().getIntent().getLongExtra(EXTRA_PARTICIPANT_TYPE_ID, -1);
            if (participantTypeId == -1) return;

            mParticipantType = ParticipantType.findById(participantTypeId);
        }
    }
        
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_new_participant, parent,
                false);
        
        mParticipantTitle = (TextView) v.findViewById(R.id.new_participant_title);
        mParticipantTitle.setText(getString(R.string.new_participant_prefix) + mParticipantType.getLabel());
        
        mParticipantPropertiesContainer = (LinearLayout) v.findViewById(R.id.new_participant_properties_container);

        for (Property property : mParticipantType.getProperties()) {
            TextView textView = new TextView(getActivity());
            textView.setText(property.getLabel());
            EditText editText = new EditText(getActivity());
            mParticipantPropertiesContainer.addView(textView);
            mParticipantPropertiesContainer.addView(editText);
        }
        
        return v;
    }
}

@@ -49,16 +49,43 @@
 acker;%0A%0A
+import java.util.HashMap;%0A%0A
 import o
@@ -126,24 +126,86 @@
 articipant;%0A
+import org.adaptlab.chpir.android.models.ParticipantProperty;%0A
 import org.a
@@ -372,33 +372,8 @@
 nt;%0A
-import android.util.Log;%0A
 impo
@@ -461,16 +461,46 @@
 wGroup;%0A
+import android.widget.Button;%0A
 import a
@@ -956,16 +956,78 @@
 ntType;%0A
+    private HashMap%3CProperty, EditText%3E mPropertyFields;%0A    %0A
     priv
@@ -1076,21 +1076,16 @@
 tainer;%0A
-    %0A
     priv
@@ -1116,16 +1116,59 @@
 tTitle;%0A
+    private Button mSaveParticipantButton;%0A
     %0A   
@@ -1690,16 +1690,81 @@
 ypeId);%0A
+            mPropertyFields = new HashMap%3CProperty, EditText%3E();%0A
         
@@ -2528,16 +2528,22 @@
         
+final 
 EditText
@@ -2575,32 +2575,85 @@
 getActivity());%0A
+            mPropertyFields.put(property, editText);%0A
             mPar
@@ -2773,32 +2773,769 @@
      %7D%0A        %0A
+        mSaveParticipantButton = (Button) v.findViewById(R.id.save_participant_button);%0A        mSaveParticipantButton.setOnClickListener(new View.OnClickListener() %7B%0A            public void onClick(View v) %7B%0A                Participant participant = new Participant(mParticipantType);%0A                participant.save();%0A                %0A                for (Property property : mParticipantType.getProperties()) %7B%0A                    ParticipantProperty participantProperty = new ParticipantProperty(participant, property, mPropertyFields.get(property).getText().toString());%0A                    participantProperty.save();%0A                %7D%0A                %0A                getActivity().finish();%0A            %7D %0A        %7D);%0A        %0A
         return v

Create new participant and save supplied property values for the participant on save button click