const React = require('react')
const e = React.createElement
const Component = React.Component
const bindActionCreators = require('redux').bindActionCreators
const connect = require('react-redux').connect
const nodegit = require('../utils/nodegit').nodegit
const LoadingActions = require('../actions/loading')

class Contributors extends Component {

	constructor(props) {
		super(props)

		this.state = {
			contributors: [],
		}
	}

	componentDidMount() {
		this.refresh()
	}

	refresh() {
		const contributors = []
		const emails = []
		this.props.actions.loading.IncrementLoadingJobs()

		const processCommit = (commit) => {
			const author = commit.author()
			if (emails.indexOf(author.email()) === -1) {
				emails.push(author.email())
				contributors.push({
					email: author.email(),
					name: author.name(),
				})
			}

			if (commit.parentcount() === 0) {
				return
			} else {
				return commit.getParents() // @TODO: tenhle postup zbytečně nekolikrát navštěvuje některé commity
					.then((parents) => {
						return Promise.all(parents.map((parent) => {
							return processCommit(parent)
						}))
					})
			}
		}

		nodegit.Repository.open(this.props.project.path)
			.then((repo) => repo.getHeadCommit())
			.then((commit) => {
				return processCommit(commit)
			})
			.then(() => {
				this.setState(Object.assign({}, this.state, {
					contributors,
				}))
			})
			.catch((error) => {
				console.error(error)
			})
			.then(() => {
				this.props.actions.loading.DecrementLoadingJobs()
			})
	}

	renderList() {
		return this.state.contributors.map((contributor, i) => {
			return e(
				'div',
				{
					key: contributor.email,
				},
				`${contributor.name} <${contributor.email}>`
			)
		})
	}

	render() {

		return (
			e(
				'div',
				null,
				e('h2', null, 'Autoři'),
				this.renderList()
			)
		)
	}
}


function mapStateToProps(state) {
	return {
		loading: state.loading,
	}
}

function mapDispatchToProps(dispatch) {
	return {
		actions: {
			loading: bindActionCreators(LoadingActions, dispatch),
		}
	}
}

module.exports = connect(mapStateToProps, mapDispatchToProps)(Contributors)

@@ -1241,24 +1241,43 @@
 ommit) =%3E %7B%0A
+%09%09%09%09if (commit) %7B%0A%09
 %09%09%09%09return p
@@ -1296,27 +1296,24 @@
 (commit)%0A%09%09%09
-%7D)%0A
 %09%09%09.then(() 
@@ -1317,24 +1317,27 @@
 () =%3E %7B%0A%09%09%09%09
+%09
+%09%09
 this.setStat
@@ -1371,24 +1371,27 @@
 ate, %7B%0A%09%09%09%09%09
+%09%09%09
 contributors
@@ -1392,27 +1392,45 @@
 butors,%0A%09%09%09%09
-%7D))
+%09%09%09%7D))%0A%09%09%09%09%09%09%7D)%0A%09%09%09%09%7D
 %0A%09%09%09%7D)%0A%09%09%09.c

handle no master