package com.example.patrice.rerservation01;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.content.Intent;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class scanner_barcode extends AppCompatActivity
{
    private Button button;

    Parcelable mListInstanceState;

    ListView mListView;

    ArrayList<String> list = new ArrayList<String>();

    ArrayAdapter<String> adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        if(savedInstanceState!=null)
        {
            mListInstanceState = savedInstanceState.getParcelable("listview.state");
        }

        setContentView(R.layout.scanner_barcode_layout);

        button = (Button) this.findViewById(R.id.btnAdd);
        mListView = (ListView) findViewById(R.id.list);

        final Activity activity = this;

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                IntentIntegrator integrator = new IntentIntegrator(activity);
                integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
                integrator.setOrientationLocked(true);

                integrator.setPrompt("Scan");
                integrator.setCameraId(0);
                integrator.setBeepEnabled(false);
          //      integrator.setBarcodeImageEnabled(false);

                integrator.initiateScan();
            }
        });

        mListView.setAdapter(adapter);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

        if(result != null)
        {
            if(result.getContents() == null)
            {
                Log.d("MainActivity", "Cancelled scan");
                Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
            }

            else
            {
                Log.d("MainActivity", "Scanned");
                Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();

                String toto = result.getContents();
                list.add(toto);

                adapter.notifyDataSetChanged();

                mListView.onRestoreInstanceState(mListInstanceState);
            }
        }
        else
        {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        outState.putParcelable("listview.state", mListView.onSaveInstanceState());
    }


}

