package com.example.patrice.scanner_barcode01;

/**
 * Created by Patrice on 06/12/2017.
 */

import java.util.ArrayList;

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

import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;

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

//compile 'com.journeyapps:zxing-android-embedded:2.0.1@aar'
//compile 'com.journeyapps:zxing-android-integration:2.0.1@aar'
//compile 'com.google.zxing:core:3.0.1'

public class ListCodeActivity extends AppCompatActivity {

    private Button button;

    Parcelable mListInstanceState;

    ListView mListView;

    /** Items entered by the user is stored in this ArrayList variable */
    ArrayList<String> list = new ArrayList<String>();

    /** Declaring an ArrayAdapter to set items to ListView */
    ArrayAdapter<String> adapter;


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

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

        setContentView(R.layout.listcode_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.set setScanningRectangle(300, 300);

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

                //integrator.setBarcodeImageEnabled(true);

                integrator.initiateScan();

// protected void onSaveInstanceState(Bundle outState)
               // https://newfivefour.com/android-save-list-position-rotation-backpress.html
// https://stackoverflow.com/questions/28093124/how-to-retain-state-of-listview-during-orientation-changes

                // http://windrealm.org/tutorials/android/android-listview.php

                // https://www.numetriclabz.com/android-qr-code-scanner-using-zxingscanner-library-tutorial/
// https://www.programcreek.com/java-api-examples/index.php?class=android.widget.ListView&method=onSaveInstanceState

                https://futurestud.io/tutorials/how-to-save-and-restore-the-scroll-position-and-state-of-a-android-listview

                http://android-er.blogspot.fr/2011/09/onsaveinstancestate-and.html

                http://www.worldbestlearningcenter.com/tips/Android-save-instance-state.htm

            }
        });




        mListView.setAdapter(adapter);

        // https://www.coderefer.com/android-listview-example-tutorial/

    }

    //android:configChanges="orientation|screenSize"

    @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();

                //list.add(edit.getText().toString());
                //edit.setText("");

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

                adapter.notifyDataSetChanged();

                mListView.onRestoreInstanceState(mListInstanceState);



            }
        } else {
            // This is important, otherwise the result will not be passed to the fragment
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

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


}

