import React, { Component } from 'react';
import './DepartureInfo.css';

class DepartureInfo extends Component {
	render() {
		var hideStopInfo = ((this.props.stop.name === 'Stop name' && this.props.header.toLowerCase() !== 'nearest') || this.props.header.toLowerCase() === 'stop') ? 'hide' : '';
		var rowClass = (this.props.row % 2) ? 'odd' : 'even';
		rowClass = this.props.header ? 'header' : rowClass;
		return (
			<div className={'departure ' + rowClass}>
				<ul>
					<li className='route type'> {this.props.stoptime.trip.route.mode} </li>
						<div className={'stop ' + hideStopInfo}>
						<li className='stop code'> {this.props.stop.name} </li>
						<li className='stop name'> {this.props.stop.code} </li>
						<li className='stop platform'> {this.props.stop.platformCode} </li>
					</div>
					<li className='route number'> {this.props.stoptime.trip.route.shortName} </li>
					<li className='route destination'> {this.props.stoptime.stopHeadsign} </li>
					<li className='route deptime'> {DepartureInfo.departureTimeToStr(this.props.stoptime.realtimeArrival, this.props.stoptime.realtime)}</li>
				</ul>
			</div>
		);
	}

	static currentTimeInMinutes() {
		var curTime = new Date();
		return curTime.getHours()*60 + curTime.getMinutes();
	}

	static parseHour(seconds) {
		var h = (~~(seconds/3600));
		h = h > 23 ? h - 24 : h;
		return h;
	}

	static parseTime(seconds, delim = ':') {
		var h = this.parseHour(seconds);

		var hStr = h.toString();
		hStr = hStr.length < 2 ? ' ' + hStr : hStr;
		var minStr = (~~((seconds%3600)/60)).toString();
		minStr = minStr.length < 2 ? '0' + minStr : minStr;

		return hStr + delim + minStr;
	}

	static departureTimeToStr(seconds, isRealTime = false) {
		if (seconds === 0) {
			return 'Time';
		}

		var departureInMinutes =  DepartureInfo.parseHour(seconds)*60 + (~~((seconds%3600)/60)) - DepartureInfo.currentTimeInMinutes();

		return (isRealTime ? ' ' : '~') + (((departureInMinutes < 10) && (departureInMinutes >= 0)) ?
			(departureInMinutes + ' min') :
			DepartureInfo.parseTime(seconds));
	}
}

DepartureInfo.defaultProps = {
	stop: {
		name: 'Stop name',
		code: 'Stop code',
		platformCode: 'Platform',
		desc: 'Stop description',
		lat: 47.916667,
		lon: 106.916667
	},
	stoptime: {
		trip: {
			route: {
				shortName: 'Route number',
				mode: 'Route type',
				alerts: []
			}
		},
		realtimeArrival: 0,
		realtimeDeparture: 0,
		realtime: true,
		stopHeadsign: 'Destination'
	},
	header: '',
	row: 0
};

DepartureInfo.propTypes = {
	stop: {
		name: React.PropTypes.string,
		code: React.PropTypes.string,
		platformCode: React.PropTypes.string,
		desc: React.PropTypes.string,
		lat: React.PropTypes.number,
		lon: React.PropTypes.number
	},
	stoptime: {
		trip: {
			route: {
				shortName: React.PropTypes.string,
				mode: React.PropTypes.string,
				alerts: React.PropTypes.array
			}
		},
		realtimeArrival: React.PropTypes.number,
		realtimeDeparture: React.PropTypes.number,
		realtime: React.PropTypes.bool,
		stopHeadsign: React.PropTypes.string
	},
	header: React.PropTypes.string,
	row: React.PropTypes.number
};

export default DepartureInfo;

@@ -547,17 +547,16 @@
 i%3E%0A%09%09%09%09%09
-%09
 %3Cdiv cla
@@ -2503,34 +2503,56 @@
 ypes = %7B%0A%09stop: 
+React.PropTypes.shape(
 %7B%0A
-
 %09%09name: React.Pr
@@ -2734,16 +2734,17 @@
 umber%0A%09%7D
+)
 ,%0A%09stopt
@@ -2748,38 +2748,104 @@
 optime: 
-%7B%0A%09%09trip: %7B%0A%09%09%09route: 
+React.PropTypes.shape(%7B%0A%09%09trip: React.PropTypes.shape(%7B%0A%09%09%09route: React.PropTypes.shape(
 %7B%0A%09%09%09%09sh
@@ -2945,28 +2945,30 @@
 s.array%0A%09%09%09%7D
+)
 %0A%09%09%7D
+)
 ,%0A%09%09realtime
@@ -3118,18 +3118,19 @@
 .string%0A
-
 %09%7D
+)
 ,%0A%09heade

Fix prop validation.