<?php

if ( ! function_exists( 'peggi_select_map_general_meta' ) ) {
	function peggi_select_map_general_meta() {
		
		$general_meta_box = peggi_select_create_meta_box(
			array(
				'scope' => apply_filters( 'peggi_select_filter_set_scope_for_meta_boxes', array( 'page', 'post' ), 'general_meta' ),
				'title' => esc_html__( 'General', 'peggi' ),
				'name'  => 'general_meta'
			)
		);
		
		/***************** Slider Layout - begin **********************/
		
		peggi_select_create_meta_box_field(
			array(
				'name'        => 'qodef_page_slider_meta',
				'type'        => 'text',
				'label'       => esc_html__( 'Slider Shortcode', 'peggi' ),
				'description' => esc_html__( 'Paste your slider shortcode here', 'peggi' ),
				'parent'      => $general_meta_box
			)
		);
		
		/***************** Slider Layout - begin **********************/
		
		/***************** Content Layout - begin **********************/
		
		peggi_select_create_meta_box_field(
			array(
				'name'          => 'qodef_page_content_behind_header_meta',
				'type'          => 'yesno',
				'default_value' => 'no',
				'label'         => esc_html__( 'Always put content behind header', 'peggi' ),
				'description'   => esc_html__( 'Enabling this option will put page content behind page header', 'peggi' ),
				'parent'        => $general_meta_box
			)
		);
		
		$qodef_content_padding_group = peggi_select_add_admin_group(
			array(
				'name'        => 'content_padding_group',
				'title'       => esc_html__( 'Content Style', 'peggi' ),
				'description' => esc_html__( 'Define styles for Content area', 'peggi' ),
				'parent'      => $general_meta_box
			)
		);
		
			$qodef_content_padding_row = peggi_select_add_admin_row(
				array(
					'name'   => 'qodef_content_padding_row',
					'next'   => true,
					'parent' => $qodef_content_padding_group
				)
			);
		
				peggi_select_create_meta_box_field(
					array(
						'name'   => 'qodef_page_content_padding',
						'type'   => 'textsimple',
						'label'  => esc_html__( 'Content Padding (e.g. 10px 5px 10px 5px)', 'peggi' ),
						'parent' => $qodef_content_padding_row,
					)
				);
				
				peggi_select_create_meta_box_field(
					array(
						'name'    => 'qodef_page_content_padding_mobile',
						'type'    => 'textsimple',
						'label'   => esc_html__( 'Content Padding for mobile (e.g. 10px 5px 10px 5px)', 'peggi' ),
						'parent'  => $qodef_content_padding_row,
					)
				);
		
		peggi_select_create_meta_box_field(
			array(
				'name'        => 'qodef_page_background_color_meta',
				'type'        => 'color',
				'label'       => esc_html__( 'Page Background Color', 'peggi' ),
				'description' => esc_html__( 'Choose background color for page content', 'peggi' ),
				'parent'      => $general_meta_box
			)
		);
		
		peggi_select_create_meta_box_field(
			array(
				'name'          => 'qodef_page_background_image_meta',
				'type'          => 'image',
				'label'         => esc_html__( 'Page Background Image', 'peggi' ),
				'description'   => esc_html__( 'Choose background image for page content', 'peggi' ),
				'parent'        => $general_meta_box
			)
		);
		
		peggi_select_create_meta_box_field(
			array(
				'name'          => 'qodef_page_background_repeat_meta',
				'type'          => 'select',
				'default_value' => '',
				'label'         => esc_html__( 'Page Background Image Repeat', 'peggi' ),
				'description'   => esc_html__( 'Enabling this option will set page background image as pattern in otherwise will be cover background image', 'peggi' ),
				'options'       => peggi_select_get_yes_no_select_array(),
				'parent'        => $general_meta_box
			)
		);
		
		/***************** Content Layout - end **********************/
		
		/***************** Boxed Layout - begin **********************/
		
		peggi_select_create_meta_box_field(
			array(
				'name'    => 'qodef_boxed_meta',
				'type'    => 'select',
				'label'   => esc_html__( 'Boxed Layout', 'peggi' ),
				'parent'  => $general_meta_box,
				'options' => peggi_select_get_yes_no_select_array()
			)
		);
		
			$boxed_container_meta = peggi_select_add_admin_container(
				array(
					'parent'          => $general_meta_box,
					'name'            => 'boxed_container_meta',
					'dependency' => array(
						'hide' => array(
							'qodef_boxed_meta'  => array('','no')
						)
					)
				)
			);
		
				peggi_select_create_meta_box_field(
					array(
						'name'        => 'qodef_page_background_color_in_box_meta',
						'type'        => 'color',
						'label'       => esc_html__( 'Page Background Color', 'peggi' ),
						'description' => esc_html__( 'Choose the page background color outside box', 'peggi' ),
						'parent'      => $boxed_container_meta
					)
				);
				
				peggi_select_create_meta_box_field(
					array(
						'name'        => 'qodef_boxed_background_image_meta',
						'type'        => 'image',
						'label'       => esc_html__( 'Background Image', 'peggi' ),
						'description' => esc_html__( 'Choose an image to be displayed in background', 'peggi' ),
						'parent'      => $boxed_container_meta
					)
				);
				
				peggi_select_create_meta_box_field(
					array(
						'name'        => 'qodef_boxed_pattern_background_image_meta',
						'type'        => 'image',
						'label'       => esc_html__( 'Background Pattern', 'peggi' ),
						'description' => esc_html__( 'Choose an image to be used as background pattern', 'peggi' ),
						'parent'      => $boxed_container_meta
					)
				);
				
				peggi_select_create_meta_box_field(
					array(
						'name'          => 'qodef_boxed_background_image_attachment_meta',
						'type'          => 'select',
						'default_value' => '',
						'label'         => esc_html__( 'Background Image Attachment', 'peggi' ),
						'description'   => esc_html__( 'Choose background image attachment', 'peggi' ),
						'parent'        => $boxed_container_meta,
						'options'       => array(
							''       => esc_html__( 'Default', 'peggi' ),
							'fixed'  => esc_html__( 'Fixed', 'peggi' ),
							'scroll' => esc_html__( 'Scroll', 'peggi' )
						)
					)
				);
		
		/***************** Boxed Layout - end **********************/
		
		/***************** Passepartout Layout - begin **********************/
		
		peggi_select_create_meta_box_field(
			array(
				'name'          => 'qodef_paspartu_meta',
				'type'          => 'select',
				'default_value' => '',
				'label'         => esc_html__( 'Passepartout', 'peggi' ),
				'description'   => esc_html__( 'Enabling this option will display passepartout around site content', 'peggi' ),
				'parent'        => $general_meta_box,
				'options'       => peggi_select_get_yes_no_select_array(),
			)
		);
		
			$paspartu_container_meta = peggi_select_add_admin_container(
				array(
					'parent'          => $general_meta_box,
					'name'            => 'qodef_paspartu_container_meta',
					'dependency' => array(
						'hide' => array(
							'qodef_paspartu_meta'  => array('','no')
						)
					)
				)
			);
		
				peggi_select_create_meta_box_field(
					array(
						'name'        => 'qodef_paspartu_color_meta',
						'type'        => 'color',
						'label'       => esc_html__( 'Passepartout Color', 'peggi' ),
						'description' => esc_html__( 'Choose passepartout color, default value is #ffffff', 'peggi' ),
						'parent'      => $paspartu_container_meta
					)
				);
				
				peggi_select_create_meta_box_field(
					array(
						'name'        => 'qodef_paspartu_width_meta',
						'type'        => 'text',
						'label'       => esc_html__( 'Passepartout Size', 'peggi' ),
						'description' => esc_html__( 'Enter size amount for passepartout', 'peggi' ),
						'parent'      => $paspartu_container_meta,
						'args'        => array(
							'col_width' => 2,
							'suffix'    => 'px or %'
						)
					)
				);
		
				peggi_select_create_meta_box_field(
					array(
						'name'        => 'qodef_paspartu_responsive_width_meta',
						'type'        => 'text',
						'label'       => esc_html__( 'Responsive Passepartout Size', 'peggi' ),
						'description' => esc_html__( 'Enter size amount for passepartout for smaller screens (tablets and mobiles view)', 'peggi' ),
						'parent'      => $paspartu_container_meta,
						'args'        => array(
							'col_width' => 2,
							'suffix'    => 'px or %'
						)
					)
				);
				
				peggi_select_create_meta_box_field(
					array(
						'parent'        => $paspartu_container_meta,
						'type'          => 'select',
						'default_value' => '',
						'name'          => 'qodef_disable_top_paspartu_meta',
						'label'         => esc_html__( 'Disable Top Passepartout', 'peggi' ),
						'options'       => peggi_select_get_yes_no_select_array(),
					)
				);
		
				peggi_select_create_meta_box_field(
					array(
						'parent'        => $paspartu_container_meta,
						'type'          => 'select',
						'default_value' => '',
						'name'          => 'qodef_enable_fixed_paspartu_meta',
						'label'         => esc_html__( 'Enable Fixed Passepartout', 'peggi' ),
						'description'   => esc_html__( 'Enabling this option will set fixed passepartout for your screens', 'peggi' ),
						'options'       => peggi_select_get_yes_no_select_array(),
					)
				);
		
		/***************** Passepartout Layout - end **********************/
		
		/***************** Content Width Layout - begin **********************/
		
		peggi_select_create_meta_box_field(
			array(
				'name'          => 'qodef_initial_content_width_meta',
				'type'          => 'select',
				'default_value' => '',
				'label'         => esc_html__( 'Initial Width of Content', 'peggi' ),
				'description'   => esc_html__( 'Choose the initial width of content which is in grid (Applies to pages set to "Default Template" and rows set to "In Grid")', 'peggi' ),
				'parent'        => $general_meta_box,
				'options'       => array(
					''                => esc_html__( 'Default', 'peggi' ),
					'qodef-grid-1100' => esc_html__( '1100px', 'peggi' ),
					'qodef-grid-1300' => esc_html__( '1300px', 'peggi' ),
					'qodef-grid-1200' => esc_html__( '1200px', 'peggi' ),
					'qodef-grid-1000' => esc_html__( '1000px', 'peggi' ),
					'qodef-grid-800'  => esc_html__( '800px', 'peggi' )
				)
			)
		);
		
		/***************** Content Width Layout - end **********************/
		
		/***************** Smooth Page Transitions Layout - begin **********************/
		
		peggi_select_create_meta_box_field(
			array(
				'name'          => 'qodef_smooth_page_transitions_meta',
				'type'          => 'select',
				'default_value' => '',
				'label'         => esc_html__( 'Smooth Page Transitions', 'peggi' ),
				'description'   => esc_html__( 'Enabling this option will perform a smooth transition between pages when clicking on links', 'peggi' ),
				'parent'        => $general_meta_box,
				'options'       => peggi_select_get_yes_no_select_array()
			)
		);
		
			$page_transitions_container_meta = peggi_select_add_admin_container(
				array(
					'parent'     => $general_meta_box,
					'name'       => 'page_transitions_container_meta',
					'dependency' => array(
						'hide' => array(
							'qodef_smooth_page_transitions_meta' => array( '', 'no' )
						)
					)
				)
			);
		
				peggi_select_create_meta_box_field(
					array(
						'name'        => 'qodef_page_transition_preloader_meta',
						'type'        => 'select',
						'label'       => esc_html__( 'Enable Preloading Animation', 'peggi' ),
						'description' => esc_html__( 'Enabling this option will display an animated preloader while the page content is loading', 'peggi' ),
						'parent'      => $page_transitions_container_meta,
						'options'     => peggi_select_get_yes_no_select_array()
					)
				);
		
				$page_transition_preloader_container_meta = peggi_select_add_admin_container(
					array(
						'parent'     => $page_transitions_container_meta,
						'name'       => 'page_transition_preloader_container_meta',
						'dependency' => array(
							'hide' => array(
								'qodef_page_transition_preloader_meta' => array( '', 'no' )
							)
						)
					)
				);
				
					peggi_select_create_meta_box_field(
						array(
							'name'   => 'qodef_smooth_pt_bgnd_color_meta',
							'type'   => 'color',
							'label'  => esc_html__( 'Page Loader Background Color', 'peggi' ),
							'parent' => $page_transition_preloader_container_meta
						)
					);
					
					$group_pt_spinner_animation_meta = peggi_select_add_admin_group(
						array(
							'name'        => 'group_pt_spinner_animation_meta',
							'title'       => esc_html__( 'Loader Style', 'peggi' ),
							'description' => esc_html__( 'Define styles for loader spinner animation', 'peggi' ),
							'parent'      => $page_transition_preloader_container_meta
						)
					);
					
					$row_pt_spinner_animation_meta = peggi_select_add_admin_row(
						array(
							'name'   => 'row_pt_spinner_animation_meta',
							'parent' => $group_pt_spinner_animation_meta
						)
					);
					
					peggi_select_create_meta_box_field(
						array(
							'type'    => 'selectsimple',
							'name'    => 'qodef_smooth_pt_spinner_type_meta',
							'label'   => esc_html__( 'Spinner Type', 'peggi' ),
							'parent'  => $row_pt_spinner_animation_meta,
							'options' => array(
								''                      => esc_html__( 'Default', 'peggi' ),
								'peggi_loader'        	=> esc_html__( 'Peggi Loader', 'peggi' ),
								'rotate_circles'        => esc_html__( 'Rotate Circles', 'peggi' ),
								'pulse'                 => esc_html__( 'Pulse', 'peggi' ),
								'double_pulse'          => esc_html__( 'Double Pulse', 'peggi' ),
								'cube'                  => esc_html__( 'Cube', 'peggi' ),
								'rotating_cubes'        => esc_html__( 'Rotating Cubes', 'peggi' ),
								'stripes'               => esc_html__( 'Stripes', 'peggi' ),
								'wave'                  => esc_html__( 'Wave', 'peggi' ),
								'two_rotating_circles'  => esc_html__( '2 Rotating Circles', 'peggi' ),
								'five_rotating_circles' => esc_html__( '5 Rotating Circles', 'peggi' ),
								'atom'                  => esc_html__( 'Atom', 'peggi' ),
								'clock'                 => esc_html__( 'Clock', 'peggi' ),
								'mitosis'               => esc_html__( 'Mitosis', 'peggi' ),
								'lines'                 => esc_html__( 'Lines', 'peggi' ),
								'fussion'               => esc_html__( 'Fussion', 'peggi' ),
								'wave_circles'          => esc_html__( 'Wave Circles', 'peggi' ),
								'pulse_circles'         => esc_html__( 'Pulse Circles', 'peggi' )
							)
						)
					);
					
					peggi_select_create_meta_box_field(
						array(
							'type'   => 'colorsimple',
							'name'   => 'qodef_smooth_pt_spinner_color_meta',
							'label'  => esc_html__( 'Spinner Color', 'peggi' ),
							'parent' => $row_pt_spinner_animation_meta
						)
					);
					
					peggi_select_create_meta_box_field(
						array(
							'name'        => 'qodef_page_transition_fadeout_meta',
							'type'        => 'select',
							'label'       => esc_html__( 'Enable Fade Out Animation', 'peggi' ),
							'description' => esc_html__( 'Enabling this option will turn on fade out animation when leaving page', 'peggi' ),
							'options'     => peggi_select_get_yes_no_select_array(),
							'parent'      => $page_transitions_container_meta
						
						)
					);
		
		/***************** Smooth Page Transitions Layout - end **********************/
		
		/***************** Comments Layout - begin **********************/
		
		peggi_select_create_meta_box_field(
			array(
				'name'        => 'qodef_page_comments_meta',
				'type'        => 'select',
				'label'       => esc_html__( 'Show Comments', 'peggi' ),
				'description' => esc_html__( 'Enabling this option will show comments on your page', 'peggi' ),
				'parent'      => $general_meta_box,
				'options'     => peggi_select_get_yes_no_select_array()
			)
		);
		
		/***************** Comments Layout - end **********************/
	}
	
	add_action( 'peggi_select_action_meta_boxes_map', 'peggi_select_map_general_meta', 10 );
}

