package app.brest.testmin3d;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;

import app.brest.utils.app.brest.game.Game;

public class TransferActivity extends Activity {

    private Handler mCustomHandler = new Handler();
    private ProgressBar mBar;
    private ImageButton mImage;
    private int mCurrentPos = 0;
    private final int MAX_POS=210;
    private final int PERIODE_SEND=35;
    private Activity mSelf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer);
        mBar = (ProgressBar) findViewById(R.id.pb_bar);
        mImage = (ImageButton) findViewById(R.id.ib_logo);
        mBar.setMax(MAX_POS);
        mBar.getProgressDrawable().setColorFilter(
                Color.BLUE, android.graphics.PorterDuff.Mode.SRC_IN);
        mSelf=this;
        mCustomHandler.postDelayed(updateTimerThread, 10);
    }

    private Runnable updateTimerThread = new Runnable() {

        public void run() {

            mBar.setProgress(mCurrentPos++);
            mImage.setAlpha(alpha());
            if(mCurrentPos < MAX_POS) mCustomHandler.postDelayed(this, 10);
            else
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(mSelf);

                builder.setMessage("Données, envoyées, le professeur à envoyer un message")
                        .setTitle("Données, envoyée");
                builder.setPositiveButton("Voir", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        if(Game.game().getCurrentStage()-1==Game.game().getNStages())
                        {
                            Game.game().finish();
                            Toast.makeText(mSelf, "Finished !!!!!!!", Toast.LENGTH_LONG).show();
                        }
                        finish();
                    }
                });

                AlertDialog dialog = builder.create();
                try{
                    dialog.show();
                }catch(Exception e)
                {

                }



            }
        }

    };


    private float alpha()
    {
        int ix = mCurrentPos % PERIODE_SEND;
        float x= ((float)mCurrentPos)/ ((float)PERIODE_SEND);
        Log.e("func__", "func = "+x+" -> "+(float)(Math.cos(x)*0.8+0.2));
        return (float)(Math.cos(x)*0.4+0.6);
    }
}
