package app.brest.testmin3d;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import app.brest.testmin3d.R;
import app.brest.utils.Settings;
import app.brest.utils.app.brest.game.Game;
import app.brest.utils.app.brest.game.Resource;
import app.brest.utils.app.brest.game.Stage;

public class MenuActivity extends Activity {
    protected Game mGame;
    protected ImageButton mICarte;
    protected ImageButton mIInventaire;
    protected ImageButton mIOptions;
    protected ImageButton mIScanner;
    protected ImageButton mIBriefing;
    protected ImageButton mINextStage;


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

        mICarte = (ImageButton) findViewById(R.id.ib_carte);
        mIInventaire = (ImageButton) findViewById(R.id.ib_inventaire);
        mIOptions = (ImageButton) findViewById(R.id.ib_options);
        mIScanner = (ImageButton) findViewById(R.id.ib_scanner);
        mIBriefing = (ImageButton) findViewById(R.id.ib_briefing);
        mINextStage = (ImageButton) findViewById(R.id.ib_send);

        Game g = null;//Game.load(this);

        if (g!= null && mGame==null) {
            mGame = g;
            Game.setGame(mGame);
            mGame.newSensorManager(this);
            Toast.makeText(this, "Chargement", Toast.LENGTH_LONG).show();
        }
        else if( mGame==null)
        {
            mGame = new Game("game_medium", this);
            Game.setGame(mGame);
            Toast.makeText(this, "Nouveau Jeu", Toast.LENGTH_LONG).show();
        }else if( mGame==null)
        {
            Toast.makeText(this, "Erreur", Toast.LENGTH_LONG).show();
            throw new Error("Le jeu n'est pas sauvegardé");
        }
        if(mGame!=null)
        {
            /*
            mGame.pickR  esource("b");
            mGame.pickResource("c");
            mGame.pickResource("d");
            mGame.pickResource("e");
            */
        }

        Intent intent = new Intent(this, NewMenuActivity.class);
        startActivity(intent);
    }

    public void onResume()
    {
        super.onResume();
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        int buttonHieght = mGame.canSendData()?((height)-6*40)/7:((height-40)-5*40)/6;
        ((ViewGroup)mINextStage.getParent()).setVisibility(mGame.canSendData()?View.VISIBLE:View.INVISIBLE);

        mICarte.setLayoutParams(new LinearLayout.LayoutParams(buttonHieght, buttonHieght));
        mIInventaire.setLayoutParams(new LinearLayout.LayoutParams(buttonHieght, buttonHieght));
        mIOptions.setLayoutParams(new LinearLayout.LayoutParams(buttonHieght, buttonHieght));
        mIScanner.setLayoutParams(new LinearLayout.LayoutParams(buttonHieght, buttonHieght));
        mIBriefing.setLayoutParams(new LinearLayout.LayoutParams(buttonHieght, buttonHieght));
        if(mGame.canSendData())mINextStage.setLayoutParams(new LinearLayout.LayoutParams(buttonHieght, buttonHieght));
    }

    public void onOptionsClick(View v)
    {
        Intent intent = new Intent(this, SettingsActivity.class);
        intent.putExtra("game", mGame);
        startActivity(intent);
    }


    public void onCarteClick(View v)
    {
        Intent intent = new Intent(this, ViewerActivity.class);
        intent.putExtra("map", mGame.getName()+"_map");
        startActivity(intent);
    }


    public void onScannerClick(View v)
    {
        Intent intent = new Intent(this, ARActivity.class);
        intent.putExtra("game", mGame);
        startActivity(intent);
    }


    public void onInventaireClick(View v)
    {
        Intent intent = new Intent(this, ResourceListActivity.class);
        intent.putExtra("game", mGame);
        startActivity(intent);
    }


    public void onInfoClick(View v) {
        Intent intent = new Intent(this, ViewerActivity.class);
        Stage s = mGame.getCurrentStageObj();
        Resource r = (s!=null)?s.getResource(): null;
        intent.putExtra("resource", r );
        startActivity(intent);
    }


    public void onSendClick(View v)
    {
        //TODO : Transmssion des donnes
        boolean finished = mGame.nextStage();

        Stage s = mGame.getCurrentStageObj();
        Resource stageRes = s.getResource();

        if(finished)
        {
            Intent intent2 = new Intent(this, EndActivity.class);
            startActivity(intent2);
        }

        Intent intent = new Intent(this, ViewerActivity.class);
        intent.putExtra("resource", stageRes );
        startActivity(intent);


        Intent intent2 = new Intent(this, TransferActivity.class);
        startActivity(intent2);
    }



}
