// Bandit Manchot, by Seb Luca, UNamur, June 2018

package com.projet.sluca.banditmanchot;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class LooseActivity extends Activity implements View.OnClickListener
{
    GlobalDatas globalDatas; // Appel de la classe traitant les variables globales.

    private static int REQUEST_CODE = 1;
    private Button button; // Bouton de retour au Menu Principal.
    private Context context;
    static MediaPlayer sound = new MediaPlayer();

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loose);
        context = this;

        // Etablissement de la liaison avec les variables globales.
        globalDatas = (GlobalDatas) getApplication();

        // Rapatriement des statistiques.
        Integer victories   = globalDatas.getNbrVictories();
        Integer defeats     = globalDatas.getNbrDefeats();
        Integer creditsWon  = globalDatas.getCreditsWon();
        Integer creditsLost = globalDatas.getCreditsLost();

        // affichage des statistiques de jeu
        TextView stats = (TextView) findViewById(R.id.stats);
        stats.setText(victories + "\n" + defeats + "\n" + creditsWon + "\n" + creditsLost);

        button = (Button)findViewById(R.id.btnMenu);
        button.setOnClickListener(this);

        // Jingle de défaite.
        sound = MediaPlayer.create(LooseActivity.this,R.raw.lose);
        sound.start();
    }

    @Override
    public void onClick(View paramView) // Si clic sur le bouton.
    {
        // Bruitage.
        sound = MediaPlayer.create(LooseActivity.this,R.raw.buttonstart);
        sound.start();

        Intent intent = new Intent(context, MainActivity.class); // pointe vers MainActivity.
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        // Retour au menu principal
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) // Lance l'activité.
        {
            // Réinitialisation de l'appli.
            Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
        }
    }
}
