package com.example.patrice.rerservation01;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.patrice.rerservation01.vocale.reconnaissance_vocale01;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import java.io.File;


public class gestion_DB extends AppCompatActivity  implements View.OnClickListener
{
    Button btn_acquisition_barcode;
    Button btn_acquisition_photo;
    Button btn_saisir_name;
    Button btn_recherche;
    Button btn_association;
    TextView tv_nom_jeu;
    TextView tv_ean_barcode;
    TextView tv_titre_image;
    ImageView simpleImageView;

    int request_saisir_nom = 125;
    int request_capture_image = 150;

    SharedPreferences sharedpreferences;
    public static final String MyPREFERENCES = "MyPrefs" ;
    public static final String KEY_code_barre = "";
    public static final String KEY_capture_image_filename = "";

    store id_class;

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

        setContentView(R.layout.test);

        getSupportActionBar().setTitle("Gestion de la DB");
        //getSupportActionBar().setLogo(R.drawable.listing_jeux);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);


        btn_acquisition_barcode = (Button) findViewById(R.id.btn_acquisition_barcode);
        btn_acquisition_barcode.setOnClickListener(this);

        btn_acquisition_photo = (Button) findViewById(R.id.btn_acquisition_photo);
        btn_acquisition_photo.setOnClickListener(this);

        btn_saisir_name = (Button) findViewById(R.id.btn_saisir_name);
        btn_saisir_name.setOnClickListener(this);

        btn_recherche = (Button) findViewById(R.id.btn_recherche);
        btn_recherche.setOnClickListener(this);

        btn_association = (Button) findViewById(R.id.btn_association);
        btn_association.setOnClickListener(this);

        simpleImageView = (ImageView) findViewById(R.id.simpleImageView);


        tv_titre_image = (TextView) findViewById(R.id.tv_titre_image);
        tv_nom_jeu = (TextView) findViewById(R.id.tv_nom_jeu);
        tv_ean_barcode = (TextView) findViewById(R.id.tv_ean_barcode);

        btn_acquisition_barcode.setEnabled(true);
        btn_acquisition_photo.setEnabled(false);
        btn_saisir_name.setEnabled(false);
        btn_recherche.setEnabled(false);
        btn_association.setEnabled(false);


        // remise à zéro
        /*
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedpreferences.edit();

        editor.putString(KEY_code_barre, "");
        editor.putString(KEY_capture_image_filename, "");
        editor.commit();
*/
        //editor.remove(code_barre);              // will delete key name
        //editor.remove(capture_image_filename);  // will delete key email
        //editor.commit(); // commit changes
        //editor.clear();
        //editor.commit(); // commit changes
/*
        String tag_code_barre = sharedpreferences.getString(KEY_code_barre, null);
        String tag_capture_image_filename = sharedpreferences.getString(KEY_capture_image_filename, null);
*/
        id_class = new store();

    }

    @Override
    public void onClick(View V)
    {
        int id = V.getId();
        Intent i;

        switch(id)
        {

            case R.id.btn_acquisition_barcode:

                Toast.makeText(this, "Barcode... ", Toast.LENGTH_LONG).show();

                IntentIntegrator integrator = new IntentIntegrator(gestion_DB.this);
                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.initiateScan();

                break;

            case R.id.btn_acquisition_photo:
                Toast.makeText(this, "Photo... ", Toast.LENGTH_LONG).show();
                i = new Intent(getApplicationContext(), camera_capture_image.class);
                startActivityForResult(i, request_capture_image );
                break;

            case R.id.btn_saisir_name:
                Toast.makeText(this, "Reconnaissance... ", Toast.LENGTH_LONG).show();
                i = new Intent(getApplicationContext(), reconnaissance_vocale01.class);
                startActivity(i);
                break;

            case R.id.btn_recherche:
                Toast.makeText(this, "Saisir le nom... ", Toast.LENGTH_LONG).show();
                i = new Intent(getApplicationContext(), saisir_nom.class);
                startActivity(i);
                break;

            case R.id.btn_association:
                Toast.makeText(this, "Association... ", Toast.LENGTH_LONG).show();
                //i = new Intent(getApplicationContext(), association.class);
                //i = new Intent(getApplicationContext(), work01.class);
                //i = new Intent(getApplicationContext(), wifi.class);
                i = new Intent(getApplicationContext(), load_imageview.class);
                startActivity(i);


                //startActivityForResult(i, request_capture_image );
                break;
        }
    }


    @Override
    protected void onResume()
    {

        super.onResume();

        String field_code = id_class.getCode();
        String field_filename = id_class.getFilename();

        if(!field_code.equals(""))
        {
            tv_ean_barcode.setText( field_code );
        }

        if(!field_filename.equals(""))
        {
            Bitmap bitmap = BitmapFactory.decodeFile( field_filename); // Environment.getExternalStorageDirectory().getPath() + field_filename );
            simpleImageView.setImageBitmap(bitmap);

            String aqw = "";
           // tv_titre_image.setText(field_filename);
        }
/*
        String tag_code_barre = sharedpreferences.getString(KEY_code_barre, null);
        String tag_capture_image_filename = sharedpreferences.getString(KEY_capture_image_filename, null);

        if(!tag_code_barre.equals("") ) // || tag_code_barre!=null
        {
            tv_ean_barcode.setText( tag_code_barre );
        }

        if(!tag_capture_image_filename.equals("")  ) // || tag_capture_image_filename!=null
         {

            Bitmap bitmap = BitmapFactory.decodeFile( Environment.getExternalStorageDirectory().getPath() + tag_capture_image_filename );

            int w = bitmap.getWidth();
            int l = bitmap.getHeight();

            simpleImageView.setImageBitmap(bitmap);
            tv_titre_image.setText(tag_capture_image_filename);

        }
*/

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);


