package app.brest.testmin3d;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Point;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import app.brest.app.brest.ui.RotateButton;
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 NewMenuActivity extends Activity implements View.OnClickListener{

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

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

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

    private void setUpStyle()
    {
        mRootLayout.setBackgroundResource(R.drawable.background);
        int h4 = mHeight/3;
        int w4 = mWidth/3;
        int w = (int)(w4*1.25);
        int h = dpToPx((int)(mBBriefing.getTextSize()*1.5));

        moveView(mBBriefing, w4-2*w/3, mHOffset+h4-h,w,h);
        moveView(mBInvotory, 2*w4-w/3, mHOffset+h4-h,w,h);
        moveView(mBMap, w4-2*w/3, mHOffset+2*h4,w,h);
        moveView(mBOptions, 2*w4-w/3, mHOffset+2*h4,w,h);
        moveView(mBSend, mWidth/2-w, mHOffset+2*h4+4*h/3, 2*w, h);
        mBMenu.setTextSize(25);
        mBMenu.setTypeface(null,Typeface.BOLD);
        sendVisibility();

    }



    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(22);
        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);

        Game g = 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.getmSettings());

        mIbScanner = new Button(this);
        //mIbScanner.setImageResource(R.drawable.scanner);
        //mIbScanner.setScaleType(ImageView.ScaleType.FIT_CENTER);
        mRootLayout.addView(mIbScanner);
        mIbScanner.setBackgroundResource(R.drawable.uibuttoncircle);
        mIbScanner.setText("Scanner");
        mIbScanner.setTextColor(getResources().getColor(R.color.dull_4));
        mIbScanner.setTextSize(22);
        mIbScanner.setOnClickListener(this);

        moveView(mIbScanner, mWidth/2-mHeight/8, mHOffset+mHeight/2-mHeight/8, mHeight/4, mHeight/4);

        mBBriefing = newButton("Briefing");
        mBInvotory = newButton("Inventaire");
        mBMap = newButton("Carte");
        mBOptions = newButton("Options");
        mBMenu = newButton("Menu Principal");
        mBMenu.setBackgroundResource(R.drawable.title);
        mBSend = newButton("Envoyer les données");
        Resources r = getResources();
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 70, r.getDisplayMetrics());
        moveView(mBMenu, 0, 0, mWidth, (int)px);
        //mBSend = newButton("Envoyer");


        setUpStyle();
        mCustomHandler.postDelayed(updateTimerThread, 10);
/*
        mGame.pickResource("aa");
        mGame.pickResource("ab");
        mGame.pickResource("ac");
        mGame.pickResource("ad");
        mGame.pickResource("ae");
        mGame.pickResource("ag");
        mGame.pickResource("ah");
        mGame.pickResource("ai");
        mGame.nextStage();
        mGame.pickResource("ba");
        mGame.pickResource("bb");
        mGame.pickResource("bc");
        mGame.pickResource("bd");
        mGame.pickResource("be");
        mGame.pickResource("bf");
        mGame.pickResource("bg");
        mGame.pickResource("bh");
        mGame.nextStage();
        mGame.pickResource("ca");
        mGame.pickResource("cb");
        mGame.pickResource("cc");
        mGame.pickResource("cd");
        mGame.pickResource("ce");
        mGame.pickResource("cf");
        mGame.pickResource("cg");
*/
    }

    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);
    }

    @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 == mBSend) onSendClick(view);
        else if(view == mBInvotory) onInventaireClick(view);
        else if(view == mBOptions) onOptionsClick(view);
    }

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

    private void sendVisibility()
    {
        if(mGame.canSendData()) mBSend.setVisibility(View.VISIBLE);
        else mBSend.setVisibility(View.INVISIBLE);
    }

    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);
    }


}
