<?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 SpsoccerViewTournament extends FOFViewHtml{

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

		//Joomla Component Helper & Get LMS Params
		$app 				= JFactory::getApplication();
		$menuItem 			= $app->getMenu()->getActive();
		$this->show_group 	= $menuItem->params->get('show_group', 1);
		$fixture_order_by 	= $menuItem->params->get('fixture_order_by', 'ASC');
		$fixture_limit 			= $menuItem->params->get('fixture_limit', '');


		// Get last played match
		$this->last_played_match = $model->getLastPlayedMatch($this->item->spsoccer_tournament_id);
		if ($this->last_played_match) {
			$this->last_played_match->teamone_logo = SpsoccerHelper::hasImg($this->last_played_match->teamone_logo);
			$this->last_played_match->teamtwo_logo = SpsoccerHelper::hasImg($this->last_played_match->teamtwo_logo);
		}

		$this->match_banner = (!empty($this->last_played_match->image)) ? 'style="background-image: url(' . $this->last_played_match->image . ')"' : '' ;

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

		foreach ($this->groups as $this->group) {
			$this->group->title = (strpos($this->group->slug, 'uncategory') !== FALSE) ? JText::_('COM_SPSOOCER_GROUP') : $this->group->title;
			$this->group->ungroup = (strpos($this->group->slug, 'uncategory') !== FALSE) ? 'ungroup' : '';
		}

		// Get Sessions by Tournament ID
		$this->sessions = $model->getTournamentSessions($this->item->spsoccer_tournament_id);

		// Get Matches By Session and Tournament
		foreach ($this->sessions as &$this->session) {
			$this->session->session_matches = $model->getSessionMatches($this->session->spsoccer_session_id, $this->item->spsoccer_tournament_id, $fixture_order_by, $fixture_limit);
			$this->session->title = (strpos($this->group->slug, 'uncategory') !== FALSE) ? JText::_('COM_SPSOOCER_SESSION') : $this->session->title;

			foreach ($this->session->session_matches as &$this->session->session_match) {
				//Get team one info
				$this->session->session_match->team_one_info = $model->getTeamInfoById($this->session->session_match->teamone);

				$this->session->session_match->team_one_url = JRoute::_('index.php?option=com_spsoccer&view=gameteam&id=' . $this->session->session_match->team_one_info->spsoccer_gameteam_id . ':' . $this->session->session_match->team_one_info->slug . SpsoccerHelper::getItemid('gameteams'));

				//Get team two info
				$this->session->session_match->team_two_info = $model->getTeamInfoById($this->session->session_match->teamtwo);

				$this->session->session_match->team_two_url = JRoute::_('index.php?option=com_spsoccer&view=gameteam&id=' . $this->session->session_match->team_two_info->spsoccer_gameteam_id . ':' . $this->session->session_match->team_two_info->slug . SpsoccerHelper::getItemid('gameteams'));

				//Match Date
				$this->session->session_match->match_date = JHtml::date($this->session->session_match->date, 'd M, Y');
				//Match Month
				$this->session->session_match->match_time = date ('h:i A',strtotime($this->session->session_match->time));
				//Match Location
				$this->session->session_match->match_location = $model->getVanueInfoById($this->session->session_match->spsoccer_vanue_id);

				$this->session->session_match->location_url = JRoute::_('index.php?option=com_spsoccer&view=vanue&id=' . $this->session->session_match->match_location->spsoccer_vanue_id . ':' . $this->session->session_match->match_location->slug . SpsoccerHelper::getItemid('vanues'));


			} //END:: $this->session->session_matches

			//Get Sessions Played Match
			$this->session->played_matches = $model->getSessionPlayedMatches($this->session->spsoccer_session_id, $this->item->spsoccer_tournament_id);

			foreach ($this->session->played_matches as &$this->session->played_match) {

				// Generate Match URL
				//$this->session->played_match->team_one_info = $model->getTeamInfoById($this->session->played_match->teamone);
				$this->session->played_match->url = JRoute::_('index.php?option=com_spsoccer&view=match&id=' . $this->session->played_match->spsoccer_match_id . ':' . $this->session->played_match->slug . SpsoccerHelper::getItemid('matches'));

				//Get team one info
				$this->session->played_match->team_one_info = $model->getTeamInfoById($this->session->played_match->teamone);

				$this->session->played_match->team_one_info->logo = SpsoccerHelper::hasImg($this->session->played_match->team_one_info->logo);

				$this->session->played_match->team_one_url = JRoute::_('index.php?option=com_spsoccer&view=gameteam&id=' . $this->session->played_match->team_one_info->spsoccer_gameteam_id . ':' . $this->session->played_match->team_one_info->slug . SpsoccerHelper::getItemid('gameteams'));

				//Get team two info
				$this->session->played_match->team_two_info = $model->getTeamInfoById($this->session->played_match->teamtwo);

				$this->session->played_match->team_two_info->logo = SpsoccerHelper::hasImg($this->session->played_match->team_two_info->logo);

				$this->session->played_match->team_two_url = JRoute::_('index.php?option=com_spsoccer&view=gameteam&id=' . $this->session->played_match->team_two_info->spsoccer_gameteam_id . ':' . $this->session->played_match->team_two_info->slug . SpsoccerHelper::getItemid('gameteams'));

				//Match Date
				$this->session->played_match->match_date = JHtml::date($this->session->played_match->date, 'd M, Y');
				//Match Month
				$this->session->played_match->match_time = date ('h:i A',strtotime($this->session->played_match->time));
				//Match Location
				$this->session->played_match->match_location = $model->getVanueInfoById($this->session->played_match->spsoccer_vanue_id);

			}

		} // END:: foreach $this->session->session_matches

		// Team Standings

		// $this->teams_ids = array();
		// foreach ($this->groups as $this->group) {
		// 	$group_team_ids = json_decode($this->group->spsoccer_gameteam_id);
		// 	foreach ($group_team_ids as &$group_team_id) {
		// 		$this->teams_ids[] = $group_team_id;
		// 	}
		// }
		// $this->uniqie_teams_ids = array_unique($this->teams_ids);

		// // Team info
		// $this->unique_team_info = array();

		// foreach ($this->uniqie_teams_ids as $this->uniqie_teams_id) {
		// 	$this->unique_team_info[] = $model->getTeamInfoById($this->uniqie_teams_id);
		// 	//print_r($this->team_info);
		// }


		// Point Table


		foreach ($this->groups as &$this->sgroup) {
			//title
			$this->sgroup->uncategory = (strpos($this->group->slug, 'uncategory') !== FALSE) ? 'uncategory' : '';

			// Get Timeline
			$point_table_decode 	= json_decode($this->sgroup->point_table);

			if ($point_table_decode) {
				// Point table
				$teams  	= $point_table_decode->team;
				$playeds	= $point_table_decode->played;
				$wons 		= $point_table_decode->won;
				$draws		= $point_table_decode->draw;
				$losts		= $point_table_decode->lost;
				$gforwards	= $point_table_decode->gforward;
				$gagainsts  = $point_table_decode->gagainst;
				$avgs   	= $point_table_decode->avg;
				$ptss  		= $point_table_decode->pts;

				$point_tables = array();
				$pt_key = 0;
				foreach ($teams as $id => $team) {
					$team_info 	= $model->getTeamInfoById($teams[$id]);
					$team_title = $team_info->title;
					$team_logo 	= $team_info->logo;


					$point_tables[$pt_key] = array(
						'team_name' => $team_title,
						'team_logo' => $team_logo,
						'plays' 	=> $playeds[$id],
						'wons'    	=> $wons[$id],
						'draws'    	=> $draws[$id],
						'losts'    	=> $losts[$id],
						'gforwards' => $gforwards[$id],
						'gagainsts' => $gagainsts[$id],
						'avgs'    	=> $avgs[$id],
						'ptss'    	=> $ptss[$id],
					);

					$pt_key ++;
				}

				$this->sgroup->point_tables = $model->msort($point_tables, array('ptss'));

			} // END:: has point table

		} // END:: foreach $this->groups



		// Match Location
		$this->match_location = $model->getVanueInfoById($this->item->spsoccer_tournament_id);



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