package corrupted.lure.blindwatch;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    Handler h = new Handler();
    int delay = 1000; //15 seconds
    Runnable runnable;
    @Override
    protected void onStart() {
//start handler as activity become visible

        h.postDelayed(new Runnable() {
            public void run() {
                Calendar c = Calendar.getInstance();
                int hours = c.get(Calendar.HOUR_OF_DAY);
                int hour1 = hours / 10;
                int hour2 = hours % 10;

                int minutes = c.get(Calendar.MINUTE);
                int minute1 = minutes/10;
                int minute2 = minutes%10;

                System.out.println(hours + ":" + minutes);
                ImageView hour1img= (ImageView) findViewById(R.id.hour1);
                ImageView hour2img= (ImageView) findViewById(R.id.hour2);
                ImageView minute1img = (ImageView) findViewById(R.id.minute1);
                ImageView minute2img = (ImageView) findViewById(R.id.minute2);


                switch (hour1){
                    case 0: hour1img.setImageResource(R.drawable.i0);
                        break;
                    case 1: hour1img.setImageResource(R.drawable.i1);
                        break;
                    case 2: hour1img.setImageResource(R.drawable.i2);
                        break;
                    case 3: hour1img.setImageResource(R.drawable.i3);
                        break;
                    case 4: hour1img.setImageResource(R.drawable.i4);
                        break;
                    case 5: hour1img.setImageResource(R.drawable.i5);
                        break;
                    case 6: hour1img.setImageResource(R.drawable.i6);
                        break;
                    case 7: hour1img.setImageResource(R.drawable.i7);
                        break;
                    case 8: hour1img.setImageResource(R.drawable.i8);
                        break;
                    case 9: hour1img.setImageResource(R.drawable.i9);
                        break;
                    default: hour1img.setImageResource(R.drawable.i0);
                }

                switch (hour2){
                    case 0: hour2img.setImageResource(R.drawable.i0);
                        break;
                    case 1: hour2img.setImageResource(R.drawable.i1);
                        break;
                    case 2: hour2img.setImageResource(R.drawable.i2);
                        break;
                    case 3: hour2img.setImageResource(R.drawable.i3);
                        break;
                    case 4: hour2img.setImageResource(R.drawable.i4);
                        break;
                    case 5: hour2img.setImageResource(R.drawable.i5);
                        break;
                    case 6: hour2img.setImageResource(R.drawable.i6);
                        break;
                    case 7: hour2img.setImageResource(R.drawable.i7);
                        break;
                    case 8: hour2img.setImageResource(R.drawable.i8);
                        break;
                    case 9: hour2img.setImageResource(R.drawable.i9);
                        break;
                    default: hour2img.setImageResource(R.drawable.i0);
                }

                switch (minute1){
                    case 0: minute1img.setImageResource(R.drawable.i0);
                        break;
                    case 1: minute1img.setImageResource(R.drawable.i1);
                        break;
                    case 2: minute1img.setImageResource(R.drawable.i2);
                        break;
                    case 3: minute1img.setImageResource(R.drawable.i3);
                        break;
                    case 4: minute1img.setImageResource(R.drawable.i4);
                        break;
                    case 5: minute1img.setImageResource(R.drawable.i5);
                        break;
                    case 6: minute1img.setImageResource(R.drawable.i6);
                        break;
                    case 7: minute1img.setImageResource(R.drawable.i7);
                        break;
                    case 8: minute1img.setImageResource(R.drawable.i8);
                        break;
                    case 9: minute1img.setImageResource(R.drawable.i9);
                        break;
                    default: minute1img.setImageResource(R.drawable.i0);
                }

                switch (minute2){
                    case 0: minute2img.setImageResource(R.drawable.i0);
                        break;
                    case 1: minute2img.setImageResource(R.drawable.i1);
                        break;
                    case 2: minute2img.setImageResource(R.drawable.i2);
                        break;
                    case 3: minute2img.setImageResource(R.drawable.i3);
                        break;
                    case 4: minute2img.setImageResource(R.drawable.i4);
                        break;
                    case 5: minute2img.setImageResource(R.drawable.i5);
                        break;
                    case 6: minute2img.setImageResource(R.drawable.i6);
                        break;
                    case 7: minute2img.setImageResource(R.drawable.i7);
                        break;
                    case 8: minute2img.setImageResource(R.drawable.i8);
                        break;
                    case 9: minute2img.setImageResource(R.drawable.i9);
                        break;
                    default: minute2img.setImageResource(R.drawable.i0);
                }

                runnable=this;
                h.postDelayed(runnable, delay);
            }
        }, delay);

        super.onStart();
    }

    @Override
    protected void onPause() {
        h.removeCallbacks(runnable); //stop handler when activity not visible
        super.onPause();
    }
}
