 R = require 'ramda'
 
+###
 data =
 	motions:
 		guggis_kamu:

		vote_value: est
	return ret

estimated_votes = ->
	est = []
	for vote_event_id, _ of data.vote_events
		for voter_id, _ of data.persons
			est.push estimate_vote voter_id, vote_event_id
	return est

my_agreements = (me) ->
	ests = estimated_votes()
	my_votes = getWhere data.votes, voter_id: me
	for person_id, person of data.persons
		paggs = []
		for _, my_vote of my_votes
			other_vote = R.values getWhere ests,
				voter_id: person_id
				vote_event_id: my_vote.vote_event_id
			other_vote = other_vote[0]
			continue unless other_vote?
			my_value = my_vote.vote_value
			other_value = other_vote.vote_value

			voter_id: person_id
			agreement: R.mean paggs
		yield res

export get_hack_data = ->
	popoloize_hel_council_votes data, require '../importer/cases.json'

export set_user_vote = (data, vote_event_id, vote_value) ->
	# Horrible! Shouldn't depend on the key format!
	vote_id = "#{vote_event_id}/CURRENT_USER"
	data.votes[vote_id] =
		voter_id: "CURRENT_USER"
		vote_event_id: vote_event_id
		vote_value: vote_value
	return data

export get_user_agreements = (data) ->
	Array.from voter_agreements data, "CURRENT_USER"

export get_user_party_agreements = (data) ->
	agreements = Array.from get_user_agreements data
	voter_party = (v) -> data.persons[v.voter_id].party
	party_agreements = R.groupBy voter_party, (R.sortBy voter_party, agreements)
	
	party_agreements = for party, agrs of party_agreements
		v = R.map (R.prop 'agreement'), agrs
		v = R.filter ((v) -> v == v), v
		
		party: party
		agreement: R.mean v
	
	[good, bad] = R.partition ((v) -> v.agreement == v.agreement), party_agreements
	R.concat (R.sortBy ((v) -> -v.agreement), good), bad


if require.main == module
	#console.log Array.from my_agreements('user1')
	data = popoloize_hel_council_votes(data, require './cases.json')
	data = set_user_vote(data, 'guggis_kamu', -1.0)
	console.log get_user_party_agreements data
