<?php
include_once ABSPATH . 'wp-includes/class-wp-customize-control.php';

class Color_Picker_Custom_control extends WP_Customize_Control {

  public function render_content() {

    if ( empty( $this->choices ) ) return;
    $name = '_customize-radio-' . $this->id; ?>
    <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
    <table style="margin-top: 10px; text-align: center; width: 100%;">
      <tr>
        <?php foreach ( $this->choices as $value => $label ) {
                $checked = '';
                if($value == 0) $checked = 'checked'; ?>
                <td>
                  <label>
                    <div style="width: 30px; height: 30px; margin: 0 auto; background: <?php echo esc_attr( $label )?> "></div><br />
                    <?php if($value == 0) $label = '' ?>
                    <input type="radio" value="<?php echo esc_attr( $label ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $this->value(), $value ); echo $checked ?> />
                  </label>
                </td>
          <?php } ?>
      </tr>
    </table><?php

  }
}

class DW_Gamez_Textarea_Custom_Control extends WP_Customize_Control {

	public $type = 'textarea';

	public $statuses;

	public function __construct( $manager, $id, $args = array() ) {

	$this->statuses = array( '' => __( 'Default', 'dw-gamez' ) );
		parent::__construct( $manager, $id, $args );
	}
	public function render_content() {
		?>
		<label>
			<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
			<textarea class="large-text" cols="20" rows="5" <?php $this->link(); ?>>
				<?php echo esc_textarea( $this->value() ); ?>
			</textarea>
			<?php echo $this->desc; ?>
		</label>
		<?php
	}
}

class Layout_Picker_Custom_control extends WP_Customize_Control {

	public function render_content() {

	if ( empty( $this->choices ) ) return;

	$name = '_customize-radio-' . $this->id;

	?>
	<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
	<table style="margin-top: 10px; text-align: center; width: 100%;">
		<tr>
		<?php foreach ( $this->choices as $value => $label ) : ?>
		<td>
			<label>
				<img src="<?php echo get_template_directory_uri(); ?>/inc/img/layout-<?php echo esc_attr( $value ); ?>.png" alt="<?php echo esc_attr( $value ); ?>" /><br />
				<input type="radio" value="<?php echo esc_attr( $value ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?> />
			</label>
		</td>
		<?php endforeach; ?>
		</tr>
	</table>
		<?php
	}
}

