import RpmMeter, { PULSE_EVENT } from 'spin-bike-rpm-meter';
import { startTripClock } from '../utils/action-helpers';

export const START_TRIP = 'START_TRIP';
export const STOP_TRIP = 'STOP_TRIP';
export const GET_TRIP_DATA = 'GET_TRIP_DATA';
export const TICK_TRIP_CLOCK = 'TICK_TRIP_CLOCK';
export const STOP_MOVING = 'STOP_MOVING';

export function startTrip() {
	return function(dispatch, getState) {
		const rpmMeter = new RpmMeter();

		return rpmMeter.start()
			.then(stopFunction => {
				const tripClock = startTripClock(() => {
					dispatch(tickTripClockAction());
				});

				dispatch(startTripAction(rpmMeter, stopFunction, tripClock));

				rpmMeter.on(PULSE_EVENT, pulseData => {
					const state = getState();

					let { movingThresholdTimeout } = state.tripDashboard;

					clearTimeout(movingThresholdTimeout);

					movingThresholdTimeout = setTimeout(() => {
						dispatch(stopMovingAction());

					}, 2000);

					dispatch(getTripDataAction(pulseData, movingThresholdTimeout));
				});
			});
	}
}

export function stopTrip() {
	return function(dispatch, getState) {
		const state = getState();

		const { stopFunction, tripClock } = state.rpmMeter;

		stopFunction();
		tripClock.stop();
		dispatch(stopTripAction());
	}
}

function stopMovingAction() {
	return {
		type: STOP_MOVING,
		payload: {
			isMoving: false
		}
	}
}

function tickTripClockAction() {
	return {
		type: TICK_TRIP_CLOCK
	}
}

function getTripDataAction(pulseData, movingThresholdTimeout) {
	return {
		type: GET_TRIP_DATA,
		payload: {
			pulseData,
			movingThresholdTimeout
		}
	};
}

function startTripAction(rpmMeter, stopFunction, tripClock) {
	return {
		type: START_TRIP,
		payload: {
			rpmMeter,
			stopFunction,
			tripClock
		}
	};
}

function stopTripAction() {
	return {
		type: STOP_TRIP
	}
}
@@ -492,52 +492,182 @@
 %3E %7B%0A
-%09%09%09%09const tripClock = startTripClock(() =%3E %7B
+%0A%09%09%09%09// Start trip clock for calculating total elapsed time.%0A%09%09%09%09const tripClock = startTripClock(() =%3E %7B%0A%0A%09%09%09%09%09// 1. Dispatch tick action each second (clock interval is 1s).
 %0A%09%09%09
@@ -706,24 +706,73 @@
 );%0A%09%09%09%09%7D);%0A%0A
+%09%09%09%09// 2. Dispatch action that trip was started.%0A
 %09%09%09%09dispatch
@@ -822,24 +822,97 @@
 ipClock));%0A%0A
+%09%09%09%09// Listen to RPMMeter %22pulses%22 (revolutions of pedals on spin bike).%0A
 %09%09%09%09rpmMeter
@@ -1123,24 +1123,161 @@
 eout(() =%3E %7B
+%0A%0A%09%09%09%09%09%09// 3. Dispatch a stop moving action if cyclist hasn't made a revolution of pedals on his/her%0A%09%09%09%09%09%09// bike in the last 2 seconds.
 %0A%09%09%09%09%09%09dispa
@@ -1297,25 +1297,24 @@
 gAction());%0A
-%0A
 %09%09%09%09%09%7D, 2000
@@ -1313,24 +1313,187 @@
 %09%7D, 2000);%0A%0A
+%09%09%09%09%09// 4. Dispatch action that a full revolution of pedals on stationary bike was made.%0A%09%09%09%09%09// Include secondsBetweenPulses (= revolutions) in pulseData object.%0A
 %09%09%09%09%09dispatc

Add short documentation for the startTrip fn