package com.example.patrice.rerservation01;


import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class acceuil extends AppCompatActivity implements View.OnClickListener
{
    Button btn_liste_jeux_database, btn_emprunter_jeu_DB, btn_emprunter_jeu_scanner , btn_compte_historique, btn_a_propos, btn_quitter;


    // preferences
    SharedPreferences sharedpreferences;
    public static final String mypreference = "mypref";
    public static final String key_nom = "nom";
    public static final String key_email = "email";

    public static final String key_account = "key_account";

    private sharedPreference sharedPreference;

    Activity context = this;


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

        /* action bar */
        ActionBar actionBar = getSupportActionBar();
        getSupportActionBar().setTitle("Ludothéque CE ONSEMI");

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setLogo(R.mipmap.ic_launcher);
        getSupportActionBar().setDisplayUseLogoEnabled(true);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        /**/

        /* listener des boutons */
        btn_liste_jeux_database = (Button) findViewById(R.id.btn_liste_jeux_database);
        btn_emprunter_jeu_DB = (Button) findViewById(R.id.btn_emprunter_jeu_DB);
        btn_emprunter_jeu_scanner = (Button) findViewById(R.id.btn_emprunter_jeu_scanner);
        btn_compte_historique = (Button) findViewById(R.id.btn_compte_historique);
        btn_a_propos = (Button) findViewById(R.id.btn_a_propos);
        btn_quitter = (Button) findViewById(R.id.btn_quitter);

        btn_liste_jeux_database.setOnClickListener(this);
        btn_emprunter_jeu_DB.setOnClickListener(this);
        btn_emprunter_jeu_scanner.setOnClickListener(this);
        btn_compte_historique.setOnClickListener(this);
        btn_a_propos.setOnClickListener(this);
        btn_quitter.setOnClickListener(this);
        /**/

        /* premier run, configuration obligatoire pour le compte utilisateur */
        sharedPreference = new sharedPreference();
        boolean exist = sharedPreference.exists(context, key_account );

        if(exist == false)
        {
            startActivity(new Intent(acceuil.this, creation_compte.class));
            Toast.makeText(acceuil.this, "Création du compte - Run only once", Toast.LENGTH_LONG)  .show();
        }
        /**/

    }

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

        switch(id)
        {
            case R.id.btn_liste_jeux_database:
                Toast.makeText(this, "listing... ", Toast.LENGTH_LONG).show();
                i = new Intent(getApplicationContext(), liste_jeux_db.class);
                startActivity(i);
                break;
            case R.id.btn_emprunter_jeu_DB:
                Toast.makeText(this, "emprunter... ", Toast.LENGTH_LONG).show();
                i = new Intent(getApplicationContext(), scanner_barcode08.class);
                startActivity(i);
                break;
            case R.id.btn_emprunter_jeu_scanner:
                Toast.makeText(this, "emprunter... ", Toast.LENGTH_LONG).show();
                i = new Intent(getApplicationContext(), scanner_barcode09.class);
                startActivity(i);
                break;
            case R.id.btn_compte_historique:
                Toast.makeText(this, "historique... ", Toast.LENGTH_LONG).show();
                i = new Intent(getApplicationContext(), mes_emprunts04.class);
                startActivity(i);
                break;
            case R.id.btn_a_propos:
                Toast.makeText(this, "A propos... ", Toast.LENGTH_LONG).show();
                about();
                break;
            case R.id.btn_quitter:
                Toast.makeText(this, "Quitter... ", Toast.LENGTH_LONG).show();
                quitter_confirmation();
                break;
        }
    }

    /*****************************************************************************************************************/

    public void about()
    {

        final Dialog dialog = new Dialog(acceuil.this);
        dialog.setContentView(R.layout.about_layout);
        dialog.setTitle("A porpos:");
        dialog.show();

        Button dialogCloseButton = (Button) dialog.findViewById(R.id.close_button);
        dialogCloseButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });

    }

    /*****************************************************************************************************************/

    public void quitter_confirmation()
    {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

        alertDialogBuilder.setTitle("Confirmation");

        alertDialogBuilder
                .setMessage("Cliquer sur Oui pour quitter l'appli !.")
                .setCancelable(false)
                .setPositiveButton("Oui",new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {
                        acceuil.this.finish();
                    }
                })

                .setNegativeButton("Non",new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog,int id)
                    {
                        dialog.cancel();
                    }
                });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

    /*****************************************************************************************************************/

}

// http://android4beginners.com/2013/07/lesson-2-2-how-to-use-margins-and-paddings-in-android-layout/