function dw_gamez_customize_register( $wp_customize ) {
	// SITE LAYOUT --------------------------------------------------------------------------------------
	$wp_customize->add_section('dw_gamez_layout', array(
		'title'    => __('طرح بندی', 'dw-gamez'),
		'priority' => 10,
	));
	$wp_customize->add_setting('dw_gamez_theme_options[layout]', array(
		'capability' => 'edit_theme_options',
		'type' => 'option'
	));
	$wp_customize->add_control( new Layout_Picker_Custom_control($wp_customize, 'layout', array(
		'label' => __('طرح بندی سایت', 'dw-gamez'),
		'section' => 'dw_gamez_layout',
		'settings' => 'dw_gamez_theme_options[layout]',
		'choices' => array('wide', 'narrow')
	)));

	// SITE TITLE & TAGLINE --------------------------------------------------------------------------------------
	$wp_customize->add_setting('dw_gamez_theme_options[favicon]', array(
		'default' => get_template_directory_uri().'/assets/img/favicon.ico',
		'capability' => 'edit_theme_options',
		'type' => 'option',
	));
	$wp_customize->add_control( new WP_Customize_Image_Control($wp_customize, 'favicon', array(
		'label' => __('فیوآیکون سایت', 'dw-gamez'),
		'section' => 'title_tagline',
		'settings' => 'dw_gamez_theme_options[favicon]',
	)));
	$wp_customize->add_setting('dw_gamez_theme_options[logo]', array(
		'default' => get_template_directory_uri().'/assets/img/logo.png',
		'capability' => 'edit_theme_options',
		'type' => 'option',
	));
	$wp_customize->add_control( new WP_Customize_Image_Control($wp_customize, 'logo', array(
		'label' => __('لوگوی سایت', 'dw-gamez'),
		'section' => 'title_tagline',
		'settings' => 'dw_gamez_theme_options[logo]',
	)));
	$wp_customize->add_setting('dw_gamez_theme_options[header_display]', array(
		'default'        => 'site_title',
		'capability'     => 'edit_theme_options',
		'type'           => 'option',
	));
	$wp_customize->add_control( 'header_display', array(
		'settings' => 'dw_gamez_theme_options[header_display]',
		'label'   => 'نمایش از',
		'section' => 'title_tagline',
		'type'    => 'select',
		'choices'    => array(
			'site_title' => 'Site Title',
			'site_logo' => 'Site Logo',
		),
	));

	// SOCIAL LINKS --------------------------------------------------------------------------------------
	$wp_customize->add_section('dw_gamez_social_links', array(
		'title'    => __('لینک شبکه اجتماعی', 'dw-gamez'),
		'priority' => 109,
	));
	$wp_customize->add_setting('dw_gamez_theme_options[facebook]', array(
		'default'        => '',
		'capability'     => 'edit_theme_options',
		'type'           => 'option',
	));
	$wp_customize->add_control('facebook', array(
		'label'      => __('فیس بوک', 'dw-gamez'),
		'section'    => 'dw_gamez_social_links',
		'settings'   => 'dw_gamez_theme_options[facebook]',
	));
	$wp_customize->add_setting('dw_gamez_theme_options[twitter]', array(
		'default'        => '',
		'capability'     => 'edit_theme_options',
		'type'           => 'option',
	));
	$wp_customize->add_control('twitter', array(
		'label'      => __('تویتر', 'dw-gamez'),
		'section'    => 'dw_gamez_social_links',
		'settings'   => 'dw_gamez_theme_options[twitter]',
	));
	$wp_customize->add_setting('dw_gamez_theme_options[google_plus]', array(
		'default'        => '',
		'capability'     => 'edit_theme_options',
		'type'           => 'option',
	));
	$wp_customize->add_control('google_plus', array(
		'label'      => __('گوگل پلاس', 'dw-gamez'),
		'section'    => 'dw_gamez_social_links',
		'settings'   => 'dw_gamez_theme_options[google_plus]',
	));
	$wp_customize->add_setting('dw_gamez_theme_options[youtube]', array(
		'default'        => '',
		'capability'     => 'edit_theme_options',
		'type'           => 'option',
	));
	$wp_customize->add_control('youtube', array(
		'label'      => __('یوتیوپ', 'dw-gamez'),
		'section'    => 'dw_gamez_social_links',
		'settings'   => 'dw_gamez_theme_options[youtube]',
	));
	$wp_customize->add_setting('dw_gamez_theme_options[linkedin]', array(
		'default'        => '',
		'capability'     => 'edit_theme_options',
		'type'           => 'option',
	));
	$wp_customize->add_control('linkedin', array(
		'label'      => __('LinkedIn', 'dw-gamez'),
		'section'    => 'dw_gamez_social_links',
		'settings'   => 'dw_gamez_theme_options[linkedin]',
	));

	// BACKGROUND --------------------------------------------------------------------------------------
	$wp_customize->add_setting('dw_gamez_theme_options[background-size]', array(
		'default'        => 'cover',
		'capability'     => 'edit_theme_options',
		'type'           => 'option',
	));
	$wp_customize->add_control('background-size', array(
		'label'      => __('اندازه پس زمینه', 'dw-gamez'),
		'section'    => 'background_image',
		'settings'   => 'dw_gamez_theme_options[background-size]',
		'type'       => 'radio',
		'choices'    => array(
			'auto' => 'Auto',
			'cover' => 'Cover',
		),
	));

	// STYLE SELECTOR --------------------------------------------------------------------------------------
    $wp_customize->add_section('dw_gamez_primary_color', array(
        'title'    => __('استایل انتخاب گر', 'dw-gamez'),
        'priority' => 110,
    ));
    $wp_customize->add_setting('dw_gamez_theme_options[select-color]', array(
        'capability'     => 'edit_theme_options',
        'type'           => 'option',
    ));
    $wp_customize->add_control( new Color_Picker_Custom_control($wp_customize, 'select-color', array(
        'label' => __('انتخاب رنگ', 'dw-gamez'),
        'section' => 'dw_gamez_primary_color',
        'settings' => 'dw_gamez_theme_options[select-color]',
        'choices' => array('#0fa9ec', '#fc615d', '#7cc576', '#B39964', '#e07798')
    )));
    $wp_customize->add_setting('dw_gamez_theme_options[custom-color]', array(
        'default'        => '',
        'capability'     => 'edit_theme_options',
        'type'           => 'option'
    ));
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'custom-color', array(
        'label'        => __( 'رنگ سفارشی', 'dw-gamez' ),
        'section'    => 'dw_gamez_primary_color',
        'settings'   => 'dw_gamez_theme_options[custom-color]',
        'choices' => '#0fa9ec'
    )));

    // FONT SELECTOR --------------------------------------------------------------------------------------
    $fonts = dw_get_gfonts();
    $newarray = array();
    $newarray[] = '';
    foreach ($fonts as $index => $font) {
    foreach ($font->files as $key => $value) {
      $newarray[$font->family . ':dw:' . $value ] = $font->family . ' - ' . $key;
    }
    }
    $wp_customize->add_section('dw_gamez_typo', array(
        'title'    => __('انتخاب فونت', 'dw-gamez'),
        'priority' => 111
    ));
    $wp_customize->add_setting('dw_gamez_theme_options[heading_font]', array(
        'default'        => '',
        'capability'     => 'edit_theme_options',
        'type'           => 'option'
    ));
    $wp_customize->add_control( 'heading_font', array(
        'settings' => 'dw_gamez_theme_options[heading_font]',
        'label'   => 'انتخاب فونت سربرگ ها',
        'section' => 'dw_gamez_typo',
        'type'    => 'select',
        'choices'    => $newarray
    ));
    $wp_customize->add_setting('dw_gamez_theme_options[body_font]', array(
        'default'        => '',
        'capability'     => 'edit_theme_options',
        'type'           => 'option'
    ));
    $wp_customize->add_control( 'body_font', array(
        'settings' => 'dw_gamez_theme_options[body_font]',
        'label'   => 'انتخاب فونت بدنه',
        'section' => 'dw_gamez_typo',
        'type'    => 'select',
        'choices'    => $newarray
    ));

    // CUSTOM CODE --------------------------------------------------------------------------------------
	$wp_customize->add_section('dw_gamez_custom_code', array(
		'title'    => __('کد های سفارشی', 'dw-gamez'),
		'priority' => 200,
	));
	$wp_customize->add_setting('dw_gamez_theme_options[header_code]', array(
			'default' => '',
			'capability' => 'edit_theme_options',
			'type' => 'option',
	));
	$wp_customize->add_control( new DW_Gamez_Textarea_Custom_Control($wp_customize, 'header_code', array(
		'label'    => __('Header Code (Meta tags, CSS, etc ...)', 'dw-gamez'),
		'section'  => 'dw_gamez_custom_code',
		'settings' => 'dw_gamez_theme_options[header_code]',
	)));
	$wp_customize->add_setting('dw_gamez_theme_options[footer_code]', array(
		'default' => '',
		'capability' => 'edit_theme_options',
		'type' => 'option',
	));
	$wp_customize->add_control( new DW_Gamez_Textarea_Custom_Control($wp_customize, 'footer_code', array(
		'label'    => __('Footer Code (Analytics, etc ...)', 'dw-gamez'),
		'section'  => 'dw_gamez_custom_code',
		'settings' => 'dw_gamez_theme_options[footer_code]'
	)));
}
add_action( 'customize_register', 'dw_gamez_customize_register' );

