package peter.mathclock;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.common.collect.ListMultimap;

import org.joda.time.LocalTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class MainActivity extends AppCompatActivity {

    private final DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm:ss");

    private Clock clock;
    private TextView clockText;
    private UpdateTask updateTask;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        MappingParser parser = new MappingParser(this);
        ListMultimap<Integer, Integer> mapping = parser.loadMapping("mapping.properties");

        clock = new Clock(getImage(R.id.clockHours), getImage(R.id.clockMinutes), getImage(R.id.clockSeconds), mapping);
        clockText = (TextView) findViewById(R.id.clockText);
        updateTask = new UpdateTask();
        handler = new Handler();

        updateClock();
        schedule();
    }

    private ImageView getImage(@IdRes int id) {
        return (ImageView) findViewById(id);
    }

    private class UpdateTask implements Runnable {
        @Override
        public void run() {
            updateClock();
            schedule();
        }
    }

    private void schedule() {
        handler.postDelayed(updateTask, 1000);
    }

    private void updateClock() {
        LocalTime now = LocalTime.now();
        clock.show(now);
        clockText.setText(format.print(now));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
//        if (id == R.id.action_settings) {
//            return true;
//        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -1431,52 +1431,8 @@
 r();
-%0A%0A        updateClock();%0A        schedule();
 %0A   
@@ -1586,16 +1586,50 @@
 nable %7B%0A
+%0A        private boolean paused;%0A%0A
         
@@ -1662,24 +1662,55 @@
 oid run() %7B%0A
+            if (!paused) %7B%0A    
             
@@ -1736,16 +1736,20 @@
         
+    
 schedule
@@ -1748,24 +1748,204 @@
 schedule();%0A
+            %7D%0A        %7D%0A%0A        public void pause() %7B%0A            this.paused = true;%0A        %7D%0A%0A        public void start() %7B%0A            this.paused = false;%0A            run();%0A
         %7D%0A  
@@ -2918,11 +2918,431 @@
 );%0A    %7D
+%0A%0A    @Override%0A    protected void onPause() %7B%0A        super.onPause();%0A        updateTask.pause();%0A    %7D%0A%0A    @Override%0A    protected void onResume() %7B%0A        super.onResume();%0A        updateTask.start();%0A    %7D%0A%0A    @Override%0A    protected void onStop() %7B%0A        super.onStop();%0A        updateTask.pause();%0A    %7D%0A%0A    @Override%0A    protected void onStart() %7B%0A        super.onStart();%0A        updateTask.start();%0A    %7D
 %0A%7D%0A

add lifecycle handling, don't update GUI during pause and stop