<?php


// fetch all data
// handle and report any errors fetching data
// transform data into dictionaries
// return class instance with usabled getBlendedScoreByBt50LegislatorID function
class Rds_Bt50_Scorecard_Score_Blender {
	private $baseScorecardData = null;
	private $sponsorshipScorecardData = null;
	private $presenceData = null;
	private $voteScoreWeight = null;
	private $presenceScoreWeight = null;
	private $sponsorPointsTotal = null;
	private $maxVoteScoreVotes = null;
	private $sponsorshipScoreMultiplier = null;

	public function __construct($bt50Client, $blenderSettings) {
		$baseScorecardId = get_option('score_id');
		$sponsorshipScorecardId = $blenderSettings['sponsorshipScorecardId'];
		$this->voteScoreWeight = $blenderSettings['voteScoreWeight'];
		$this->presenceScoreWeight = $blenderSettings['presenceScoreWeight'];
		$this->sponsorPointsTotal = $blenderSettings['sponsorPointsTotal'];
		$this->maxVoteScoreVotes = $blenderSettings['maxVoteScoreVotes'];
		$this->sponsorshipScoreMultiplier = $blenderSettings['sponsorshipScoreMultiplier'];

		$baseScorecardResponse = $bt50Client->scorecardLegislatorList($baseScorecardId);
		$sponsorshipScorecardResponse = $bt50Client->scorecardLegislatorList($sponsorshipScorecardId);

		$presenceSessionId = $this->getPresenceSessionIdForBaseScorecard($baseScorecardId);
		if (!$presenceSessionId) {
			$presenceSessionId = $bt50Client->getCurrentSessionByScorecard($baseScorecardId);
		}
		$presenceResponse = $bt50Client->presence($presenceSessionId);

		$this->baseScorecardData =
			Rds_Bt50_Scorecard_Dictionary_Generator::generateBt50ScorecardDictionary($baseScorecardResponse);
		$this->sponsorshipScorecardData =
			Rds_Bt50_Scorecard_Dictionary_Generator::generateBt50ScorecardDictionary($sponsorshipScorecardResponse);
		$this->presenceData =
			Rds_Bt50_Scorecard_Dictionary_Generator::generateBt50PresenceDictionary($presenceResponse);
	}

	public function getPresenceSessionIdForBaseScorecard($baseScorecardId) {
		if ($baseScorecardId == 1700) { return 479; }
		return;
	}

	public function getBlendedScoreByBt50LegislatorID($legislatorId) {
		$voteScore = $this->getVoteScore($legislatorId) * $this->voteScoreWeight;
		$sponsorshipScore = $this->getSponsorshipScore($legislatorId);
		$presenceScore = $this->getPresenceScore($legislatorId) * $this->presenceScoreWeight;
		$blendedScore = round($voteScore) + round($sponsorshipScore) + round($presenceScore);
		return $blendedScore;
	}

	public function getVoteScore($legislatorId) {
		return $this->baseScorecardData[$legislatorId]['TotalScore'] / $this->maxVoteScoreVotes * 100;
	}

	public function getSponsorshipScore($legislatorId) {
		$multiplier = $this->sponsorshipScoreMultiplier;
		$score = $this->sponsorshipScorecardData[$legislatorId]['TotalScore'];
		$sponsorshipScore = $score * $multiplier;
		return $sponsorshipScore;
	}

	public function getPresenceScore($legislatorId) {
		return (float) $this->presenceData[$legislatorId]['Presence'] * 100;
	}
}
