package org.adaptlab.chpir.android.survey.questionfragments;

import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.ViewGroup;
import android.widget.EditText;

import org.adaptlab.chpir.android.survey.R;
import org.adaptlab.chpir.android.survey.SingleQuestionFragment;
import org.adaptlab.chpir.android.survey.models.Validation;
import org.adaptlab.chpir.android.survey.verhoeff.ParticipantIdValidator;

public class FreeResponseQuestionFragment extends SingleQuestionFragment {
    private static final String TAG = "FreeResponseQuestionFragment";
    private String mText = "";
    private EditText mFreeText;

    @Override
    public void createQuestionComponent(ViewGroup questionComponent) {
        mFreeText = new EditText(getActivity());
        mFreeText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE |
                InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
        beforeAddViewHook(mFreeText);
        mFreeText.setHint(R.string.free_response_edittext);
        mFreeText.addTextChangedListener(new TextWatcher() {
            private boolean backspacing = false;
            // Required by interface
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                backspacing = before > count;
                mText = s.toString();
                setResponseText();
            }

            public void afterTextChanged(Editable s) {
                if (mSpecialResponses != null && s.length() > 0) {
                    mSpecialResponses.clearCheck();
                }
                if (!backspacing && getQuestion().getValidation() != null && getQuestion()
                        .getValidation().getValidationType().equals(
                                Validation.Type.VERHOEFF.toString())) {
                    mFreeText.removeTextChangedListener(this);
                    mFreeText.setText(ParticipantIdValidator.formatText(s.toString()));
                    mFreeText.setSelection(mFreeText.getText().length());
                    mFreeText.addTextChangedListener(this);
                }
            }
        });
        questionComponent.addView(mFreeText);
    }

    protected void beforeAddViewHook(EditText editText) {
        if (getQuestion().getValidation() != null && getQuestion().getValidation()
                .getValidationType().equals(Validation.Type.VERHOEFF.toString())) {
            mFreeText.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
        }
    }

    @Override
    protected void deserialize(String responseText) {
        mFreeText.setText(responseText);
    }

    @Override
    protected String serialize() {
        return mText;
    }

    @Override
    protected void unSetResponse() {
        mFreeText.setText("");
        mResponse.setResponse("");
    }
}
@@ -149,16 +149,41 @@
 atcher;%0A
+import android.util.Log;%0A
 import a
@@ -407,16 +407,76 @@
 dation;%0A
+import org.adaptlab.chpir.android.survey.utils.FormatUtils;%0A
 import o
@@ -754,437 +754,47 @@
 xt;%0A
-%0A
     
-@Override%0A    public void createQuestionComponent(ViewGroup questionComponent) %7B%0A        mFreeText = new EditText(getActivity());%0A        mFreeText.setInputType(InputType.TYPE_CLASS_TEXT %7C InputType.TYPE_TEXT_FLAG_MULTI_LINE %7C%0A                InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);%0A        beforeAddViewHook(mFreeText);%0A        mFreeText.setHint(R.string.free_response_edittext);%0A        mFreeText.addTextChangedListener(
+private TextWatcher mTextWatcher = 
 new 
@@ -817,20 +817,16 @@
         
-    
 private 
@@ -862,20 +862,16 @@
         
-    
 // Requi
@@ -887,20 +887,16 @@
 terface%0A
-    
         
@@ -980,35 +980,27 @@
 ) %7B%0A        
-   
- 
 %7D%0A%0A
-    
         publ
@@ -1069,24 +1069,78 @@
 nt count) %7B%0A
+            if (!FormatUtils.isEmpty(s.toString())) %7B%0A
             
@@ -1256,29 +1256,35 @@
           %7D%0A
-%0A
+    
     
+%7D%0A%0A
         publ
@@ -1330,28 +1330,24 @@
             
- 
-   
 if (mSpecial
@@ -1401,20 +1401,16 @@
         
-    
 mSpecial
@@ -1441,34 +1441,26 @@
             
- 
-   %7D%0A    
+%7D%0A
             
@@ -1546,36 +1546,32 @@
                 
-    
 .getValidation()
@@ -1599,20 +1599,16 @@
 equals(%0A
-    
         
@@ -1675,36 +1675,32 @@
                 
-    
 mFreeText.remove
@@ -1718,36 +1718,32 @@
 Listener(this);%0A
-    
                 
@@ -1818,36 +1818,32 @@
                 
-    
 mFreeText.setSel
@@ -1888,36 +1888,32 @@
                 
-   
- 
 mFreeText.addTex
@@ -1936,32 +1936,38 @@
 (this);%0A        
+    %7D%0A
         %7D%0A      
@@ -1964,35 +1964,460 @@
   %7D%0A    
-        %7D%0A        %7D
+%7D;%0A%0A    @Override%0A    public void createQuestionComponent(ViewGroup questionComponent) %7B%0A        mFreeText = new EditText(getActivity());%0A        mFreeText.setInputType(InputType.TYPE_CLASS_TEXT %7C InputType.TYPE_TEXT_FLAG_MULTI_LINE %7C%0A                InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);%0A        beforeAddViewHook(mFreeText);%0A        mFreeText.setHint(R.string.free_response_edittext);%0A        mFreeText.addTextChangedListener(mTextWatcher
 );%0A     
@@ -3050,53 +3050,168 @@
 ext.
-setText(%22%22);%0A        mResponse.setResponse(%22%22
+removeTextChangedListener(mTextWatcher);%0A        mFreeText.setText(%22%22);%0A        mResponse.setResponse(%22%22);%0A        mFreeText.addTextChangedListener(mTextWatcher
 );%0A 

fix free response special response error