/*
        String tag_code_barre = sharedpreferences.getString(code_barre, "");
        String tag_capture_image_filename = sharedpreferences.getString(capture_image_filename, "");

        if(!tag_code_barre.equals("") )
        {
            tv_ean_barcode.setText( tag_code_barre );
        }

        if(!tag_capture_image_filename.equals("") )
        {

            Bitmap bitmap = BitmapFactory.decodeFile( Environment.getExternalStorageDirectory().getPath() + tag_capture_image_filename );

            int w = bitmap.getWidth();
            int l = bitmap.getHeight();

            simpleImageView.setImageBitmap(bitmap);
            tv_titre_image.setText(tag_capture_image_filename);

        }
*/
        if(requestCode == request_saisir_nom)
        {
            String nom_jeu = data.getStringExtra("TAG_NOM_JEU");
            String position = data.getStringExtra("TAG_position");

            tv_nom_jeu.setText(nom_jeu);
            //Toast.makeText(this, "++++ " + nom_jeu + "  " + position, Toast.LENGTH_LONG).show();

        }
        if(requestCode == request_capture_image)
        {
            String filename =data.getStringExtra("TAG_FILENAME");
            if(filename.equals(""))
            {
                Toast.makeText(this, "Vide", Toast.LENGTH_LONG).show();
            }
            else
            {
                Toast.makeText(this, filename, Toast.LENGTH_LONG).show();
/*
                filename = File.separator + "patoche_image_05-03-2018__11:00:55" + ".jpeg";
                String sdPath = Environment.getExternalStorageDirectory() + filename;
                File imgFile = new File(sdPath);

                if(imgFile.exists())
                {
                    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                    simpleImageView.setImageBitmap(myBitmap);
                }
*/
/*
                Bitmap bitmap = BitmapFactory.decodeFile( Environment.getExternalStorageDirectory().getPath() + filename );

                int w = bitmap.getWidth();
                int l = bitmap.getHeight();

                simpleImageView.setImageBitmap(bitmap);

                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putString(KEY_capture_image_filename, filename);
                editor.commit();
*/


                Bitmap bitmap = BitmapFactory.decodeFile( Environment.getExternalStorageDirectory().getPath() + filename );

                String path = Environment.getExternalStorageDirectory().getPath() + filename;
                id_class.setFilename(path);

                //https://androidexample.com/Android_SharedPreferences_Basics/index.php?view=article_discription&aid=126
                //https://exceptionshub.com/android-check-if-value-exists-in-shared-preferences.html

                //tv_titre_image.setText(filename);


//                int w = bitmap.getWidth();
//                int l = bitmap.getHeight();

//                simpleImageView.setImageBitmap(bitmap);
/*
                String path = filename;

                File sdcard_path = Environment.getExternalStorageDirectory();

                String sdPath = Environment.getExternalStorageDirectory() + "/" + filename;

                Bitmap xxxbitmap = BitmapFactory.decodeFile(sdPath);
                int largeur = xxxbitmap.getWidth();
                int hauteur = xxxbitmap.getHeight();
                //imageView.setImageBitmap(BitmapFactory.decodeFile(filename));
*/
            }
        }


        else
        {
            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("------------> >>>> ", "Scanned");
                    Toast.makeText(this, "Scanned from camera : " + result.getContents(), Toast.LENGTH_LONG).show();
                    //tv_ean_barcode.setText(result.getContents());
/*
                    SharedPreferences.Editor editor = sharedpreferences.edit();
                    editor.putString(KEY_code_barre, val);
                    editor.commit();
                    String tag_code_barre = sharedpreferences.getString(KEY_code_barre, null);
                    String tag_capture_image_filename = sharedpreferences.getString(KEY_capture_image_filename, null);
*/
                    String val = result.getContents();
                    id_class.setCode(val);
                    btn_acquisition_photo.setEnabled(true);

                }
            }

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

    }

    // pour le retour - inclus dans l'action bar ( < )
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

class store
{
    private String code_barre = "";
    private String filename = "";

    public void setCode(String code_barre)
    {
        this.code_barre = code_barre;
    }

    public String getCode()
    {
        return code_barre;
    }

    public void setFilename(String filename)
    {
        this.filename = filename;
    }

    public String getFilename()
    {
        return filename;
    }




}

