package dk.kleistsvendsen;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.inject.Inject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import roboguice.activity.RoboActivity;

public class HomeActivity extends RoboActivity {
    public static final int MilliSecsPerMinute = 60 * 1000;
    public static final int MilliSecsPerHalf = 20 * MilliSecsPerMinute;
    @Inject
    private IGameTimer gameTimer_;

    private TextView timeLeftText_;
    private TextView timePlayedText_;

    private final Handler updateHandler_ = new Handler();
    private final Runnable updateViewRunner_ = new Runnable() {
        @Override
        public void run() {
            updateView();
            updateHandler_.postDelayed(this, 50);
        }
    };
    final Calendar calendar_ = Calendar.getInstance();
    private final SimpleDateFormat dateFormatter_ = new SimpleDateFormat("m:ss.SSS");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_layout);
        connectButtons_();
        updateHandler_.post(updateViewRunner_);
    }

    private void connectButtons_() {
        Button startButton = (Button) findViewById(R.id.start_button);
        timeLeftText_ = (TextView) findViewById(R.id.timeLeftText);
        timePlayedText_ = (TextView) findViewById(R.id.timePlayedText);
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onStartClick_();
            }
        });

        Button pauseButton = (Button) findViewById(R.id.pause_button);
        pauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onPauseClick_();
            }
        });

        Button resetButton = (Button) findViewById(R.id.reset_button);
        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showConfirmResetDialog_();
            }
        });
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int action = event.getAction();
        int keyCode = event.getKeyCode();
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                if (action == KeyEvent.ACTION_DOWN) {
                    onStartClick_();
                }
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                if (action == KeyEvent.ACTION_DOWN) {
                    onPauseClick_();
                }
                return true;
            default:
                return super.dispatchKeyEvent(event);
        }
    }

    private void onStartClick_() {
        gameTimer_.startTimer();
        long[] pattern = {20,10,20,10,20};
        vibrate_(pattern);
    }

    private void onPauseClick_() {
        gameTimer_.pauseTimer();
        long[] pattern = {200,200,200,200,200};
        vibrate_(pattern);
    }

    private void vibrate_(long[] pattern) {
        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(pattern, -1);
    }

    public void showConfirmResetDialog_() {
        new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle(R.string.reset_timer_question_title)
                .setMessage(R.string.reset_timer_question)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        gameTimer_.resetTimer();
                    }

                })
                .setNegativeButton(R.string.cancel, null)
                .show();
    }

    public void updateView() {
        Long played = gameTimer_.timePlayed();
        assert(played<= MilliSecsPerHalf);
        Long left = MilliSecsPerHalf - played;
        timeLeftText_.setText(formatTime_(left));
        timePlayedText_.setText(formatTime_(played));
    }

    private String formatTime_(Long left) {
        calendar_.setTimeInMillis(left);
        return dateFormatter_.format(calendar_.getTime());
    }
}

@@ -3162,21 +3162,32 @@
  = %7B
-20,10,20,10,2
+0,50,50,50,50,50,50,50,5
 0%7D;%0A
@@ -3314,16 +3314,18 @@
 tern = %7B
+0,
 200,200,

Improve vibrate patterns