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

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\Voting\Model\Vote;

class ReportType extends AbstractType {

	protected static $_reasons = null;

	protected function _votingTypeInfo() {
		return array(
			'label'                   => __( 'Reports', 'directories-pro' ),
			'label_sort'              => _x( 'Report score', 'sort label', 'directories-pro' ),
			'label_button'            => __( 'Report item button', 'directories-pro' ),
			'label_action'            => _x( 'Report %s', 'action', 'directories-pro' ),
			'label_btn'               => _x( 'Report', 'action', 'directories-pro' ),
			'label_statistic'         => __( 'Report count', 'directories-pro' ),
			'icon'                    => 'far fa-flag',
			'icon_active'             => 'fas fa-flag',
			'min'                     => 1,
			'max'                     => 10,
			'step'                    => 1,
			'allow_empty'             => false,
			'allow_anonymous'         => true,
			'require_permission'      => true,
			'require_down_permission' => false,
			'require_own_permission'  => false,
			'permission_label'        => __( 'Report %s', 'directories-pro' ),
			'entity_statistic'        => false,
			'vote_form'               => true,
			'allow_comment'           => true,
			'color'                   => 'danger',
			'dashboard_disable'       => true,
			'vote_table_headers'      => array(
				'author'  => __( 'Reporter', 'directories-pro' ),
				'created' => __( 'Date', 'directories-pro' ),
				'value'   => __( 'Reason', 'directories-pro' ),
				'comment' => __( 'Additional details', 'directories-pro' ),
			),
			'vote_table_submit_label' => __( 'Mark as Reviewed', 'directories-pro' ),
			'entity_table_headers'    => array(
				'entity'  => __( 'Reported item', 'directories-pro' ),
				'bundle'  => __( 'Content type', 'directories-pro' ),
				'count'   => __( 'Number of reports', 'directories-pro' ),
				'average' => __( 'Severity score', 'directories-pro' ),
				'status'  => __( 'Status', 'directories-pro' ),
			),
			'vote_colors'             => array(
				10 => '#990000',
				9  => '#b30000',
				8  => '#cc0000',
				7  => '#e60000',
				6  => '#ff0000',
				5  => '#ff3333',
				4  => '#ff5555',
				3  => '#ff7777',
				2  => '#ff9999',
				1  => '#ffbbbb',
			),
			'vote_deletable'          => false,
		);
	}

	public function votingTypeIsEnabled() {
		return (bool) $this->_application->getComponent( 'Voting' )->getConfig( 'report_enable' );
	}

	public function votingTypeFormat( array $value, $format = null ) {
		switch ( $format ) {
			case 'num':
				return (int) $value['count'];
			case 'column':
				return sprintf( '%d (%04.2f)', $value['count'], $value['average'] );
			default:
				return $this->_application->H( _n( '%d report', '%d reports', $value['count'], 'directories-pro' ) );
		}
	}

	public function votingTypeOnVoteEntityForm( array $form, IEntity $entity, ?Vote $currentVote = null ) {
		if ( ! $reasons = $this->_application->getComponent( 'Voting' )->getConfig( 'report_severity_levels' ) ) {
			$reasons = self::reasons( $this->_application );
		}
		foreach ( $reasons as $score => $reason ) {
			$reasons[ $score ] = sprintf( __( '%1$s (%2$s)', 'directories-pro' ), $reason, sprintf( __( 'Severity %d', 'directories-pro' ), $score ) );
		}
		$form['value']                = array(
			'#title'         => __( 'Reason for the report', 'directories-pro' ),
			'#description'   => __( 'Please select a reason for reporting this item. Severity levels range from 1 (minor issues) to 10 (critical violations).', 'directories-pro' ),
			'#type'          => 'select',
			'#options'       => $reasons,
			'#default_value' => isset( $currentVote ) ? $currentVote->value : 3,
			'#required'      => true,
			'#disabled'      => isset( $currentVote ) && $currentVote->marked,
		);
		$form['comment']['#title']    = __( 'Additional details', 'directories-pro' );
		$form['comment']['#required'] = (bool) $this->_application->getComponent( 'Voting' )->getConfig( 'report_require_comment' );

		return $form;
	}

	public static function reasons( Application $application ) {
		if ( ! isset( self::$_reasons ) ) {
			self::$_reasons = array(
				10 => __( 'Illegal Content', 'directories-pro' ),
				9  => __( 'Violent Threats', 'directories-pro' ),
				8  => __( 'Sexually Explicit Content', 'directories-pro' ),
				7  => __( 'Hate Speech', 'directories-pro' ),
				6  => __( 'Harassment / Bullying', 'directories-pro' ),
				5  => __( 'Impersonation', 'directories-pro' ),
				4  => __( 'Misinformation', 'directories-pro' ),
				3  => __( 'Spam / Advertising', 'directories-pro' ),
				2  => __( 'Off-topic / Irrelevant', 'directories-pro' ),
				1  => __( 'Duplicate / Low Quality', 'directories-pro' ),
			);
		}
		return self::$_reasons;
	}

	public function votingTypeTableRow( Vote $vote, array $tableHeaders ) {
		$row = parent::votingTypeTableRow( $vote, $tableHeaders );
		if ( isset( $tableHeaders['value'] ) ) {
			$colors       = $this->votingTypeInfo( 'vote_colors' );
			$reasons      = self::reasons( $this->_application );
			$row['value'] = isset( $reasons[ $vote->value ] )
				? '<span class="' . DRTS_BS_PREFIX . 'badge" data-voting-report-severity=' . $vote->value . '" style="background-color:' . $this->_application->H( $colors[ $vote->value ] ) . '">' . $this->_application->H( $reasons[ $vote->value ] ) . '</span>'
				: '';
		}
		return $row;
	}
}
