private void updateSplits(long cumulativeTime, int cummulativeDistance, boolean reset) {
    MyLog.i("RaceTimerFragment", "updateSplits for Athlete " + mAthleteNumber);
    List<Split> splits = new ArrayList<>();

    if (reset) {
      tvAthletePace.setText(getActivity().getString(R.string.header_pace));
    } else {
      // gets the splits associated with the race Uuid
      splits = Split.getAllSplits(mRaceUuid, false);

      String paceAndDistance =
          CommonMethods.getAveragePaceString(
              cumulativeTime, cummulativeDistance, mPaceDistance, mPoolLengthUnits);

      if (mIsClockRunning) {
        paceAndDistance =
            paceAndDistance
                + CommonMethods.getEstimatedTotalDistance(
                    cumulativeTime, cummulativeDistance, mEventDurationMs, mPoolLengthUnits);
      }

      tvAthletePace.setText(paceAndDistance);
    }

    mSplitArrayAdapter.setData(splits);
    mSplitArrayAdapter.notifyDataSetChanged();
  }
 @Override
 public void onSaveInstanceState(Bundle outState) {
   MyLog.i("RaceTimerFragment", "onSaveInstanceState");
   super.onSaveInstanceState(outState);
   outState.putInt(ARG_ATHLETE_NUMBER, mAthleteNumber);
   outState.putString(ARG_RACE_UUID, mRaceUuid);
 }
 public static RaceTimerFragment newInstance(int athleteNumber) {
   MyLog.i("RaceTimerFragment", "newInstance: Athlete" + athleteNumber);
   RaceTimerFragment fragment = new RaceTimerFragment();
   Bundle args = new Bundle();
   args.putInt(ARG_ATHLETE_NUMBER, athleteNumber);
   fragment.setArguments(args);
   return fragment;
 }
 private void resetLapCounter() {
   MyLog.i("RaceTimerFragment", "resetLapCounter");
   enableAthlete();
   showStartButton();
   Split.deleteSplitsNotRaceComplete(mSelectedSwimEvent);
   resetSplits();
   tvAthletePace.setText(getActivity().getString(R.string.header_pace));
   mIsClockRunning = false;
 }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
      mAthleteNumber = getArguments().getInt(ARG_ATHLETE_NUMBER);
    }

    switch (mAthleteNumber) {
      case 1:
        mRaceUuid = MySettings.getAthlete1RaceUuid();
        MyLog.i("RaceTimerFragment", "onCreate: Athlete1 RaceUuid: " + mRaceUuid);
        break;
      case 2:
        mRaceUuid = MySettings.getAthlete2RaceUuid();
        MyLog.i("RaceTimerFragment", "onCreate: Athlete2 RaceUuid: " + mRaceUuid);
        break;
    }

    EventBus.getDefault().register(this);
    mVibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
  }
  private void updateAthletesSpinner() {
    MyLog.i("RaceTimerFragment", "updateAthletesSpinner for Athlete " + mAthleteNumber);
    String excludedAthleteID = MySettings.NOT_AVAILABLE;
    switch (mAthleteNumber) {
      case 1:
        excludedAthleteID = MySettings.getSelectedAthlete2ID();
        break;

      case 2:
        excludedAthleteID = MySettings.getSelectedAthlete1ID();
        break;
    }
    List<Athlete> athletesList = Athlete.getAllAthletes(excludedAthleteID);
    mAthleteSpinnerAdapter.setData(athletesList);
    mAthleteSpinnerAdapter.notifyDataSetChanged();
  }
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.btnEditAthlete:
        showEditAthleteDialog();
        break;

      case R.id.btnStart:
        // truncate time to tenths of seconds
        mStartTime = (System.currentTimeMillis() / 100) * 100;
        mIsClockRunning = true;
        new buttonVibration().execute();
        EventBus.getDefault().post(new MyEvents.startClock(mStartTime));
        EventBus.getDefault().post(new MyEvents.startRace(mAthleteNumber));
        startRace();
        break;

      case R.id.btnSplit:
        if (mIsClockRunning) {
          MyLog.i("RaceTimerFragment", "btnSplitClick Athlete" + mAthleteNumber);
          // truncate time to tenths of seconds
          long currentTime = (System.currentTimeMillis() / 100) * 100;
          new buttonVibration().execute();
          long mCumulativeTime = currentTime - mStartTime;
          long splitTime = mCumulativeTime - mPreviousCummulativeTime;
          mLapNumber++;
          int mCummulativeDistance = mLapNumber * mLapDistance;
          createSplit(
              mSelectedAthlete,
              mSelectedSwimEvent,
              mLapNumber,
              mCummulativeDistance,
              splitTime,
              mCumulativeTime);
          temporarilyDisableSplitButton();
        }
        break;

      case R.id.btnEmail:
        CommonMethods.sendRaceSummaryEmail(getActivity(), mSwimEventSummary);
        break;
    }
  }
 private void startRace() {
   MyLog.i("RaceTimerFragment", "startRace for Athlete " + mAthleteNumber);
   if (mSplitArrayAdapter.getCount() > 0) {
     resetSplits();
   }
   disableAthlete();
   showSplitButton();
   mSelectedSwimEvent = MainActivity.getSelectedSwimEvent();
   mLapNumber = 0;
   mPreviousCummulativeTime = 0;
   mIsClockRunning = true;
   mRaceUuid = UUID.randomUUID().toString();
   switch (mAthleteNumber) {
     case 1:
       MySettings.setAthlete1RaceUuid(mRaceUuid);
       break;
     case 2:
       MySettings.setAthlete2RaceUuid(mRaceUuid);
       break;
   }
 }
  private void createSplit(
      Athlete athlete,
      SwimEvent swimEvent,
      int lapNumber,
      int cummulativeDistance,
      long splitTime,
      long cumulativeTime) {
    try {
      Split newSplit = new Split();
      newSplit.newInstance(
          mRaceUuid, athlete, swimEvent, lapNumber, cummulativeDistance, splitTime, cumulativeTime);
      newSplit.pin();
      updateSplits(cumulativeTime, cummulativeDistance, false);
      mPreviousCummulativeTime = cumulativeTime;

    } catch (ParseException e) {
      MyLog.e(
          "RaceTimerFragment",
          "createSplit: ParseException: for Athlete " + mAthleteNumber + ": " + e.getMessage());
    }
  }
  private void completeRace(String lastLapDistanceString, String timerName, String eventLocation) {
    int lastLapDistance = Integer.valueOf(lastLapDistanceString);

    // create last split
    int totalDistance = (mLapNumber * mLapDistance) + lastLapDistance;
    String averagePaceString =
        CommonMethods.getAveragePaceString(
            mEventDurationMs, totalDistance, mPaceDistance, mPoolLengthUnits);
    long splitTime = mEventDurationMs - mPreviousCummulativeTime;

    createSplit(
        mSelectedAthlete,
        mSelectedSwimEvent,
        mLapNumber + 1,
        totalDistance,
        splitTime,
        mEventDurationMs);

    // create event summary
    mSwimEventSummary = new SwimEventSummary();
    mSwimEventSummary.newInstance(
        mRaceUuid,
        mSelectedSwimEvent,
        mSelectedAthlete,
        mStartTime,
        timerName,
        eventLocation,
        mPoolLength,
        mPoolLengthUnits,
        totalDistance,
        averagePaceString);
    mSwimEventSummary.pinInBackground();

    // Mark splits as Race Complete
    markSplitsRaceComplete();
    MyLog.i("RaceTimerFragment", "completeRace: for Athlete " + mAthleteNumber);
  }
 // region OnEvents
 public void onEvent(MyEvents.updateUI event) {
   MyLog.i("RaceTimerFragment", "updateUI for Athlete " + mAthleteNumber);
   updateAthletesSpinner();
 }
 private void stopRace() {
   MyLog.i("RaceTimerFragment", "stopRace for Athlete " + mAthleteNumber);
   mIsClockRunning = false;
   showEmailButton();
 }
 @Override
 public void onDestroy() {
   super.onDestroy();
   MyLog.i("RaceTimerFragment", "onDestroy: Athlete" + mAthleteNumber);
   EventBus.getDefault().unregister(this);
 }
 public void onEvent(MyEvents.resetLapCounter event) {
   MyLog.i("RaceTimerFragment", "resetLapCounter for Athlete " + mAthleteNumber);
   resetLapCounter();
 }
 @Override
 public void onPause() {
   super.onPause();
   MyLog.i("RaceTimerFragment", "onPause: Athlete" + mAthleteNumber);
 }
 @Override
 public void onViewStateRestored(Bundle savedInstanceState) {
   super.onViewStateRestored(savedInstanceState);
   MyLog.i("RaceTimerFragment", "onViewStateRestored");
 }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (savedInstanceState != null) {
      MyLog.i("RaceTimerFragment", "onActivityCreated: savedInstanceState Not Null");
      if (savedInstanceState.containsKey(ARG_ATHLETE_NUMBER)) {
        mAthleteNumber = savedInstanceState.getInt(ARG_ATHLETE_NUMBER);
      }
      if (savedInstanceState.containsKey(ARG_RACE_UUID)) {
        mRaceUuid = savedInstanceState.getString(ARG_RACE_UUID);
      }
    } else {
      switch (mAthleteNumber) {
        case 1:
          mRaceUuid = MySettings.getAthlete1RaceUuid();
          break;
        case 2:
          mRaceUuid = MySettings.getAthlete2RaceUuid();
          break;
      }
    }

    MyLog.i(
        "RaceTimerFragment",
        "onActivityCreated: Athlete" + mAthleteNumber + "RaceUuid: " + mRaceUuid);
    if (mAthleteNumber == 1) {
      mLapDistance = MySettings.getLapDistance();
      mPaceDistance = MySettings.getPaceDistance();
      mPoolLength = MySettings.getPoolLength();
      mPoolLengthUnits = MySettings.getPoolLengthUnits();
      mEventDurationMs = MySettings.getEventDurationMinutes() * 60000;
      mSplitButtonDisableDurationMs = MySettings.getSplitButtonDisabledSeconds() * 1000;
    }

    int spnAthletesStartingPosition = 0;
    switch (mAthleteNumber) {
      case 1:
        if (!MySettings.getSelectedAthlete1ID().equals(MySettings.NOT_AVAILABLE)) {
          spnAthletesStartingPosition =
              mAthleteSpinnerAdapter.getItemPosition(MySettings.getSelectedAthlete1ID());
        }
        break;

      case 2:
        if (!MySettings.getSelectedAthlete2ID().equals(MySettings.NOT_AVAILABLE)) {
          spnAthletesStartingPosition =
              mAthleteSpinnerAdapter.getItemPosition(MySettings.getSelectedAthlete2ID());
        }
        break;
    }
    mOkToResetLapCounter = false;
    spnAthletes.setSelection(spnAthletesStartingPosition);
    mIsClockRunning = false;

    String pace = getActivity().getString(R.string.header_pace);
    if (mSplitsOfCompletedRace.size() > 0) {
      Split lastSplit = mSplitsOfCompletedRace.get(0);
      long cummulativeDurationMs = lastSplit.getAccumulativeTimeMs();
      int cummulativeDistance = lastSplit.getDistance();
      pace =
          CommonMethods.getAveragePaceString(
              cummulativeDurationMs, cummulativeDistance, mPaceDistance, mPoolLengthUnits);
    }
    tvAthletePace.setText(pace);
  }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    MyLog.i("RaceTimerFragment", "onCreateView: Athlete" + mAthleteNumber);
    View view = null;

    if (savedInstanceState != null) {
      if (savedInstanceState.containsKey(ARG_ATHLETE_NUMBER)) {
        mAthleteNumber = savedInstanceState.getInt(ARG_ATHLETE_NUMBER);
      }
      if (savedInstanceState.containsKey(ARG_RACE_UUID)) {
        mRaceUuid = savedInstanceState.getString(ARG_RACE_UUID);
      }
    }

    switch (mAthleteNumber) {
      case 1:
        view = inflater.inflate(R.layout.frag_race_timer_athlete1, container, false);
        break;

      case 2:
        view = inflater.inflate(R.layout.frag_race_timer_athlete2, container, false);
        break;
    }

    if (view != null) {
      llAthlete = (LinearLayout) view.findViewById(R.id.llAthletes);
      spnAthletes = (Spinner) view.findViewById(R.id.spnAthletes);
      mAthleteSpinnerAdapter =
          new AthleteSpinnerAdapter(getActivity(), new ArrayList<Athlete>(), mAthleteNumber);
      spnAthletes.setAdapter(mAthleteSpinnerAdapter);
      updateAthletesSpinner();
      spnAthletes.setOnItemSelectedListener(
          new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              mSelectedAthlete = mAthleteSpinnerAdapter.getItem(position);
              switch (mAthleteNumber) {
                case 1:
                  MySettings.setSelectedAthlete1ID(mSelectedAthlete.getAthleteID());
                  break;
                case 2:
                  MySettings.setSelectedAthlete2ID(mSelectedAthlete.getAthleteID());
                  break;
              }
              EventBus.getDefault().post(new MyEvents.updateAthletesSpinner(mAthleteNumber));
              if (mOkToResetLapCounter) {
                resetLapCounter();
              } else {
                mOkToResetLapCounter = true;
              }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
          });

      Button btnEditAthlete = (Button) view.findViewById(R.id.btnEditAthlete);

      tvAthletePace = (TextView) view.findViewById(R.id.tvAthletePace);
      ListView lvAthleteRaceSplits = (ListView) view.findViewById(R.id.lvAthleteRaceSplits);

      mSplitsOfCompletedRace = Split.getAllSplits(mRaceUuid, false);
      mSplitArrayAdapter =
          new SplitArrayAdapter(getActivity(), mSplitsOfCompletedRace, mAthleteNumber);
      lvAthleteRaceSplits.setAdapter(mSplitArrayAdapter);

      btnStart = (Button) view.findViewById(R.id.btnStart);
      btnSplit = (Button) view.findViewById(R.id.btnSplit);
      btnEmail = (Button) view.findViewById(R.id.btnEmail);

      btnEditAthlete.setOnClickListener(this);
      btnStart.setOnClickListener(this);
      btnSplit.setOnClickListener(this);
      btnEmail.setOnClickListener(this);
    }
    return view;
  }