package org.commcarehq.aadharuid;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    int BARCODE_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        callBarcodeScanner();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == BARCODE_CODE) {
            switch (resultCode) {
                case RESULT_OK:
                    String result = data.getStringExtra("SCAN_RESULT");
                    Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
                    returnOdkResult(result);
                    break;
                case RESULT_CANCELED:
                    break;
            }

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void callBarcodeScanner() {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        try {
            this.startActivityForResult(intent, BARCODE_CODE);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(this,
                    getResources().getString(R.string.barcode_scanner_error),
                    Toast.LENGTH_SHORT).show();
        }
    }

    private void returnOdkResult(String result) {
        Intent data = new Intent();

        Bundle responses = new Bundle();
        responses.putString("example_id", "Example text");
        data.putExtra("odk_intent_bundle", responses);

        // this is the value that CommCare will use as the result of the intent question
        data.putExtra("odk_intent_data", result);
        setResult(Activity.RESULT_OK, data);
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -1633,24 +1633,80 @@
 g result) %7B%0A
+        ScanResult scanResult = new ScanResult(result);%0A
         Inte
@@ -1804,37 +1804,899 @@
 ng(%22
-example_id%22, %22Example text%22);
+rawString%22, scanResult.rawString);%0A        responses.putString(%22uid%22, scanResult.uid);%0A        responses.putString(%22name%22, scanResult.name);%0A        responses.putString(%22gender%22, scanResult.gender);%0A        responses.putString(%22yob%22, scanResult.yob);%0A        responses.putString(%22co%22, scanResult.co);%0A        responses.putString(%22house%22, scanResult.house);%0A        responses.putString(%22street%22, scanResult.street);%0A        responses.putString(%22lm%22, scanResult.lm);%0A        responses.putString(%22loc%22, scanResult.loc);%0A        responses.putString(%22vtc%22, scanResult.vtc);%0A        responses.putString(%22po%22, scanResult.po);%0A        responses.putString(%22dist%22, scanResult.dist);%0A        responses.putString(%22subdist%22, scanResult.subdist);%0A        responses.putString(%22state%22, scanResult.state);%0A        responses.putString(%22pc%22, scanResult.pc);%0A        responses.putString(%22dob%22, scanResult.dob);%0A
 %0A   

add ScanResult variables to response