if ( ! function_exists( 'peggi_select_container_background_style' ) ) {
	/**
	 * Function that return container style
	 */
	function peggi_select_container_background_style( $style ) {
		$page_id      = peggi_select_get_page_id();
		$class_prefix = peggi_select_get_unique_page_class( $page_id, true );
		
		$container_selector = array(
			$class_prefix . ' .qodef-content'
		);
		
		$container_class        = array();
		$page_background_color  = get_post_meta( $page_id, 'qodef_page_background_color_meta', true );
		$page_background_image  = get_post_meta( $page_id, 'qodef_page_background_image_meta', true );
		$page_background_repeat = get_post_meta( $page_id, 'qodef_page_background_repeat_meta', true );
		
		if ( ! empty( $page_background_color ) ) {
			$container_class['background-color'] = $page_background_color;
		}
		
		if ( ! empty( $page_background_image ) ) {
			$container_class['background-image'] = 'url(' . esc_url( $page_background_image ) . ')';
			
			if ( $page_background_repeat === 'yes' ) {
				$container_class['background-repeat']   = 'repeat';
				$container_class['background-position'] = '0 0';
			} else {
				$container_class['background-repeat']   = 'no-repeat';
				$container_class['background-position'] = 'center 0';
				$container_class['background-size']     = 'cover';
			}
		}
		
		$current_style = peggi_select_dynamic_css( $container_selector, $container_class );
		$current_style = $current_style . $style;
		
		return $current_style;
	}
	
	add_filter( 'peggi_select_filter_add_page_custom_style', 'peggi_select_container_background_style' );
}