function dw_gamez_get_theme_option( $option_name, $default = '' ) {
	$options = get_option( 'dw_gamez_theme_options' );
	if( isset($options[$option_name]) ) {
		return $options[$option_name];
	}
	return $default; 
}

if( ! function_exists('dw_get_gfonts') ) {
  function dw_get_gfonts(){
    $fontsSeraliazed = wp_remote_fopen( get_template_directory_uri() . '/inc/font/gfonts_v2.txt' );
    $fontArray = unserialize( $fontsSeraliazed );
    return $fontArray->items;
  }
}
if( ! function_exists('dw_colour_brightness') ) {
	function dw_colour_brightness($hex, $percent) {
	    $hash = '';
	    if (stristr($hex,'#')) {
	        $hex = str_replace('#','',$hex);
	        $hash = '#';
	    }
	    $rgb = array(hexdec(substr($hex,0,2)), hexdec(substr($hex,2,2)), hexdec(substr($hex,4,2)));
	    for ($i=0; $i < 3; $i++) {
	        if ($percent > 0) {
	            $rgb[$i] = round($rgb[$i] * $percent) + round(255 * (1-$percent));
	        } else {
	            $positivePercent = $percent - ($percent*2);
	            $rgb[$i] = round($rgb[$i] * $positivePercent) + round(0 * (1-$positivePercent));
	        }
	        if ($rgb[$i] > 255) {
	            $rgb[$i] = 255;
	        }
	    }
	    $hex = '';
	    for($i=0; $i < 3; $i++) {
	        $hexDigit = dechex($rgb[$i]);
	        if(strlen($hexDigit) == 1) {
	            $hexDigit = "0" . $hexDigit;
	        }
	        $hex .= $hexDigit;
	    }
	    return $hash.$hex;
	}
}

function dw_gamez_custom_background_css() {
	echo '<style type="text/css" id="custom-background-css"> body.custom-background { background-size: '.dw_gamez_get_theme_option('background-size', 'cover').' }</style>';
}
add_action('wp_head','dw_gamez_custom_background_css');

function dw_gamez_customize_preview_js() {
	unset( $_COOKIE["dw_gamez_main_content_layout"] );
	wp_enqueue_script( 'dw-gamez-cookie', get_template_directory_uri() . '/assets/js/jquery.cookie.js', array( 'customize-preview' ), '20130716' );
	wp_enqueue_script( 'dw_gamez_customizer', get_template_directory_uri() . '/inc/js/customizer.js', array( 'customize-preview' ), '20130508', true );
}
add_action( 'customize_preview_init', 'dw_gamez_customize_preview_js' );