<?php 
class Code_Manas_Kirki_Fields {

	private static $_instance = null;

	public static function instance() {
		if ( self::$_instance == null ) {
	    	self::$_instance = new Code_Manas_Kirki_Fields();
	    }
	 
	    return self::$_instance;
	}

	public function __construct() {
		$this->kirki_config();
		$this->add_panels();
		$this->add_sections();
		$this->add_fields();
		add_action( 'customize_controls_print_styles', array( $this, 'control_styles' ) );
	}


	/**
	 * Add style for controls which is loaded only on Customizer preview.
	 * @return [type] [description]
	 */
	function control_styles() { ?>
		<style>
			.customize-control-kirki-radio-image label {
				margin-right: 10px;
			}
			.customize-control-kirki-radio-image input:checked + label img {
				border: 3px solid #3498DB;
			}
		</style>
		<?php
	}

	function kirki_config() {
		// Add Kirki config for this theme
		Kirki::add_config( 'code_manas_config', array(
			'capability'    => 'edit_theme_options',
			'option_type'   => 'theme_mod',
		) );
	}

	function add_panels() {
		// Add header panel
		Kirki::add_panel( 'code_manas_header', array(
		    'priority'    => 10,
		    'title'       => esc_html__( 'Header', 'code-manas' ),
		    'description' => esc_html__( 'Manage header section.', 'code-manas' ),
		) );

		// Add advance options panel
		Kirki::add_panel( 'code_manas_advance_options', array(
		    'priority'    => 30,
		    'title'       => esc_html__( 'Advanced options', 'code-manas' ),
		) );
	}

	function sections_data() {
		$sections = array(
			// Add header options section
			'code_manas_header_section' => array(
			    'title'          => esc_html__( 'Header options', 'code-manas' ),
			    'panel'          => 'code_manas_header',
			    'priority'		 => 30
			),
			// Add featured section
			'code_manas_featured_section' => array(
			    'title'          => esc_html__( 'Featured section', 'code-manas' ),
			    'description'    => esc_html__( 'Featured section is the top section which appears at top in everywhere as managed.', 'code-manas' ),
			    'panel'          => 'code_manas_header',
			    'priority'		 => 40
			),
			// Add layout section
			'code_manas_layout_option_section' => array(
			    'title'          => esc_html__( 'Layout options', 'code-manas' ),
			    'description'    => esc_html__( 'Layout options for site.', 'code-manas' ),
			    'panel'          => 'code_manas_advance_options',
			),
			// Add advance menu section
			'code_manas_menu_option_section' => array(
			    'title'          => esc_html__( 'Menu options', 'code-manas' ),
			    'description'    => esc_html__( 'Advanced options for menus.', 'code-manas' ),
			    'panel'          => 'code_manas_advance_options',
			),
			// Add advance blog options
			'code_manas_blog_option_section' => array(
			    'title'          => esc_html__( 'Blog/Archive options', 'code-manas' ),
			    'description'    => esc_html__( 'Advanced options for blog/archive.', 'code-manas' ),
			    'panel'          => 'code_manas_advance_options',
			),
			// Add advance posts options
			'code_manas_post_option_section' => array(
			    'title'          => esc_html__( 'Posts options', 'code-manas' ),
			    'description'    => esc_html__( 'Note: This option will effect all the single posts.', 'code-manas' ),
			    'panel'          => 'code_manas_advance_options',
			),
			// Add advance pages options
			'code_manas_page_option_section' => array(
			    'title'          => esc_html__( 'Pages options', 'code-manas' ),
			    'description'    => esc_html__( 'Note: This option will effect all the single pages.', 'code-manas' ),
			    'panel'          => 'code_manas_advance_options',
			),
			// Add footer section
			'code_manas_footer_section' => array(
			    'title'          => esc_html__( 'Footer section', 'code-manas' ),
			    'priority'		 => 200
			),
			// Add breadcrumb section
			'code_manas_woo_breadcrumb_section' => array(
			    'title'          => esc_html__( 'Breadcrumb', 'code-manas' ),
			    'priority'		 => 60,
			    'panel'		 => 'woocommerce',
			)
		);

		return $sections;
	}

