/*
 * Copyright (C) 2009 University of Washington
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package org.odk.collect.android.widgets;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.commcare.android.util.StringUtils;
import org.commcare.dalvik.R;
import org.javarosa.form.api.FormEntryPrompt;
import org.odk.collect.android.activities.FormEntryActivity;
import org.odk.collect.android.jr.extensions.IntentCallout;


/**
 * Widget that allows user to scan barcodes and add them to the form.
 *
 * @author Yaw Anokwa (yanokwa@gmail.com)
 */

public class BarcodeWidget extends IntentWidget implements IBinaryWidget {

    private TextView mStringAnswer;

    public BarcodeWidget(Context context, FormEntryPrompt prompt, Intent i, IntentCallout ic) {
        super(context, prompt, i, ic, FormEntryActivity.BARCODE_CAPTURE);

        mWaitingForData = false;
        setOrientation(LinearLayout.VERTICAL);
    }

    @Override
    public void makeButton(FormEntryPrompt prompt){
        setOrientation(LinearLayout.VERTICAL);
        launchIntentButton = new Button(getContext());
        WidgetUtils.setupButton(launchIntentButton,
                StringUtils.getStringSpannableRobust(getContext(), R.string.get_barcode),
                mAnswerFontsize,
                !prompt.isReadOnly());

        // launch barcode capture intent on click
        launchIntentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent("com.google.zxing.client.android.SCAN");
                mWaitingForData = true;
                try {
                    ((Activity)getContext()).startActivityForResult(i,
                            FormEntryActivity.BARCODE_CAPTURE);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(getContext(),
                            StringUtils.getStringSpannableRobust(getContext(),
                                    R.string.barcode_scanner_error),
                            Toast.LENGTH_SHORT).show();
                    mWaitingForData = false;
                }
            }
        });
        addView(launchIntentButton);
    }


    @Override
    public void clearAnswer() {
        mStringAnswer.setText(null);
        launchIntentButton.setText(StringUtils.getStringSpannableRobust(getContext(), R.string.get_barcode));
    }

    @Override
    public void makeTextView(FormEntryPrompt prompt){
        if("editable".equals(ic.getAppearance())){
            // set text formatting
            mStringAnswer = new EditText(getContext());
            mStringAnswer.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontsize);
            mStringAnswer.setGravity(Gravity.CENTER);

            String s = prompt.getAnswerText();
            if (s != null) {
                mStringAnswer.setText(s);
            }
            // finish complex layout
            addView(mStringAnswer);
        } else{
            super.makeTextView(prompt);
        }
    }

}

@@ -1475,16 +1475,60 @@
 dget %7B%0A%0A
+    private final Button mGetBarcodeButton;%0A
     priv
@@ -1811,148 +1811,61 @@
 L);%0A
+%0A
     
-%7D%0A%0A
     
-@Override%0A    public void makeB
+// set b
 utton
-(FormEntryPrompt prompt)%7B%0A        setOrientation(LinearLayout.VERTICAL);%0A        launchIntent
+ formatting%0A        mGetBarcode
 Butt
@@ -1927,28 +1927,27 @@
 pButton(
-launchIntent
+mGetBarcode
 Button,%0A
@@ -2163,36 +2163,35 @@
 ick%0A        
-launchIntent
+mGetBarcode
 Button.setOn
@@ -2971,24 +2971,25 @@
     %7D);%0A
+%0A
         
 addView(
@@ -2984,34 +2984,566 @@
     
-addView(launchIntentButton
+// set text formatting%0A        mStringAnswer = new TextView(getContext());%0A        mStringAnswer.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontsize);%0A        mStringAnswer.setGravity(Gravity.CENTER);%0A%0A        String s = prompt.getAnswerText();%0A        if (s != null) %7B%0A            mGetBarcodeButton.setText(StringUtils.getStringSpannableRobust(getContext(),%0A                    R.string.replace_barcode));%0A            mStringAnswer.setText(s);%0A        %7D%0A        // finish complex layout%0A        addView(mGetBarcodeButton);%0A        addView(mStringAnswer
 );%0A 
@@ -3644,20 +3644,19 @@
     
-launchIntent
+mGetBarcode
 Butt

Revert "fix barcode widget"