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

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Entity;
use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\View\Model;

class AdminViewHelper {

	public function add( Application $application, Entity\Model\Bundle $bundle, $name, $mode, $label, array $settings, $default = false ) {
		if ( $this->exists( $application, $bundle->name, $name ) ) {
			return;
		}

		$view              = $application->getModel( 'View', 'View' )->create();
		$view->bundle_name = $bundle->name;
		$view->name        = $name;
		$view->mode        = $mode;
		$view->data        = array(
			'label'    => $label,
			'settings' => $settings,
		);
		$view->default     = $default ? 1 : 0;
		$view->markNew()->commit();

		$application->Action( 'view_admin_view_added', array( $view ) );

		return $view;
	}

	public function update( Application $application, Model\View $view, $name, $mode, $label, array $settings ) {
		$view->name = $name;
		$view->mode = $mode;
		$view->data = array(
			'label'    => $label,
			'settings' => $settings,
		);
		$view->commit();

		$application->Action( 'view_admin_view_edited', array( $view ) );

		return $view;
	}

	public function exists( Application $application, $bundleName, $name ) {
		return $application->getModel( 'View', 'View' )
			->name_is( $name )
			->bundleName_is( $bundleName )
			->fetchOne();
	}

	public function setDefault( Application $application, Entity\Model\Bundle $bundle, Model\View $view, $force = false ) {
		$commit        = false;
		$current_views = $application->getModel( 'View', 'View' )->bundleName_is( $bundle->name )->default_is( true )->fetch();
		$current       = $current_views->getNext();
		// Set other views as non-default since there should be only one default view
		while ( $_view = $current_views->getNext() ) {
			$_view->default = 1;
			$commit         = true;
		}
		if ( $force
			|| ! $current // no default view set
		) {
			if ( $current ) {
				$current->default = 0;
			}
			$view->default = 1;
			$commit        = true;
		}
		if ( $commit ) {
			$application->getModel( null, 'View' )->commit();
		}
	}

	public function onDisplayAdminElementsFormFilter( Application $application, &$form, Bundle $bundle, $display, $parents ) {
		if ( $display['type'] === 'entity' ) {
			if ( strpos( $display['name'], 'summary' ) === 0 ) {
				$form['view_add_border']   = array(
					'#type'          => 'checkbox',
					'#title'         => __( 'Add border', 'directories-pro' ),
					'#default_value' => ! isset( $display['data']['view_add_border'] ) || ! empty( $display['data']['view_add_border'] ),
					'#weight'        => 20,
					'#class'         => 'drts-form-label-lg ' . DRTS_BS_PREFIX . 'mb-3',
					'#horizontal'    => true,
				);
				$form['view_border_color'] = array(
					'#type'             => 'radios',
					'#options'          => $application->System_Color_options( array( 'include_outline' => false ) ),
					'#option_no_escape' => true,
					'#default_value'    => ! empty( $display['data']['view_border_color'] ) ? $display['data']['view_border_color'] : 'secondary',
					'#weight'           => 21,
					'#states'           => $add_border_states = array(
						'visible' => array(
							sprintf( 'input[name="%s"]', $application->Form_Form_fieldName( array( $parents, 'view_add_border' ) ) ) => array(
								'type'  => 'checked',
								'value' => true,
							),
						),
					),
					'#columns'          => 8,
					'#class'            => DRTS_BS_PREFIX . 'mb-3',
					'#horizontal'       => true,
				);
				$form['view_border_opacity']  = array(
					'#type'          => 'slider',
					'#field_prefix'  => __( 'Opacity', 'directories-pro' ),
					'#min'           => 1,
					'#max'           => 100,
					'#step'          => 1,
					'#field_suffix'  => '%',
					'#default_value' => ! empty( $display['data']['view_border_opacity'] ) ? $display['data']['view_border_opacity'] : 20,
					'#weight'        => 22,
					'#states'        => $add_border_states,
					'#class'         => DRTS_BS_PREFIX . 'mb-3',
					'#horizontal'    => true,
				);
				$form['view_add_shadow']      = array(
					'#type'          => 'select',
					'#title'         => __( 'Add shadow effect', 'directories-pro' ),
					'#options'       => array(
						''   => __( 'None', 'directories-pro' ),
						'sm' => __( 'Small', 'directories-pro' ),
						'md' => __( 'Medium', 'directories-pro' ),
						'lg' => __( 'Large', 'directories-pro' ),
					),
					'#default_value' => empty( $display['data']['view_add_shadow'] ) ? '' : $display['data']['view_add_shadow'],
					'#weight'        => 25,
					'#horizontal'    => true,
				);
				$form['enable_border_radius'] = array(
					'#type'          => 'checkbox',
					'#title'         => __( 'Enable border radius', 'directories-pro' ),
					'#default_value' => ! empty( $display['data']['enable_border_radius'] ),
					'#weight'        => 26,
					'#horizontal'    => true,
				);
			}
		}
	}

	public function onDisplayAdminDisplayDataKeysFilter( Application $application, &$keys, Bundle $bundle, $display ) {
		if ( $display->type === 'entity' ) {
			if ( strpos( $display->name, 'summary' ) === 0 ) {
				$keys[] = 'view_add_border';
				$keys[] = 'view_border_color';
				$keys[] = 'view_border_opacity';
				$keys[] = 'view_add_shadow';
				$keys[] = 'enable_border_radius';
			}
		}
	}
}
