<?php
namespace SabaiApps\Directories\Component\Voting\Helper;

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Entity\Type\IEntity;

class VotesByEntityHelper {

	public function help( Application $application, $voteType = 'report' ) {
		$vote_type_impl = $application->Voting_Types_impl( $voteType );
		$voted_items    = $application->getModel( null, 'Voting' )
			->getGateway( 'Vote' )
			->getVotedItems( $voteType );
		$options        = array();
		foreach ( $voted_items as $bundle_name => $_voted_items ) {
			foreach ( $_voted_items as $entity_id => $vote_stats ) {
				if ( ( ! $bundle = $application->Entity_Bundle( $bundle_name ) )
					|| ( ! $entity = $application->Entity_Entity( $bundle->entitytype_name, $entity_id ) )
					|| ( ! $entity_type = $application->Entity_Types_impl( $entity->getType() ) )
				) {
					continue;
				}

				$options[ $entity_id ] = $this->_getTableRow(
					$application,
					$entity,
					$voteType,
					$vote_stats['count'],
					$vote_stats['sum'],
					$vote_stats['average']
				);
			}
		}
		$form = array(
			'#type'           => 'tableselect',
			'#class'          => 'drts-data-table drts-voting-votes',
			'#header'         => $vote_type_impl->votingTypeInfo( 'entity_table_headers' ),
			'#options'        => $options,
			// '#multiple' => true,
			// '#js_select' => true,
			'#disabled'       => true,
			'#row_attributes' => array(
				'@all' => array(
					'entity' => array(
						'style' => 'width:35%;',
					),
				),
			),
		);

		return $form;
	}

	protected function _getTableRow( Application $application, IEntity $entity, $voteType, $voteCount, $voteSum, $voteAverage ) {
		$vote_type_impl = $application->Voting_Types_impl( $voteType );
		$table_headers  = $vote_type_impl->votingTypeInfo( 'entity_table_headers' );
		$ret            = array();
		if ( isset( $table_headers['entity'] ) ) {
			if ( $application->getPlatform()->isAdmin() ) {
				$ret['entity'] = '<a href="' . $application->Entity_AdminUrl( $entity ) . '">' . $application->H( $application->Entity_Title( $entity ) ) . '</a>';
			} else {
				$ret['entity'] = $entity->isPublished() ? $application->Entity_Permalink( $entity ) : $application->H( $application->Entity_Title( $entity ) );
			}
		}
		if ( isset( $table_headers['bundle'] ) ) {
			$ret['bundle'] = ( $bundle = $application->Entity_Bundle( $entity ) )
				? $application->H( $bundle->getGroupLabel() . ' - ' . $bundle->getLabel( 'singular' ) )
				: '';
		}
		if ( isset( $table_headers['status'] ) ) {
			$ret['status'] = '';
			if ( ( $status = $entity->getStatus() )
				&& ( $entity_type = $application->Entity_Types_impl( $entity->getType() ) )
			) {
				$ret['status'] = $application->H( $entity_type->entityTypeEntityStatusLabel( $status ) );
			}
		}
		if ( isset( $table_headers['count'] ) ) {
			$ret['count'] = $application->LinkTo(
				'<span class="drts-voting-vote-num"> ' . (int) $voteCount . '</span>',
				$application->Url(
					'/directories/system/voting_votes',
					array(
						'entity_type' => $entity->getType(),
						'entity_id'   => $entity->getId(),
						'voting_type' => $voteType,
					)
				),
				array(
					'container' => 'modal',
					'modalSize' => 'xl',
					'no_escape' => true,
				),
				array(
					'data-modal-title' => $vote_type_impl->votingTypeInfo( 'label' ) . ' - ' . $application->Entity_Title( $entity ),
				)
			);
		}
		if ( isset( $table_headers['sum'] ) ) {
			$ret['sum'] = '<span class="drts-voting-vote-sum"> ' . (int) $voteSum . '</span>';
		}
		if ( isset( $table_headers['average'] ) ) {
			$ret['average'] = number_format( $voteAverage, 2, '.', '' );
			if ( ( $level = (int) round( $voteAverage ) )
				&& ( $colors = $vote_type_impl->votingTypeInfo( 'vote_colors' ) )
				&& isset( $colors[ $level ] )
			) {
				$ret['average'] = '<span class="' . DRTS_BS_PREFIX . 'badge drts-voting-vote-average" style="background-color:' . $colors[ $level ] . ';">' . $ret['average'] . '</span>';
			}
		}
		return $ret;
	}
}
