<?php
/**
 * @package Structure
 */

function structure_default_theme_settings() {
	return array( // define our defaults
		'hp_top_cat'          => 1,
		'dark_scheme'         => 0,
		'single_feature'      => 0,
		'search_and_navicons' => 0,
		'hide_featured'       => 0,
		'facebook_url'        => '',
		'twitter_url'         => '',
	);
}

/**
 * Register our settings in the database
 */
function register_theme_settings() {
	register_setting( 'structure-options', 'structure-options', 'structure_sanitize_theme_settings' );
}
add_action( 'admin_init', 'register_theme_settings' );

/**
 * Adds the settings page to the Appearance tab
 */
function add_theme_options_menu() {
	add_theme_page( __( 'Theme Options', 'structuretheme' ), __( 'Theme Options', 'structuretheme' ), 'edit_theme_options', 'customize.php' );
}
add_action( 'admin_menu', 'add_theme_options_menu' );

/**
 * Sanitize Theme Options
 */
function structure_sanitize_theme_settings( $in_settings ) {
	$defaults     = structure_default_theme_settings();
	$out_settings = array();

	foreach ( array( 'facebook_url', 'twitter_url', ) as $url ) {
		if ( isset( $in_settings[ $url ] ) )
			$out_settings[ $url ] = esc_url_raw( $in_settings[ $url ] );
		unset( $defaults[ $url ] );
	}

	foreach ( array_keys( $defaults ) as $setting )
		$out_settings[ $setting ] = absint( $in_settings[ $setting ] );

	return $out_settings;
}

/**
 * Implements Structure theme options into Theme Customizer
 *
 * @param $wp_customize Theme Customizer object
 * @return void
 *
 */
function structure_customize_register( $wp_customize ) {
	$wp_customize->get_setting( 'blogname' )->transport        = 'postMessage';
	$wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';

	$defaults = structure_default_theme_settings();

	$wp_customize->add_section( 'structure_options', array(
		'title'    => __( 'Theme Options', 'structuretheme' ),
		'priority' => 50,
	) );

	$wp_customize->add_setting( 'structure-options[hp_top_cat]', array(
		'default'    => $defaults['hp_top_cat'],
		'type'       => 'option',
		'capability' => 'edit_theme_options',
	) );

	$choices = array(
		0 =>  __( 'None: Use the most recent post', 'structuretheme' ),
	);

	foreach( get_terms( 'category', array( 'orderby' => 'id' ) ) as $term )
		$choices[ $term->term_id ] = $term->name;

	$wp_customize->add_control( 'structure_hp_top_cat', array(
		'label'    => __( 'Featured Category', 'structuretheme' ),
		'section'  => 'structure_options',
		'settings' => 'structure-options[hp_top_cat]',
		'type'     => 'select',
		'choices'  => $choices,
		'priority' => 5,
	) );

	$wp_customize->add_setting( 'structure-options[hide_featured]', array(
		'default'    => $defaults['hide_featured'],
		'type'       => 'option',
		'capability' => 'edit_theme_options',
	) );

	$wp_customize->add_control( 'structure_hide_featured', array(
		'label'    => __( 'Front page: Show the most recent featured post only in the featured area (and not in the blog)', 'structuretheme' ),
		'section'  => 'structure_options',
		'settings' => 'structure-options[hide_featured]',
		'type'     => 'checkbox',
		'priority' => 10,
	) );

	$wp_customize->add_setting( 'structure-options[single_feature]', array(
		'default'    => $defaults['single_feature'],
		'type'       => 'option',
		'capability' => 'edit_theme_options',
	) );

	$wp_customize->add_control( 'structure_single_feature', array(
		'label'    => __( 'Show the set Featured Image on single posts', 'structuretheme' ),
		'section'  => 'structure_options',
		'settings' => 'structure-options[single_feature]',
		'type'     => 'checkbox',
		'priority' => 15,
	) );

	$wp_customize->add_setting( 'structure-options[dark_scheme]', array(
		'default'    => $defaults['dark_scheme'],
		'type'       => 'option',
		'capability' => 'edit_theme_options',
		'transport'  => 'postMessage',
	) );

	$wp_customize->add_control( 'structure_dark_scheme', array(
		'label'    => __( 'Use the alternate dark color scheme. You may want to change your header text color in Appearance->Header', 'structuretheme' ),
		'section'  => 'structure_options',
		'settings' => 'structure-options[dark_scheme]',
		'type'     => 'checkbox',
		'priority' => 20,
	) );

	$wp_customize->add_setting( 'structure-options[search_and_navicons]', array(
		'default'    => $defaults['search_and_navicons'],
		'type'       => 'option',
		'capability' => 'edit_theme_options',
		'transport'  => 'postMessage',
	) );

	$wp_customize->add_control( 'structure_search_and_navicons', array(
		'label'    => __( 'Hide the search form and navigation icons in the header', 'structuretheme' ),
		'section'  => 'structure_options',
		'settings' => 'structure-options[search_and_navicons]',
		'type'     => 'checkbox',
		'priority' => 25,
	) );

	$wp_customize->add_setting( 'structure-options[twitter_url]', array(
		'default'    => $defaults['twitter_url'],
		'type'       => 'option',
		'capability' => 'edit_theme_options',
	) );

	$wp_customize->add_control( 'structure_twitter_url', array(
		'label'    => __( 'Twitter URL', 'structuretheme' ),
		'section'  => 'structure_options',
		'settings' => 'structure-options[twitter_url]',
		'type'     => 'text',
		'priority' => 30,
	) );

	$wp_customize->add_setting( 'structure-options[facebook_url]', array(
		'default'    => $defaults['facebook_url'],
		'type'       => 'option',
		'capability' => 'edit_theme_options',
	) );

	$wp_customize->add_control( 'structure_facebook_url', array(
		'label'    => __( 'Facebook URL', 'structuretheme' ),
		'section'  => 'structure_options',
		'settings' => 'structure-options[facebook_url]',
		'type'     => 'text',
		'priority' => 35,
	) );
}
add_action( 'customize_register', 'structure_customize_register' );

/**
 * Bind JS handlers to make Theme Customizer preview reload changes asynchronously
 */
function structure_customize_preview_js() {
	wp_enqueue_script( 'structure-customizer', get_template_directory_uri() . '/js/customizer.js', array( 'customize-preview' ), '20130723', true );
	wp_localize_script( 'structure-customizer', 'Structure', array(
		'dark_scheme_url' => get_template_directory_uri() . '/dark.css',
	) );
}
add_action( 'customize_preview_init', 'structure_customize_preview_js' );

function theme_settings_admin() {}