package macampcorp.macamp.activities;

import android.content.Intent;
import android.graphics.Point;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import java.util.Timer;
import java.util.TimerTask;

import macampcorp.macamp.game.Bomb;
import macampcorp.macamp.game.Game;
import macampcorp.macamp.game.scheduler.SpecialTask;
import macampcorp.macamp.lang.Locale;

public class BombActivity extends AppCompatActivity {

    protected Bomb mBomb;
    protected Game mGame;
    protected int mLength;
    protected String mTyped;
    protected TextView mTvPassword;
    protected TextView mTvCar;
    protected TextView mTvBomb;
    protected Button mButtons[] = new Button[12];

    /**
     * Si vrai, c'est que c'est la fin du jeu
     * sinon c'est un essai sans avoir tout
     */
    protected boolean mIsEnd = false;

    public static final int KEY_ERASE=-1;
    public static final int KEY_OK=10;


    private Timer mTimer;
    private Runnable mTimerTick = new Runnable() {
        public void run() {
            if(!mGame.getBomb().hasExplosed())
            {
                mTvCar.setText(mGame.getBomb().getTimeBeforeBoom());
            }else
            {
                mTimer.cancel();
                mTvCar.setText("BOOM !");
                mGame.getScheduler().clear();
                mGame.getScheduler().append(SpecialTask.FINISHED_FAILED);
                finish();
            }
        }
    };

    private void timerCallback()
    {
        this.runOnUiThread(mTimerTick);
    }

    private void startTimer()
    {
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                timerCallback();
            }

        }, 0, 1000);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bomb);
        mGame = Game.game();
        mBomb = mGame.getBomb();
        mLength=mBomb.getPasswordLength();
        mTyped="";
        mTvBomb=findViewById(R.id.tv_bombe);
        mTvPassword= findViewById(R.id.tv_code);
        mTvCar= findViewById(R.id.tv_car);

        mTvBomb.setText(mBomb.getPasswordKnown());

        Typeface face = Typeface.createFromAsset(getAssets(), "fonts/font.ttf");
        mTvPassword.setTypeface(face);
        mTvCar.setTypeface(face);

        mTvPassword.setText(completeTypedString());
        startTimer();

        mButtons[0] = findViewById(R.id.button7);
        mButtons[1] = findViewById(R.id.button8);
        mButtons[2] = findViewById(R.id.button9);

        mButtons[3] = findViewById(R.id.button4);
        mButtons[4] = findViewById(R.id.button5);
        mButtons[5] = findViewById(R.id.button6);


        mButtons[6] = findViewById(R.id.button1);
        mButtons[7] = findViewById(R.id.button2);
        mButtons[8] = findViewById(R.id.button3);

        mButtons[9] = findViewById(R.id.b_erase);
        mButtons[10] = findViewById(R.id.button0);
        mButtons[11] = findViewById(R.id.buttonOk);

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int c = 70;
        int width = size.x;
        int height = size.y;

        if(height<600) c=10;
        else if(height>=600 && height<800) c=10;
        else c=10;

        for(int i=0; i<mButtons.length; i++)
        {
            mButtons[i].setHeight(c);
            mButtons[i].setWidth(c);
        }
    }

    protected String completeTypedString()
    {
        String t = "";
        for(int i=0; i<mLength; i++)
            t+=(i<mTyped.length())?mTyped.charAt(i):'-';

        return t;
    }

    protected void updatePassword()
    {
        String str = completeTypedString();
        mTvPassword.setText(str);
    }

    protected void erase()
    {
        if(mTyped.length()<1) return;
        String nouveau = "";
        for(int i=0; i<mTyped.length()-1; i++)
            nouveau+=mTyped.charAt(i);
        mTyped=nouveau;
        updatePassword();
    }

    protected void valid()
    {
        if(mLength == mTyped.length())
        {
            mTimer.cancel();

            if(mBomb.disarm(mTyped))//section critique
            { //code bon
                mGame.getScheduler().clear();
                mGame.getScheduler().append(SpecialTask.FINISHED_SUCESS);
                setResult(0);
                finish();
                Toast.makeText(this, R.string.toast_ok, Toast.LENGTH_LONG).show();
                clear();
            }else
            { //code mauvais
                Toast.makeText(this,  R.string.toast_fail, Toast.LENGTH_LONG).show();
                mGame.getBomb().addTime(-100);
                clear();
                startTimer();
            }
        }else
        Toast.makeText(this, R.string.toast_length, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onBackPressed()
    {
        setResult(0);
        finish();
    }

    protected void enterKey(char c)
    {
        if(mTyped.length()<mLength)
            mTyped+=c;
        updatePassword();
    }


    protected void clear()
    {
        mTyped="";
        updatePassword();
    }

    private void onButton(int k)
    {
        String cc ="0123456789";
        if(k>=0 && k<=9) enterKey(cc.charAt(k));
        else if(k==KEY_ERASE) erase();
        else if(k==KEY_OK) valid();
    }

    public void onButton0(View v) {onButton(0);}
    public void onButton1(View v) {onButton(1);}
    public void onButton2(View v) {onButton(2);}
    public void onButton3(View v) {onButton(3);}
    public void onButton4(View v) {onButton(4);}
    public void onButton5(View v) {onButton(5);}
    public void onButton6(View v) {onButton(6);}
    public void onButton7(View v) {onButton(7);}
    public void onButton8(View v) {onButton(8);}
    public void onButton9(View v) {onButton(9);}
    public void onButtonEff(View v) {onButton(KEY_ERASE);}
    public void onButtonOk(View v) {onButton(KEY_OK);}
    public static final int REQUEST_CODE = 24;

}


