/*
 * Copyright (C) The Android Open Source Project
 *
 * 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 me.kglawrence.kerilawrence.book_park;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseException;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import me.kglawrence.kerilawrence.book_park.models.Student;

/**
 * Main activity demonstrating how to pass extra parameters to an activity that
 * reads barcodes.
 */
public class MainActivity extends Activity implements View.OnClickListener {

    private boolean checkIn;

    private static final int RC_BARCODE_CAPTURE = 9001;
    private static final String TAG = "BarcodeMain";

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

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            checkIn = (boolean) bundle.get("checkin");
            Log.d("tag", "" + checkIn);
        }

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, BarcodeCaptureActivity.class);
                String studentId = Integer.toString(position);
                intent.putExtra("studentId", studentId);
                intent.putExtra("checkin", checkIn);
                startActivityForResult(intent, RC_BARCODE_CAPTURE);
            }
        });

        //statusMessage = (TextView)findViewById(R.id.status_message);
        //barcodeValue = (TextView)findViewById(R.id.barcode_value);

        //findViewById(R.id.read_barcode).setOnClickListener(this);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
    //    if (v.getId() == R.id.read_barcode) {
    //        // launch barcode activity.
    //        Intent intent = new Intent(this, BarcodeCaptureActivity.class);
    //        startActivityForResult(intent, RC_BARCODE_CAPTURE);
    //    }
    }

    /**
     * Called when an activity you launched exits, giving you the requestCode
     * you started it with, the resultCode it returned, and any additional
     * data from it.  The <var>resultCode</var> will be
     * {@link #RESULT_CANCELED} if the activity explicitly returned that,
     * didn't return any result, or crashed during its operation.
     * <p/>
     * <p>You will receive this call immediately before onResume() when your
     * activity is re-starting.
     * <p/>
     *
     * @param requestCode The integer request code originally supplied to
     *                    startActivityForResult(), allowing you to identify who this
     *                    result came from.
     * @param resultCode  The integer result code returned by the child activity
     *                    through its setResult().
     * @param data        An Intent, which can return result data to the caller
     *                    (various data can be attached to Intent "extras").
     * @see #startActivityForResult
     * @see #createPendingResult
     * @see #setResult(int)
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RC_BARCODE_CAPTURE) {
            if (resultCode == CommonStatusCodes.SUCCESS) {
                if (data != null) {
                    Barcode barcode = data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject);
                    //statusMessage.setText(R.string.barcode_success);
                    //barcodeValue.setText(barcode.displayValue);
                    Log.d(TAG, "Barcode read: " + barcode.displayValue);
                } else {
                    //statusMessage.setText(R.string.barcode_failure);
                    Log.d(TAG, "No barcode captured, intent data is null");
                }
            } //else {
                //statusMessage.setText(String.format(getString(R.string.barcode_error),
                 //       CommonStatusCodes.getStatusCodeString(resultCode)));
            //}
        }
        else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

@@ -656,16 +656,48 @@
 _park;%0A%0A
+import android.content.Context;%0A
 import a
@@ -923,16 +923,45 @@
 extView;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -4785,128 +4785,408 @@
     
-if (data != null) %7B%0A                    Barcode barcode = data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject);
+Context context = getApplicationContext();%0A                int duration = Toast.LENGTH_SHORT;%0A                Toast toast;%0A                if (data != null) %7B%0A                    Barcode barcode = data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject);%0A                    CharSequence text = %22Successfully scanned book!!%22;%0A                    toast = Toast.makeText(context, text, duration);%0A
 %0A   
@@ -5409,32 +5409,168 @@
        %7D else %7B%0A
+                    CharSequence text = %22Failed to scan barcode.%22;%0A                    toast = Toast.makeText(context, text, duration);%0A
                 
@@ -5710,32 +5710,62 @@
               %7D%0A
+                toast.show();%0A
             %7D //

add a toast for successful or failed book check