/*  OpenTally: Open-source election vote counting
 *  Copyright © 2021 Lee Yingtong Li (RunasSudo)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use opentally::election::{CandidateState, CountState, Election};
use opentally::numbers::Number;
use opentally::stv;

use csv::StringRecord;

use std::fs::File;
use std::io::{self, BufRead}; 
use std::ops;

#[allow(dead_code)] // Suppress false positive
pub fn read_validate_election<N: Number>(csv_file: &str, blt_file: &str, stv_opts: stv::STVOptions, cmp_dps: Option<usize>, sum_rows: &[&str])
where
	for<'r> &'r N: ops::Add<&'r N, Output=N>,
	for<'r> &'r N: ops::Sub<&'r N, Output=N>,
	for<'r> &'r N: ops::Mul<&'r N, Output=N>,
	for<'r> &'r N: ops::Div<&'r N, Output=N>,
	for<'r> &'r N: ops::Neg<Output=N>,
{
	// Read CSV file
	let reader = csv::ReaderBuilder::new()
		.has_headers(false)
		.from_path(csv_file)
		.expect("IO Error");
	let records: Vec<StringRecord> = reader.into_records().map(|r| r.expect("Syntax Error")).collect();
	
	let mut candidates: Vec<&str> = records.iter().skip(2).map(|r| &r[0]).collect();
	// Remove exhausted/LBF rows
	candidates.truncate(candidates.len() - sum_rows.len());
	
	let stages: Vec<usize> = records.first().unwrap().iter().skip(1).step_by(2).map(|s| s.parse().unwrap()).collect();
	
	// Read BLT
	let file = File::open(blt_file).expect("IO Error");
	let file_reader = io::BufReader::new(file);
	let lines = file_reader.lines();
	
	let election: Election<N> = Election::from_blt(lines.map(|r| r.expect("IO Error").to_string()).into_iter());
	
	// Validate candidate names
	for (i, candidate) in candidates.iter().enumerate() {
		assert_eq!(election.candidates[i].name, *candidate);
	}
	
	validate_election(stages, records, election, stv_opts, cmp_dps, sum_rows);
}

pub fn validate_election<N: Number>(stages: Vec<usize>, records: Vec<StringRecord>, mut election: Election<N>, stv_opts: stv::STVOptions, cmp_dps: Option<usize>, sum_rows: &[&str])
where
	for<'r> &'r N: ops::Add<&'r N, Output=N>,
	for<'r> &'r N: ops::Sub<&'r N, Output=N>,
	for<'r> &'r N: ops::Mul<&'r N, Output=N>,
	for<'r> &'r N: ops::Div<&'r N, Output=N>,
	for<'r> &'r N: ops::Neg<Output=N>,
{
	if stv_opts.normalise_ballots {
		election.normalise_ballots();
	}
	
	// Initialise count state
	let mut state = CountState::new(&election);
	
	// Distribute first preferences
	stv::count_init(&mut state, &stv_opts).unwrap();
	let mut stage_num = 1;
	
	for (idx, stage) in stages.into_iter().enumerate() {
		while stage_num < stage {
			// Step through stages
			// Assert count not yet done
			assert_eq!(stv::count_one_stage(&mut state, &stv_opts).unwrap(), false);
			stage_num += 1;
		}
		validate_stage(idx, &state, &records, cmp_dps, sum_rows);
	}
}

fn validate_stage<N: Number>(idx: usize, state: &CountState<N>, records: &Vec<StringRecord>, cmp_dps: Option<usize>, sum_rows: &[&str])
where
	for<'r> &'r N: ops::Add<&'r N, Output=N>,
{
	// Validate stage name
	let stage_name = &records.iter().nth(1).unwrap()[idx*2 + 1];
	if stage_name.len() > 0 {
		match state.kind {
			Some(kind) => assert_eq!(format!("{} {}", kind, state.title), stage_name),
			None => assert_eq!(state.title, stage_name),
		}
	}
	
	let mut candidate_votes: Vec<Option<N>> = records.iter().skip(2)
		.map(|r| if r[idx*2 + 1].len() > 0 { Some(N::parse(&r[idx*2 + 1])) } else { None })
		.collect();
	
	// Validate exhausted/LBF
	for kind in sum_rows.iter().rev() {
		let votes = candidate_votes.pop().unwrap_or(None);
		if let Some(votes) = votes {
			match kind {
				&"exhausted" => approx_eq(&state.exhausted.votes, &votes, cmp_dps, idx, "exhausted votes"),
				&"lbf" => approx_eq(&state.loss_fraction.votes, &votes, cmp_dps, idx, "LBF"),
				&"nt" => approx_eq(&(&state.exhausted.votes + &state.loss_fraction.votes), &votes, cmp_dps, idx, "NTs"),
				&"quota" => approx_eq(state.quota.as_ref().unwrap(), &votes, cmp_dps, idx, "quota"),
				&"vre" => approx_eq(state.vote_required_election.as_ref().unwrap(), &votes, cmp_dps, idx, "VRE"),
				_ => panic!("Unknown sum_rows"),
			}
		}
	}
	
	// Validate candidate votes
	for (candidate, votes) in state.election.candidates.iter().zip(candidate_votes) {
		let count_card = state.candidates.get(candidate).unwrap();
		if votes.is_some() {
			let votes = votes.unwrap();
			approx_eq(&count_card.votes, &votes, cmp_dps, idx, &format!("votes for candidate {}", candidate.name));
		}
	}
	
	// Validate candidate states
	let mut candidate_states: Vec<&str> = records.iter().skip(2).map(|r| &r[idx*2 + 2]).collect();
	candidate_states.truncate(candidate_states.len() - 2);
	
	for (candidate, candidate_state) in state.election.candidates.iter().zip(candidate_states) {
		let count_card = state.candidates.get(candidate).unwrap();
		if candidate_state == "" {
		}
		else if candidate_state == "H" {
			assert!(count_card.state == CandidateState::Hopeful, "Unexpected state for \"{}\" at index {}", candidate.name, idx);
		} else if candidate_state == "EL" || candidate_state == "PEL" {
			assert!(count_card.state == CandidateState::Elected, "Unexpected state for \"{}\" at index {}", candidate.name, idx);
		} else if candidate_state == "EX" || candidate_state == "EXCLUDING" {
			assert!(count_card.state == CandidateState::Excluded, "Unexpected state for \"{}\" at index {}", candidate.name, idx);
		} else {
			panic!("Unknown state descriptor {}", candidate_state);
		}
	}
}

fn approx_eq<N: Number>(n1: &N, n2: &N, cmp_dps: Option<usize>, idx: usize, description: &str) {
	match cmp_dps {
		Some(dps) => {
			let s1 = format!("{:.dps$}", n1, dps=dps);
			let s2 = format!("{:.dps$}", n2, dps=dps);
			assert!(s1 == s2, "Failed to verify {} for idx {}. Expected {}, got {}", description, idx, s2, s1);
		}
		None => {
			assert!(n1 == n2, "Failed to verify {} for idx {}. Expected {}, got {}", description, idx, n2, n1);
		}
	}
	
}
