<?php
/**
 * @package     SP Soccer
 * @author      JoomShaper http://www.joomshaper.com
 * @copyright   Copyright (C) 2010 - 2016 JoomShaper. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die('Restricted access');

class SpsoccerViewMatch extends FOFViewHtml{

	public function display($tpl = null){
		// Get model
		$model = $this->getModel();
		$this->item = $model->getItem();

		$tournament_model = FOFModel::getTmpInstance('Tournaments', 'SpsoccerModel');
		$this->team1_info = $tournament_model->getTeamInfoById($this->item->teamone);
		$team_one_url  = JRoute::_('index.php?option=com_spsoccer&view=team&id=' . $this->team1_info->spsoccer_gameteam_id . ':' . $this->item->slug . SpsoccerHelper::getItemid('teams'));
		$this->team1_info->logo = SpsoccerHelper::hasImg($this->team1_info->logo);
		$this->team2_info = $tournament_model->getTeamInfoById($this->item->teamtwo);
		$team_two_url  = JRoute::_('index.php?option=com_spsoccer&view=team&id=' . $this->team2_info->spsoccer_gameteam_id . ':' . $this->item->slug . SpsoccerHelper::getItemid('teams'));
		$this->team2_info->logo = SpsoccerHelper::hasImg($this->team2_info->logo);

		// Team one Score List //
		$this->teamone_score_lists 	= json_decode($this->item->list_teamone_score);

		// has team one player goal
		if (isset($this->teamone_score_lists->player_goal)) {
			//Team one Goals
			$teamone_playergoals = $this->teamone_score_lists->player_goal;
			// Team one player id
			$teamone_playenames	 = $this->teamone_score_lists->player_name;

			// Team one goal player
			$this->t1_g_players = array();
			foreach ($teamone_playergoals as $key => $teamone_playergoal) {
				if($teamone_playergoal) {
					$this->t1_g_players[] = $model->getPlayerInfo($teamone_playenames[$key]);
				}
			}

		}

		// Team one Score List //
		$this->teamtwo_score_lists 	= json_decode($this->item->list_teamtwo_score);

		// Has tam two player goal
		if (isset($this->teamtwo_score_lists->player_goal)) {
			//Team one Goals
			$teamtwo_playergoals	= $this->teamtwo_score_lists->player_goal;
			$teamtwo_playenames	= $this->teamtwo_score_lists->player_name;

			// Team one goal player
			$this->t2_g_players = array();
			foreach ($teamtwo_playergoals as $key => $teamtwo_playergoal) {
				if($teamtwo_playergoal) {
					$this->t2_g_players[] = $model->getPlayerInfo($teamtwo_playenames[$key]);
				}
			}
		}

		// Get match Stats
		if (isset($this->item->list_match_stats)) {
			$this->match_stats_lists = json_decode($this->item->list_match_stats);

			// Get Match stat types
			$types   = $this->match_stats_lists->type;

			// Get no of stats
			$teamone  = $this->match_stats_lists->teamone;
			$teamtwo = $this->match_stats_lists->teamtwo;

			// match stats as row
			$this->match_stats = array();
			foreach ($types as $team => $type) {
				$teamone[$team] = ($teamone[$team]) ? $teamone[$team] : '0' ;
				$teamtwo[$team] = ($teamtwo[$team]) ? $teamtwo[$team] : '0' ;
				$stat_type 		= str_replace('_', ' ', $type);
				$this->match_stats[$type] = array(
					'type'     	=> $stat_type,
					'teamone' 	=> $teamone[$team],
					'teamtwo' 	=> $teamtwo[$team],
				);
			}
		}

		// *** Match Timeline *** //

		//Has match time line
		if (isset($this->item->list_match_timeline)) {

			// Get Timeline
			$this->match_timeline_decode 	= json_decode($this->item->list_match_timeline);
			//Get values
			$match_timeline_times			= $this->match_timeline_decode->min;
			$match_timeline_types			= $this->match_timeline_decode->type;
			$match_timeline_teams			= $this->match_timeline_decode->team;
			$match_timeline_players			= $this->match_timeline_decode->player;

			$timeline_key = 0;
			$goal_key = 0;
			// Match goals
			$match_goals = array();
			// Match timeline
			$this->match_timelines = array();
			foreach ($match_timeline_times as $key => $match_timeline_time) {
				//Replace type _ by space
				$match_timeline_name[$key] = $match_timeline_types[$key];
				$match_timeline_types[$key] = str_replace('_', ' ', $match_timeline_types[$key]);
				// Get player info
				$match_players_info[$key] = $model->getPlayerInfo($match_timeline_players[$key]);
				$match_players_name		  = $match_players_info[$key]->title;
				$match_players_squad_no	  = $match_players_info[$key]->squad_no;

				//Get team id
				$match_timeline_teams[$key] = ($match_timeline_teams[$key] == 'team_one') ? $this->item->teamone: $this->item->teamtwo;
				// Get team info
				$match_timeline_teams[$key]= $tournament_model->getTeamInfoById($match_timeline_teams[$key])->title;

				// Timelines
				$this->match_timelines[$timeline_key] = array(
					'time'  	=> $match_timeline_times[$key],
					'name' 		=> $match_timeline_name[$key],
					'type' 		=> $match_timeline_types[$key],
					'team'   	=> $match_timeline_teams[$key],
					'player'   	=> $match_players_name,
					'squad_no'  => $match_players_squad_no,
				);

				// Goals
				if ($match_timeline_types[$key] == 'goal') {
					$match_goals[$goal_key] = array(
						'time'  	=> $match_timeline_times[$key],
						'type' 		=> $match_timeline_types[$key],
						'team'   	=> $match_timeline_teams[$key],
						'player'   	=> $match_players_name,
					);
					$goal_key ++;
				}

				$timeline_key ++;
			} // END:: foreach $match_timelines

			// before half time
			$this->match_goals_before45s = array();
			foreach ($match_goals as $match_goal) {
				if ($match_goal['time'] <= 45) {
					$this->match_goals_before45s[] = $match_goal;
				}
			}

			// after half time
			$this->match_goals_after45s = array();
			foreach ($match_goals as $match_goal) {
				if ($match_goal['time'] > 45) {
					$this->match_goals_after45s[] = $match_goal;
				}
			}

		} // Has match time line

		// *** Match Teams players *** //
		// Get team one players infos
		$this->teamone_players = array();
		foreach ($this->item->teamone_players as $this->item->teamone_player) {
			$this->teamone_players[] = $model->getPlayerInfo($this->item->teamone_player);
		}

		// Get team two players infos
		$this->teamtwo_players = array();
		foreach ($this->item->teamtwo_players as $this->item->teamtwo_player) {
			$this->teamtwo_players[] = $model->getPlayerInfo($this->item->teamtwo_player);
		}


		// *** Match Stubstitutes *** //

		// Has substitutes
		if (isset($this->item->list_substitutes)) {
			// Get substitutes
			$this->match_subs_decode 	= json_decode($this->item->list_substitutes);
			//Get values
			$match_subs_times			= $this->match_subs_decode->min;
			$match_subs_team			= $this->match_subs_decode->team;
			$match_subs_player_up		= $this->match_subs_decode->player_up;
			$match_subs_player_down		= $this->match_subs_decode->player_down;

			$subs_key = 0;
			// Match timeline
			$this->match_subs = array();
			foreach ($match_subs_times as $key => $match_subs_time) {
				if (isset($match_subs_time)) {
					// // Get player info
					$match_subs_player_up_info[$key] 	= $model->getPlayerInfo($match_subs_player_up[$key]);
					$match_subs_player_down_info[$key] 	= $model->getPlayerInfo($match_subs_player_down[$key]);
					// Get team info //
					//Get team id
					$match_subs_team[$key] = ($match_subs_team[$key] == 'team_one') ? $this->item->teamone: $this->item->teamtwo;
					// Get team info
					$match_subs_team[$key] = $tournament_model->getTeamInfoById($match_subs_team[$key]);

					$this->match_subs[$subs_key] = array(
						'time'  		=> $match_subs_times[$key],
						'team_name'		=> $match_subs_team[$key]->title,
						'team_logo' 	=> $match_subs_team[$key]->logo,
						'p_up_name'		=> $match_subs_player_up_info[$key]->title,
						'p_up_no'		=> $match_subs_player_up_info[$key]->squad_no,
						'p_down_name'	=> $match_subs_player_down_info[$key]->title,
						'p_down_no'   	=> $match_subs_player_down_info[$key]->squad_no,
						);
					$subs_key ++;
				}
			}
		}

		// get groups of this tournament
		//$this->groups = $model->getTournamentGroups($this->item->spsoccer_tournament_id);
		$this->tournament = $model->getTournamentInfo($this->item->spsoccer_tournament_id);

		return parent::display($tpl = null);
	}
}