	function add_sections() {
		$sections = $this->sections_data();
		if( ! empty( $sections ) ) {
			foreach ( $sections as $key => $section ) {
				// Add primary menu sticky option
				Kirki::add_section( $key, $section );
			}
		}
	}

	/**
	 * Collects all the default theme mods value in an array
	 * @return array All the default theme mods values
	 */
	function get_theme_mods_defaults() {
	    $defaults = array(
			'featured_title' => esc_html__( 'Readers live longer', 'code-manas' ),
			'featured_content' => esc_html__( 'We\'re more than happy to do this stuff. So please welcome to our theme. Enjoy!', 'code-manas' ),
			'featured_bg' => 'color',
			'featured_bg_color' => '#f5f0ed',
			'featured_enable_on' => 'homepage',

			'blog_excerpt_more' => esc_html__( '...(Continue Reading)', 'code-manas' ),
			'footer_copyright' => __( 'Copyright &#169; All rights reserved', 'code-manas' ),
		);
		return apply_filters( 'code_manas_get_theme_mods_defaults', $defaults );
	}

	function fields_data() {
		// Get all default values
		$defaults = $this->get_theme_mods_defaults();

		$featured_enable_active_cb = array(
			'setting'  => 'featured_enable',
			'operator' => '===',
			'value'    => true,
		);

		$fields = array( 
			// Add header alignment toggle option.
			array(
				'type'        => 'toggle',
				'settings'    => 'header_alignment',
				'label'       => esc_html__( 'Center align header:', 'code-manas' ),
				'description'       => esc_html__( 'Enabling it will align the header and secondary menu to center.', 'code-manas' ),
				'section'     => 'code_manas_header_section',
				'default'     => 0,
			),  
			// Add primary menu sticky option
			array(
				'type'        => 'toggle',
				'settings'    => 'primary_menu_sticky',
				'label'       => esc_html__( 'Make header sticky:', 'code-manas' ),
				'description'       => esc_html__( 'Enabling it will make the header stick to the top.', 'code-manas' ),
				'section'     => 'code_manas_header_section',
				'default'     => 1,
			),
			// Add header background color option
			array(
				'type'        => 'color',
				'settings'    => 'header_bg_color',
    			'transport' => 'auto',
				'label'       => esc_html__( 'Header background color:', 'code-manas' ),
				'section'     => 'code_manas_header_section',
				'default'     => '#fff',
				'output' => array(
				    array(
				        'element'  => 'header#masthead',
				        'property' => 'background-color',
				    ),
				),
			),
			// Add featured section enable/disable option.
			array(
				'type'        => 'switch',
				'settings'    => 'featured_enable',
				'label'       => esc_html__( 'Featured section', 'code-manas' ),
				'section'     => 'code_manas_featured_section',
				'default'     => 0,
				'choices'     => array(
					'on'  => esc_html__( 'Enable', 'code-manas' ),
					'off' => esc_html__( 'Disable', 'code-manas' ),
				),
			),
			// Featured section entire site enable/disable option
			array(
				'type'        => 'radio',
				'settings'    => 'featured_enable_on',
				'label'       => esc_html__( 'Enable on', 'code-manas' ),
				'section'     => 'code_manas_featured_section',
				'default'     => $defaults['featured_enable_on'],
				'active_callback'    => array( $featured_enable_active_cb ),
				'choices'     => array(
					'homepage'   => array(
						esc_html__( 'Frontpage', 'code-manas' ),
						esc_html__( 'Will be enabled only on homepage.', 'code-manas' ),
					),
					'entire-site' => array(
						esc_html__( 'Entire site', 'code-manas' ),
						esc_html__( 'Will be enabled on the whole site.', 'code-manas' ),
					),
				),
			),
			// Add featured title
			array(
				'type'        => 'text',
				'settings'    => 'featured_title',
				'label'       => esc_html__( 'Title', 'code-manas' ),
				'section'     => 'code_manas_featured_section',
				'transport'	  => 'postMessage',
				'default'     => $defaults['featured_title'],
				'active_callback'    => array( $featured_enable_active_cb ),
				'partial_refresh' => array(
					'featured_title' => array(
						'selector'        => '.featured-text-content h1',
						'render_callback' => function() {
							return get_theme_mod( 'featured_title' );
						},
					),
				),
			),
			// Add featured content
			array(
				'type'     => 'textarea',
				'settings' => 'featured_content',
				'label'    => esc_html__( 'Content', 'code-manas' ),
				'description'  => esc_html__( 'HTML allowed', 'code-manas' ),
				'section'  => 'code_manas_featured_section',
				'default'     => $defaults['featured_content'],
				'active_callback'    => array( $featured_enable_active_cb ),
				'partial_refresh' => array(
					'featured_content' => array(
						'selector'        => '.featured-text-content h2.theme-tagline',
						'render_callback' => function() {
							return get_theme_mod( 'featured_content' );
						},
					),
				),
			),
			// Add featured background type option
			array(
				'type'     => 'radio-buttonset',
				'settings' => 'featured_bg',
				'label'    => esc_html__( 'Background', 'code-manas' ),
				'section'  => 'code_manas_featured_section',
				'active_callback'    => array( $featured_enable_active_cb ),
				'default'     => $defaults['featured_bg'],
				'choices'     => array(
					'image' => esc_html__( 'Image', 'code-manas' ),
					'color' => esc_html__( 'Color', 'code-manas' ),
				),
			),
			// Add featured background color
			array(
				'type'        => 'color',
				'settings'    => 'featured_bg_color',
				'label'       => esc_html__( 'Background color', 'code-manas' ),
				'section'     => 'code_manas_featured_section',
				'default'     => $defaults['featured_bg_color'],
				'active_callback'    => array(
			    	array(
			    		'setting'  => 'featured_bg',
			    		'operator' => '===',
			    		'value'    => 'color',
			    	),
			    	$featured_enable_active_cb
			    ),
			),
			// Add site layout option
			array(
				'type'        => 'select',
				'settings'    => 'site_layout',
				'label'       => esc_html__( 'Container width', 'code-manas' ),
				'section'     => 'code_manas_layout_option_section',
				'default'     => 'boxed',
				'choices'     => array(
					'boxed'   => esc_html__( 'Boxed', 'code-manas' ),
					'wide' => esc_html__( 'Full width', 'code-manas' ),
				),
			),
			// Add search option for primary menu
			array(
				'type'        => 'toggle',
				'settings'    => 'primary_menu_search_enable',
				'label'       => esc_html__( 'Enable search on Primary menu.', 'code-manas' ),
				'section'     => 'code_manas_menu_option_section',
				'default'     => 1,
			),
			// Add search option for primary menu
			array(
				'type'        => 'toggle',
				'settings'    => 'secondary_menu_search_enable',
				'label'       => esc_html__( 'Enable search on Secondary menu.', 'code-manas' ),
				'section'     => 'code_manas_menu_option_section',
				'default'     => 1,
			),
			// Add date enable option
			array(
				'type'        => 'toggle',
				'settings'    => 'blog_date_enable',
				'label'       => esc_html__( 'Enable date:', 'code-manas' ),
				'section'     => 'code_manas_blog_option_section',
				'default'     => 1,
			),
			// Add author enable option
			array(
				'type'        => 'toggle',
				'settings'    => 'blog_author_enable',
				'label'       => esc_html__( 'Enable author:', 'code-manas' ),
				'section'     => 'code_manas_blog_option_section',
				'default'     => 1,
			),
			// Add tag enable option
			array(
				'type'        => 'toggle',
				'settings'    => 'blog_tag_enable',
				'label'       => esc_html__( 'Enable tag:', 'code-manas' ),
				'section'     => 'code_manas_blog_option_section',
				'default'     => 1,
			),
			// Add category enable option
			array(
				'type'        => 'toggle',
				'settings'    => 'blog_cat_enable',
				'label'       => esc_html__( 'Enable category:', 'code-manas' ),
				'section'     => 'code_manas_blog_option_section',
				'default'     => 1,
			),
			// Add comment enable option
			array(
				'type'        => 'toggle',
				'settings'    => 'blog_comment_enable',
				'label'       => esc_html__( 'Enable comment:', 'code-manas' ),
				'section'     => 'code_manas_blog_option_section',
				'default'     => 1,
			),
			// Add image enable option
			array(
				'type'        => 'toggle',
				'settings'    => 'blog_image_enable',
				'label'       => esc_html__( 'Enable image:', 'code-manas' ),
				'section'     => 'code_manas_blog_option_section',
				'default'     => 1,
			),
			// Add blog content type option
			array(
				'type'        => 'radio-buttonset',
				'settings'    => 'blog_content',
				'label'       => esc_html__( 'Blog content:', 'code-manas' ),
				'section'     => 'code_manas_blog_option_section',
				'default'     => 'excerpt',
				'choices'     => array(
					'excerpt'   => esc_html__( 'Excerpt', 'code-manas' ),
					'full' => esc_html__( 'Full content', 'code-manas' ),
				),
			),
			// Add blog excerpt more text
			array(
				'type'        => 'text',
				'settings'    => 'blog_excerpt_more',
				'label'       => esc_html__( 'Read more text:', 'code-manas' ),
				'section'     => 'code_manas_blog_option_section',
				'default'     => $defaults['blog_excerpt_more'],
			),
			// Add blog pagination type option
			array(
				'type'        => 'radio-buttonset',
				'settings'    => 'blog_pagination',
				'label'       => esc_html__( 'Pagination:', 'code-manas' ),
				'section'     => 'code_manas_blog_option_section',
				'default'     => 'numeric',
				'choices'     => array(
					'numeric'   => esc_html__( 'Numeric', 'code-manas' ),
					'wording' => esc_html__( 'Older/Newer', 'code-manas' ),
				),
			),
			// Add archive/blog sidebar layout option
			array(
				'type'        => 'radio-image',
				'settings'    => 'archive_layout',
				'label'       => esc_html__( 'Archive/blog sidebar', 'code-manas' ),
				'description'       => esc_html__( 'This changes the sidebar position on archive, search and blog post page.', 'code-manas' ),
				'section'     => 'code_manas_blog_option_section',
				'default'     => 'rs',
				'choices'     => array(
					'ls'   => get_theme_file_uri( '/assets/img/ls.png' ),
					'ns' => get_theme_file_uri( '/assets/img/ns.png' ),
					'rs'  => get_theme_file_uri( '/assets/img/rs.png' ),
				),
			),
			// Add date enable option
			array(
				'type'        => 'toggle',
				'settings'    => 'post_date_enable',
				'label'       => esc_html__( 'Enable date:', 'code-manas' ),
				'section'     => 'code_manas_post_option_section',
				'default'     => 1,
			),
			// Add author enable option
			array(
				'type'        => 'toggle',
				'settings'    => 'post_author_enable',
				'label'       => esc_html__( 'Enable author:', 'code-manas' ),
				'section'     => 'code_manas_post_option_section',
				'default'     => 1,
			),
			// Add tag enable option
			array(
				'type'        => 'toggle',
				'settings'    => 'post_tag_enable',
				'label'       => esc_html__( 'Enable tag:', 'code-manas' ),
				'section'     => 'code_manas_post_option_section',
				'default'     => 1,
			),
			// Add category enable option
			array(
				'type'        => 'toggle',
				'settings'    => 'post_cat_enable',
				'label'       => esc_html__( 'Enable category:', 'code-manas' ),
				'section'     => 'code_manas_post_option_section',
				'default'     => 1,
			),
			// Add comment enable option
			array(
				'type'        => 'toggle',
				'settings'    => 'post_comment_enable',
				'label'       => esc_html__( 'Enable comment:', 'code-manas' ),
				'section'     => 'code_manas_post_option_section',
				'default'     => 1,
			),
			// Add image enable option
			array(
				'type'        => 'toggle',
				'settings'    => 'post_image_enable',
				'label'       => esc_html__( 'Enable image:', 'code-manas' ),
				'section'     => 'code_manas_post_option_section',
				'default'     => 1,
			),
			// Add post pagination type option
			array(
				'type'        => 'radio-buttonset',
				'settings'    => 'post_pagination',
				'label'       => esc_html__( 'Pagination:', 'code-manas' ),
				'section'     => 'code_manas_post_option_section',
				'default'     => 'enable',
				'choices'     => array(
					'enable'   => esc_html__( 'Enable', 'code-manas' ),
					'disable' => esc_html__( 'Disable', 'code-manas' ),
				),
			),
			// Add single posts sidebar layout option
			array(
				'type'        => 'radio-image',
				'settings'    => 'posts_layout',
				'label'       => esc_html__( 'Posts sidebar', 'code-manas' ),
				'description'       => esc_html__( 'This changes the sidebar position on all single posts.', 'code-manas' ),
				'section'     => 'code_manas_post_option_section',
				'default'     => 'rs',
				'choices'     => array(
					'ls'   => get_theme_file_uri( '/assets/img/ls.png' ),
					'ns' => get_theme_file_uri( '/assets/img/ns.png' ),
					'rs'  => get_theme_file_uri( '/assets/img/rs.png' ),
				),
			),
			// Add image enable option
			array(
				'type'        => 'toggle',
				'settings'    => 'page_image_enable',
				'label'       => esc_html__( 'Enable image:', 'code-manas' ),
				'section'     => 'code_manas_page_option_section',
				'default'     => 1,
			),
			// Add single pages sidebar layout option
			array(
				'type'        => 'radio-image',
				'settings'    => 'pages_layout',
				'label'       => esc_html__( 'Pages sidebar', 'code-manas' ),
				'description'       => esc_html__( 'This changes the sidebar position on all single pages.', 'code-manas' ),
				'section'     => 'code_manas_page_option_section',
				'default'     => 'rs',
				'choices'     => array(
					'ls'   => get_theme_file_uri( '/assets/img/ls.png' ),
					'ns' => get_theme_file_uri( '/assets/img/ns.png' ),
					'rs'  => get_theme_file_uri( '/assets/img/rs.png' ),
				),
			),
			// Add copyright content
			array(
				'type'     => 'textarea',
				'settings' => 'footer_copyright',
				'label'    => esc_html__( 'Footer content:', 'code-manas' ),
				'description'  => esc_html__( 'HTML allowed', 'code-manas' ),
				'section'  => 'code_manas_footer_section',
				'default'     => $defaults['footer_copyright'],
				'partial_refresh' => array(
					'footer_copyright' => array(
						'selector'        => 'footer#colophon .copyright',
						'render_callback' => function() {
							return get_theme_mod( 'footer_copyright' );
						},
					),
				),
			),
			// Add scroll up enable option
			array(
				'type'        => 'toggle',
				'settings'    => 'scroll_up_enable',
				'label'       => esc_html__( 'Enable scroll up:', 'code-manas' ),
				'description'       => esc_html__( 'This option toggles the "Scroll to top" arrow.', 'code-manas' ),
				'section'     => 'code_manas_footer_section',
				'default'     => 1,
			),
			array(
				'type'        => 'switch',
				'settings'    => 'woo_breadcrumb_enable',
				'label'       => esc_html__( 'Breadcrumb', 'code-manas' ),
				'section'     => 'code_manas_woo_breadcrumb_section',
				'default'     => 1,
				'choices'     => array(
					'on'  => esc_html__( 'Enable', 'code-manas' ),
					'off' => esc_html__( 'Disable', 'code-manas' ),
				),
			),
		);	
		return $fields;
	}

	function add_fields() {
		$fields = $this->fields_data();
		if( ! empty( $fields ) ) {
			foreach ( $fields as $field ) {
				// Add primary menu sticky option
				Kirki::add_field( 'code_manas_config', $field );
			}
		}
	}
}

function cm_kirki_fields() {
	return Code_Manas_Kirki_Fields::instance();
}

cm_kirki_fields();

