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

use SabaiApps\Directories\Component\Dashboard;
use SabaiApps\Framework\User\AbstractIdentity;

class VotesDashboardPanel extends Dashboard\Panel\AbstractPanel {

	protected function _dashboardPanelInfo() {
		return array(
			'label'      => __( 'Votes', 'directories-pro' ),
			'weight'     => 5,
			'wp_um_icon' => 'um-faicon-thumbs-up',
		);
	}

	protected function _dashboardPanelLinks( array $settings, ?AbstractIdentity $identity = null ) {
		if ( isset( $identity ) ) {
			return; // Do not show if public dashboard
		}

		$types = isset( $settings['types']['default'] ) ? $settings['types']['default'] : array_keys( $this->_application->Voting_Types() );
		if ( empty( $types ) ) {
			return array();
		}

		$counts = $this->_application->getModel( null, 'Voting' )->getGateway( 'Vote' )->countVotesByUser( $types, $this->_application->getUser()->id );
		$ret    = array();
		$weight = 0;
		foreach ( $types as $type ) {
			if ( ( ! $type_impl = $this->_application->Voting_Types_impl( $type ) )
				|| ( ! $type_info = $type_impl->votingTypeInfo() )
			) {
				continue;
			}

			$ret[ $type ] = array(
				'title'  => $type_info['label'],
				'icon'   => $type_info['icon'],
				'weight' => ++$weight,
			);
			if ( isset( $settings['types']['options'][ $type ] ) ) {
				$ret[ $type ]['title']           = $settings['types']['options'][ $type ];
				$ret[ $type ]['title_is_custom'] = true;
			}
			if ( ! empty( $counts[ $type ] ) ) {
				$ret[ $type ]['count'] = $counts[ $type ];
			}
		}

		return $ret;
	}

	public function dashboardPanelSettingsForm( array $settings, array $parents ) {
		foreach ( array_keys( $this->_application->Voting_Types() ) as $type ) {
			if ( ( ! $type_impl = $this->_application->Voting_Types_impl( $type ) )
				|| ( ! $type_info = $type_impl->votingTypeInfo() )
				|| ! empty( $type_info['dashboard_disable'] )
			) {
				continue;
			}

			$types[ $type ] = $type_info['label'];
		}
		return array(
			'types' => array(
				'#title'                  => __( 'Vote types', 'directories-pro' ),
				'#type'                   => 'options',
				'#horizontal'             => true,
				'#disable_add'            => true,
				'#disable_icon'           => true,
				'#disable_add_csv'        => true,
				'#multiple'               => true,
				'#options_value_disabled' => true,
				'#default_value'          => array(
					'options' => isset( $settings['types']['options'] ) ? $settings['types']['options'] + $types : $types,
					'default' => isset( $settings['types']['default'] ) ? $settings['types']['default'] : array_keys( $types ),
				),
				'#options_placeholder'    => $types,
			),
		);
	}

	public function dashboardPanelContent( $link, array $settings, array $params, ?AbstractIdentity $identity = null ) {
		return $this->_application->getPlatform()->render(
			$this->_application->Dashboard_Panels_panelUrl( 'voting_votes', $link, '/votes', array(), true, $identity ),
			array(
				'is_dashboard' => false, // prevent rendering duplicate panel sections on reload panel
				'identity'     => $identity,
			)
		);
	}

	public function dashboardPanelOnLoad( $isPublic = false ) {
		if ( $this->_application->getPlatform()->isAdmin() ) {
			return;
		}

		$this->_application->getPlatform()->loadJqueryUiJs( array( 'effects-highlight' ) );
	}
}
