package com.andela.iworkout.fragments;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.andela.iworkout.R;
import com.andela.iworkout.activities.MyApplication;
import com.andela.iworkout.activities.WorkoutActivity;
import com.andela.iworkout.model.Day;
import com.andela.iworkout.repository.WorkoutManager;
import com.andela.iworkout.utilities.DateFormatter;
import com.andela.iworkout.utilities.MsgBox;
import com.andela.iworkout.utilities.OnTimerTickListener;
import com.andela.iworkout.utilities.PushUpListener;
import com.andela.iworkout.utilities.PushUpManager;
import com.andela.iworkout.utilities.Settings;
import com.andela.iworkout.utilities.TimeCounter;

public class WorkoutActivityFragment extends Fragment {
    private static final int SECONDS = 1000;
    private static final int MINUTES = 60000;

    private TimeCounter timeCounter;
    private PushUpManager pushUpManager;
    private int counter = 0;
    private int pushUpGoal = 0;
    private Day day;

    public WorkoutActivityFragment() {
        timeCounter = new TimeCounter();
        day = new Day();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_workout, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setTimeKeeper(view);
    }

    private void setPushUpManager() {
        View touchScreen = getActivity().findViewById(R.id.touchScreen);
        final TextView pushUpKeeper = (TextView) getActivity().findViewById(R.id.pushUpKeeper);

        pushUpManager = new PushUpManager(getContext(), touchScreen);

        pushUpGoal = 50;
        //pushUpGoal = Settings.getPushUps(getContext());
        if (pushUpGoal <= 0) { //PUSHUP COUNT UP TILL TIME ELAPSE
            pushUpManager.setListener(new PushUpListener() {
                @Override
                public void pushUp() {
                    pushUpKeeper.setText(String.valueOf(counter++));
                }

                @Override
                public void pushDown() {
                }
            });
        } else {
            pushUpKeeper.setText(String.valueOf(pushUpGoal));
            pushUpManager.setListener(new PushUpListener() {
                @Override
                public void pushUp() {
                    counter++;
                    pushUpKeeper.setText(String.valueOf(pushUpGoal--));

                    if (pushUpGoal < 0) {
                        endPushupSession();
                    }
                }

                @Override
                public void pushDown() {
                }
            });
        }

    }

    private void setTimeKeeper(View view) {
        final TextView timeKeeper = (TextView) view.findViewById(R.id.timeKeeper);

        long timeTill = Settings.getTime(getContext());
        //long timeTill = 100000;
        if (timeTill <= 0) { //TIME COUNT UP TO MEET PUSHUP GOAL
            timeCounter.countUp(SECONDS, new OnTimerTickListener() {
                @Override
                public void onTick(long millisecond) {
                    timeKeeper.setText(getTime(millisecond));
                }

                @Override
                public void onFinish() {
                }
            });
        } else {
            timeKeeper.setText(getTime(timeTill));
            timeCounter.countDown(timeTill, SECONDS, new OnTimerTickListener() {
                @Override
                public void onTick(long millisecond) {
                    timeKeeper.setText(getTime(millisecond));
                }

                @Override
                public void onFinish() {
                    timeKeeper.setText(String.valueOf(0));
                    endPushupSession();
                }
            });
        }
    }

    public void startWorkout() {
        counter = 0;
        timeCounter.start();
        setPushUpManager();
    }

    public void stopWorkout() {
        cancelWorkout();

        day.setThedate(DateFormatter.getReadableDate(System.currentTimeMillis()));
        day.setPushups(counter - 1);
        getWorkoutManager().savePushUps(day);
    }

    public void cancelWorkout() {
        timeCounter.stop();
        if (pushUpManager != null) {
            pushUpManager.disconnectPushUp();
        }
    }

    private void endPushupSession() {
        //PUSHUP PREVIEW.
        //PLAY SOUND

        WorkoutActivity workoutActivity = (WorkoutActivity) getActivity();
        workoutActivity.pressButton();
    }

    private String getTime(long milliSeconds) {
        if (milliSeconds >= MINUTES) {
            return DateFormatter.getReadableTime(milliSeconds);
        }

        return DateFormatter.getReadableSeconds(milliSeconds);
    }

    private WorkoutManager getWorkoutManager() {
        return MyApplication.getWorkoutManager();
    }

}

@@ -99,16 +99,59 @@
 Bundle;%0A
+import android.support.v7.app.AlertDialog;%0A
 import a
@@ -174,24 +174,24 @@
 utInflater;%0A
-
 import andro
@@ -1078,24 +1078,61 @@
 hUpManager;%0A
+    private AlertDialog alertDialog;%0A
     private 
@@ -1176,24 +1176,55 @@
 UpGoal = 0;%0A
+    private int timeTaken = 0;%0A
     private 
@@ -3476,32 +3476,65 @@
  millisecond) %7B%0A
+                    timeTaken++;%0A
                 
@@ -3921,32 +3921,65 @@
  millisecond) %7B%0A
+                    timeTaken++;%0A
                 
@@ -4310,24 +4310,47 @@
 ounter = 0;%0A
+        timeTaken = 0;%0A
         time
@@ -4455,24 +4455,493 @@
 Workout();%0A%0A
+        savePushUp();%0A%0A        displayDialog();%0A%0A        //PLAY SOUND%0A    %7D%0A%0A    private void displayDialog() %7B%0A        alertDialog = MsgBox.show(getContext(), getString(R.string.congratulations), new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                alertDialog.dismiss();%0A                getActivity().finish();%0A            %7D%0A        %7D, false);%0A        alertDialog.show();%0A    %7D%0A%0A    private void savePushUp() %7B%0A
         day.
@@ -5048,16 +5048,53 @@
 r - 1);%0A
+        day.setTimeTaken(timeTaken);%0A
         
@@ -5342,56 +5342,8 @@
 ) %7B%0A
-        //PUSHUP PREVIEW.%0A        //PLAY SOUND%0A%0A
     
@@ -5790,17 +5790,16 @@
 ager();%0A
+
     %7D%0A
-%0A
 %7D%0A

Refactor WorkoutActivityFragment class