<?php

class Builder_Tiles_Admin {

	public static function init() {
		add_action( 'admin_menu', array( __CLASS__, 'setup_options' ), 100 );
		add_action( 'admin_init', array( __CLASS__, 'page_init' ) );
		add_filter( 'themify_do_metaboxes', array(__CLASS__, 'themify_do_metaboxes') );
	}

	public static function setup_options() {
		$parent_page = themify_is_themify_theme() ? 'themify' : 'themify-builder';
		add_submenu_page( $parent_page, __( 'Tiles Module', 'builder-tiles' ), __( 'Builder Tiles', 'builder-tiles' ), 'manage_options', 'builder-tiles', array( __CLASS__, 'create_admin_page' ) );
	
	}

	public static function create_admin_page() {
		?>
		<div class="wrap">
			<h2><?php _e( 'Builder Tiles Module', 'builder-tiles' ); ?></h2>           
			<form method="post" action="options.php">
				<?php
				// This prints out all hidden setting fields
				settings_fields( 'builder_tiles' );   
				do_settings_sections( 'builder-tiles' );
				submit_button(); 
				?>
			</form>
		</div>
		<?php
	}
        
	/**
	 * Register and add settings
	 */
	public static function page_init() {        
		register_setting(
			'builder_tiles', // Option group
			'builder_tiles' // Option name
		);

		add_settings_section(
			'builder-tiles-responsive', // ID
			__( 'Responsive Tiles', 'builder-tiles' ), // Title
			null, // Callback
			'builder-tiles' // Page
		);

		add_settings_field(
			'fluid_tiles', // ID
			__( 'Responsive Tiles', 'builder-contact' ), // Title 
			array( __CLASS__, 'render_option' ), // Callback
			'builder-tiles', // Page
			'builder-tiles-responsive' // Section           
		);

		add_settings_field(
			'fluid_tiles_base_size', // ID
			__( 'Fluid Tile Base Size', 'builder-contact' ), // Title 
			array( __CLASS__, 'tiles_size_control' ), // Callback
			'builder-tiles', // Page
			'builder-tiles-responsive' // Section           
		);

		add_settings_field(
			'tiles_gutter', // ID
			__( 'Tile Spacing', 'builder-contact' ), // Title 
			array( __CLASS__, 'render_spacing_field' ), // Callback
			'builder-tiles', // Page
			'builder-tiles-responsive' // Section           
		);
	}

	public static function render_option() {
				$fluid_size = Builder_Tiles::get_option( 'fluid_tiles' );
		?>
		<select name="builder_tiles[fluid_tiles]">
			<option value="1" <?php selected( 1, $fluid_size ); ?>><?php _e( 'Enabled', 'builder-tiles' ); ?></option>
			<option value="0" <?php selected( 0, $fluid_size ); ?>><?php _e( 'Disabled', 'builder-tiles' ); ?></option>
		</select>
		<?php
	}

	public static function tiles_size_control() {
		$bar_size = Builder_Tiles::get_option( 'fluid_tiles_base_size' );
		?>
		<select name="builder_tiles[fluid_tiles_base_size]">
			<option value="16" <?php selected( 16, $bar_size ); ?>>16%</option>
			<option value="20" <?php selected( 20, $bar_size ); ?>>20%</option>
			<option value="25" <?php selected( 25, $bar_size ); ?>>25%</option>
			<option value="30" <?php selected( 30, $bar_size ); ?>>30%</option>
		</select>
		<?php
	}

	public static function render_spacing_field() {
		?><input type="text" class="small" value="<?php esc_attr_e( Builder_Tiles::get_option( 'gutter') ) ?>" name="builder_tiles[gutter]">px
		<?php
	}
        
        
	public static function themify_do_metaboxes(array $panels):array{
		$options = array(
			array(
				'name' => 'builder_tiles_fluid_tiles',
				'title' => __('Fluid Tiles', 'builder-tiles'),
				'type' => 'dropdown',
				'meta' => array(
					array('value' => '', 'name' => __('Default', 'builder-tiles'), 'selected' => true),
					array('value' => 'yes', 'name' => __('Enable', 'themify')),
					array('value' => 'no', 'name' => __('Disable', 'themify'))
				)
			),
			array(
				'name' => 'builder_tiles_fluid_tiles_base_size',
				'title' => __('Fluid Tile Base Size', 'builder-tiles'),
				'description' => __('Only works if Fluid Tiles is enabled to control how many tiles will be rendered in a row according to the tile size.', 'builder-tiles'),
				'type' => 'dropdown',
				'meta' => array(
					array('value' => '', 'name' => __('Default', 'builder-tiles'), 'selected' => true),
					array('value' => 16, 'name' => __('16%', 'themify')),
					array('value' => 20, 'name' => __('20%', 'themify')),
					array('value' => 25, 'name' => __('25%', 'themify')),
					array('value' => 30, 'name' => __('30%', 'themify'))
				)
			),
			array(
				'name' => 'builder_tiles_gutter',
				'title' => __('Tile Spacing', 'builder-tiles'),
				'description' => '',
				'type' => 'textbox',
				'meta' => array('size' => 'small'),
				'after' => ' px'
			),
		);
		$panels[] = array(
			'name' => __('Builder Tiles', 'builder-tiles'),
			'id' => 'builder-tiles',
			'options' => $options,
			'pages' => 'page'
		);
		remove_filter( 'themify_do_metaboxes', array(__CLASS__, 'themify_do_metaboxes') );
		return $panels;
	}
}
Builder_Tiles_Admin::init();