<?php
/**
Plugin Admin Page
**/

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

// Settings Page: Gallery Settings
class gallerysettings_Settings_Page {

	public function __construct() {
		add_action( 'admin_menu', array( $this, 'wph_create_settings' ) );
		add_action( 'admin_init', array( $this, 'wph_setup_sections' ) );
		add_action( 'admin_init', array( $this, 'wph_setup_fields' ) );
	}

	public function wph_create_settings() {
		$page_title = 'Gallery Settings';
		$menu_title = 'Gallery Settings';
		$capability = 'manage_options';
		$slug = 'gallerysettings';
		$callback = array($this, 'wph_settings_content');
		$icon = 'dashicons-format-gallery';
		$position = 25;
		add_menu_page($page_title, $menu_title, $capability, $slug, $callback, $icon, $position);
	}

	public function wph_settings_content() { ?>
		<div class="wrap">
			<h1>Gallery Settings</h1>
			<?php settings_errors(); ?>
			<form method="POST" action="options.php">
				<?php
					settings_fields( 'gallerysettings' );
					do_settings_sections( 'gallerysettings' );
					submit_button();
				?>
			</form>
		</div> <?php
	}

	public function wph_setup_sections() {
		add_settings_section( 'gallerysettings_section', '', array(), 'gallerysettings' );
	}

	public function wph_setup_fields() {
		$fields = array(
			array(
				'label' => 'Layout',
				'id' => 'pgsfw_layout',
				'type' => 'select',
				'section' => 'gallerysettings_section',
				'options' => array(
					'0' => 'Horizontal',
					'1' => 'Vertical',
				),
				'placeholder' => 'Select Layout',
			),
			array(
				'label' => 'Layout Style',
				'id' => 'pgsfw_lstyle',
				'type' => 'select',
				'section' => 'gallerysettings_section',
				'options' => array(
					'left' => 'Left',
					'right' => 'Right',
				),
				'placeholder' => 'Select Style',
			),
			array(
				'label' => 'Main Slider Arrow Disable',
				'id' => 'pgsfw_msadisable',
				'type' => 'checkbox',
				'section' => 'gallerysettings_section',
				'placeholder' => 'Check / Uncheck',
			),
			array(
				'label' => 'Thumbnail Arrow Disable',
				'id' => 'pgsfw_tadisable',
				'type' => 'checkbox',
				'section' => 'gallerysettings_section',
				'placeholder' => 'Check / Uncheck',
			),
			
			array(
				'label' => 'Display Gallery Images',
				'id' => 'pgsfw_dgimg',
				'type' => 'text',
				'section' => 'gallerysettings_section',
				'placeholder' => 'Number of Images',
			),

			array(
				'label' => 'Video Gallery Height',
				'id' => 'pgsfw_dgvgh',
				'type' => 'text',
				'section' => 'gallerysettings_section',
				'placeholder' => '350',
			),
			
			array(
				'label' => 'Slider Arrow Left Position',
				'id' => 'pgsfw_salp',
				'type' => 'text',
				'section' => 'gallerysettings_section',
				'placeholder' => '15px',
			),
			array(
				'label' => 'Slider Arrow Right Position',
				'id' => 'pgsfw_sarp',
				'type' => 'text',
				'section' => 'gallerysettings_section',
				'placeholder' => '15px',
			),
			
			array('label' => 'Arrow Background Color',
				'id' => 'arbgcolor',
				'type' => 'text',
				'section' => 'gallerysettings_section',
				'placeholder' => '#1a1a1a',
			),
			array(
				'label' => 'Arrow Icon Color',
				'id' => 'aricolor',
				'type' => 'text',
				'section' => 'gallerysettings_section',
				'placeholder' => '#fff',
			),
			
			array('label' => 'Arrow Hover Background Color',
				'id' => 'harbgcolor',
				'type' => 'text',
				'section' => 'gallerysettings_section',
				'placeholder' => '#4A3AFF',
			),
			array(
				'label' => 'Arrow Hover Icon Color',
				'id' => 'haricolor',
				'type' => 'text',
				'section' => 'gallerysettings_section',
				'placeholder' => '#fff',
			),
			
			array(
				'label' => 'Main Slider Arrow  Size',
				'id' => 'mslidersize',
				'type' => 'text',
				'section' => 'gallerysettings_section',
				'placeholder' => '45px',
			),
			array(
				'label' => 'Thumbnail Arrow Size',
				'id' => 'tarrsize',
				'type' => 'text',
				'section' => 'gallerysettings_section',
				'placeholder' => '30px',
			),			
			
		);
		foreach( $fields as $field ){
			add_settings_field( $field['id'], $field['label'], array( $this, 'wph_field_callback' ), 'gallerysettings', $field['section'], $field );
			register_setting( 'gallerysettings', $field['id'] );
		}
	}

	public function wph_field_callback( $field ) {
		$value = get_option( $field['id'] );
		$placeholder = '';
		if ( isset($field['placeholder']) ) {
			$placeholder = $field['placeholder'];
		}
		switch ( $field['type'] ) {
				case 'checkbox':
					printf('<input %s id="%s" name="%s" type="checkbox" value="1">',
						$value === '1' ? 'checked' : '',
						$field['id'],
						$field['id']
				);
					break;
				case 'select':
				case 'multiselect':
					if( ! empty ( $field['options'] ) && is_array( $field['options'] ) ) {
						$attr = '';
						$options = '';
						foreach( $field['options'] as $key => $label ) {
							$options.= sprintf('<option value="%s" %s>%s</option>',
								$key,
								selected($value, $key, false),
								$label
							);
						}
						if( $field['type'] === 'multiselect' ){
							$attr = ' multiple="multiple" ';
						}
						printf( '<select name="%1$s" id="%1$s" %2$s>%3$s</select>',
							$field['id'],
							$attr,
							$options
						);
					}
					break;
			default:
				printf( '<input name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" />',
					$field['id'],
					$field['type'],
					$placeholder,
					$value
				);
		}
		if( isset($field['desc']) ) {
			if( $desc = $field['desc'] ) {
				printf( '<p class="description">%s </p>', $desc );
			}
		}
	}
}
new gallerysettings_Settings_Page();