package app.mar.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Handler;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import app.mar.ui.RotateButton;
import app.mar.utils.AndroidResources;
import app.mar.game.Game;

public  class  MenuActivity extends Activity implements View.OnClickListener {

    protected int mWidth;
    protected int mHeight;
    protected Game mGame;

    protected RelativeLayout mRootLayout;
    protected Handler mCustomHandler = new Handler();
    //private final int COLOR_BACKGROUND = ;
    protected final int COLOR_SHAPE = R.color.dull_2;
    protected final int COLOR_BACKGROUND_SECOND = R.color.dull_3;
    protected final int COLOR_OTHER = R.color.dull_4;
    protected final int COLOR_TEXT = R.color.dull_5;

    protected Button mIbScanner;
    protected Button mBBriefing;
    protected Button mBInvotory;
    protected Button mBMap;
    protected Button mBOptions;
    protected View mBMenu;
    protected Button mBSend;
    protected int mHOffset=0;


    protected  void refreshMenuStyle(){}

    protected void setUpMenuStyle(){}

    private void updateSize()
    {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        mWidth = size.x;
        mHeight = size.y;
        mHOffset=mHeight/15;
    }

    protected void moveView(View v, int x, int y, int w, int h)
    {
        RelativeLayout.LayoutParams l = new RelativeLayout.LayoutParams(w, h);
        l.leftMargin = x;
        l.topMargin = y;
        v.setLayoutParams(l);
    }

    protected void moveView(View v, int x, int y)
    {
        moveView(v, x, y, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }


    protected Button newButton(String text, float angle)
    {
        Button b = new RotateButton(this,angle);
        b.setText(text);
        mRootLayout.addView(b);
        b.setBackgroundResource(R.drawable.uibutton);
        b.setTextColor(getResources().getColor(R.color.dull_4));
        b.setTextSize(18);
        b.setOnClickListener(this);
        return b;
    }

    protected Button newButton(String text)
    {
        return newButton(text, 0);
    }



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

        setContentView(R.layout.activity_new_menu);
        mRootLayout = (RelativeLayout) findViewById(R.id.menu_root);
        if(Game.game()==null)
        {
            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é");
            }
        }





        updateSize();
        //Settings.setSettings(mGame.getSettings());


        ////// A virer dela

        //startChallenge();

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

        /// a dela


        setUpMenuStyle();
        refreshMenuStyle();

        mCustomHandler.postDelayed(updateTimerThread, 10);
    }


    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 = AndroidResources.getMapIntent(this, mGame);
        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 onCredits(View v)
    {
        Intent intent = new Intent(this, CreditsActivity.class);
        startActivity(intent);
    }


    public void onInfoClick(View v) {
        /*Stage s = mGame.getCurrentStage();
        Resource r = (s!=null)?s.getResource(): null;

        Intent intent = AndroidResources.getViewerIntent(this, r);
        startActivity(intent);*/
    }



    @Override
    public void onClick(View view) {
        if(view == mBBriefing) onInfoClick(view);
        else if(view == mIbScanner) onScannerClick(view);
        else if(view == mBMap) onCarteClick(view);
        else if(view == mBInvotory) onInventaireClick(view);
        else if(view == mBOptions) onOptionsClick(view);
    }

    public void onResume()
    {
        super.onResume();
        mGame = Game.game();
        refreshMenuStyle();
    }


    private Runnable updateTimerThread = new Runnable() {

        public void run() {

            mCustomHandler.postDelayed(this, 10);
        }

    };

    public static int dpToPx(int dp)
    {
        return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
    }

    public static int pxToDp(int px)
    {
        return (int) (px / Resources.getSystem().getDisplayMetrics().density);
    }
}
