package com.example.patrice.rerservation01;



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

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

public class persistance01 extends AppCompatActivity
{
    ArrayAdapter<String> adapter;
    ArrayList<String> itemList;
    ListView listV;

    private static final String STATE_LIST = "State Adapter Data";

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

        setContentView(R.layout.scanner_barcode02_layout);

        listV = (ListView)findViewById(R.id.list);

        if (savedInstanceState != null)
        {
            itemList = (ArrayList<String>) savedInstanceState.getSerializable(STATE_LIST);
        }
        else
        {
//            String[] items={"Apple","Banana","Clementine"};
            String[] items={};

            itemList = new ArrayList<String>(Arrays.asList(items));
        }

        adapter = new ArrayAdapter<String>(this,R.layout.list_item02,R.id.txtview,itemList);

        //   adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, itemList);

        final Activity activity = this;


        Button btAdd=(Button)findViewById(R.id.btAdd);

        btAdd.setOnClickListener(new View.OnClickListener()
        {
            /*
                        @Override
                        public void onClick(View v)
                        {
                            String newItem=editText.getText().toString();
                            itemList.add(newItem);
                            adapter.notifyDataSetChanged();
                        }
            */
            @Override
            public void onClick(View v)
            {
                IntentIntegrator integrator = new IntentIntegrator(activity);
                integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
                integrator.setOrientationLocked(true);

                integrator.setPrompt("Scan le code bar de la boîte de jeu");
                integrator.setCameraId(0);
                integrator.setBeepEnabled(false);
                //     integrator.setBarcodeImageEnabled(false);

                integrator.initiateScan();
            }

        });

        listV.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                String value = listV.getItemAtPosition(position).toString();
                int pos = parent.getPositionForView(view);
                Toast.makeText(persistance01.this ,pos + "",Toast.LENGTH_SHORT).show();
            }
        });

        listV.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, "Pas de scan !.", Toast.LENGTH_LONG).show();
            }

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

                String toto = result.getContents();
                itemList.add(toto);
                adapter.notifyDataSetChanged();
            }
        }
        else
        {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        outState.putSerializable(STATE_LIST, itemList);
    }


}