<?php
/**
 * materializer Theme Customizer
 *
 * @package materializer
 */

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function materializer_theme_customize_register($wp_customize)
{
    $wp_customize->get_setting('blogname')->transport = 'postMessage';
}

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function materializer_theme_customize_preview_js()
{
    if (function_exists('wp_enqueue_script') && function_exists('get_template_directory_uri')) {
        wp_enqueue_script('materializer_theme_customizer', get_template_directory_uri() . '/js/customizer.js', array('customize-preview'), '20151215', true);
    }
}

/**
 * Theme custom colors/content/logo settings
 */
function materializer_register_theme_customizer($wp_customize)
{

    /**
     * Custom Header background
     */
    $wp_customize->add_setting(
        'materializer_header_background', array(
            'default' => '#29b6f6',
            'sanitize_callback' => 'cs_sanitize_clean',
        )
    );

    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize, 'header_background', array(
                'label' => __('Header Background', 'materializer'),
                'section' => 'colors',
                'settings' => 'materializer_header_background'
            )
        )
    );

    /**
     * Custom Header logo link
     */
    $wp_customize->add_setting(
        'materializer_header_logo_link', array(
            'default' => '#fff',
            'sanitize_callback' => 'cs_sanitize_clean',
        )
    );


    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize, 'header_logo_link', array(
                'label' => __('Header Logo Link', 'materializer'),
                'section' => 'colors',
                'settings' => 'materializer_header_logo_link'
            )
        )
    );

    /**
     * Custom Header nav link
     */
    $wp_customize->add_setting(
        'materializer_header_nav_link', array(
            'default' => '#fff',
            'sanitize_callback' => 'cs_sanitize_clean',
        )
    );


    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize, 'header_nav_link', array(
                'label' => __('Header Nav Link', 'materializer'),
                'section' => 'colors',
                'settings' => 'materializer_header_nav_link'
            )
        )
    );

    /**
     * Custom Header tagline
     */
    $wp_customize->add_setting(
        'materializer_header_tagline', array(
            'default' => '#ff9800',
            'sanitize_callback' => 'cs_sanitize_clean',
        )
    );


    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize, 'header_tagline', array(
                'label' => __('Header Tagline', 'materializer'),
                'section' => 'colors',
                'settings' => 'materializer_header_tagline'
            )
        )
    );

    /**
     * Custom Button large
     */
    $wp_customize->add_setting(
        'materializer_btn_large', array(
            'default' => '#ff9800',
            'sanitize_callback' => 'cs_sanitize_clean',
        )
    );


    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize, 'btn_large', array(
                'label' => __('Button Large', 'materializer'),
                'section' => 'colors',
                'settings' => 'materializer_btn_large'
            )
        )
    );

    /**
     * Footer background
     */
    $wp_customize->add_setting(
        'materializer_footer_background', array(
            'default' => '#ff9800',
            'sanitize_callback' => 'cs_sanitize_clean',
        )
    );


    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize, 'footer_background', array(
                'label' => __('Footer Background', 'materializer'),
                'section' => 'colors',
                'settings' => 'materializer_footer_background'
            )
        )
    );

    /**
     * Footer text
     */
    $wp_customize->add_setting(
        'materializer_footer_text', array(
            'default' => '#fff',
            'sanitize_callback' => 'cs_sanitize_clean',
        )
    );


    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize, 'footer_text', array(
                'label' => __('Footer Text', 'materializer'),
                'section' => 'color',
                'settings' => 'materializer_footer_text'
            )
        )
    );

    /**
     * Footer text link
     */
    $wp_customize->add_setting(
        'materializer_footer_text_link', array(
            'default' => '#fff',
            'sanitize_callback' => 'cs_sanitize_clean',
        )
    );


    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize, 'footer_text_link', array(
                'label' => __('Footer Text Link', 'materializer'),
                'section' => 'colors',
                'settings' => 'materializer_footer_text_link'
            )
        )
    );

    /**
     * Footer copyright link
     */
    $wp_customize->add_setting(
        'materializer_footer_copyright_link', array(
            'default' => '#fff',
            'sanitize_callback' => 'cs_sanitize_clean',
        )
    );


    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize, 'footer_copyright_link', array(
                'label' => __('Footer Copyright Link', 'materializer'),
                'section' => 'colors',
                'settings' => 'materializer_footer_copyright_link'
            )
        )
    );

    /**
     * Breadcrumbs background
     */
    $wp_customize->add_setting(
        'materializer_breadcrumbs', array(
            'default' => '#fff',
            'sanitize_callback' => 'cs_sanitize_clean',
        )
    );


    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize, 'breadcrumbs', array(
                'label' => __('Breadcrumbs Background', 'materializer'),
                'section' => 'colors',
                'settings' => 'materializer_breadcrumbs'
            )
        )
    );

    /**
     * Breadcrumbs text link
     */
    $wp_customize->add_setting(
        'materializer_breadcrumbs_text_link', array(
            'default' => '#fff',
            'sanitize_callback' => 'cs_sanitize_clean',
        )
    );


    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize, 'breadcrumbs_text_link', array(
                'label' => __('Breadcrumbs Text Link', 'materializer'),
                'section' => 'colors',
                'settings' => 'materializer_breadcrumbs_text_link'
            )
        )
    );

    /**
     * -----------------------------------------------------------------------------------------------------------
     * Custom text blocks
     */
    $wp_customize->add_panel('custom_text_blocks', array(
        'priority' => 500,
        'theme_supports' => '',
        'title' => __('Custom Text Blocks', 'materializer'),
        'description' => __('Set editable text for certain content.', 'materializer'),
    ));

    /**
     * -----------------------------------------------------------------------------------------------------------
     * Add Homepage block text section.
     */
    $wp_customize->add_section('custom_homepage_text', array(
        'title' => __('Homepage Block Texts', 'materializer'),
        'panel' => 'custom_text_blocks',
        'priority' => 10
    ));

    /**
     * -----------------------------------------------------------------------------------------------------------
     * Add Homepage call to action section.
     */
    $wp_customize->add_section('homepage_call_to_action', array(
        'title' => __('Homepage Call To Action', 'materializer'),
        'panel' => 'custom_text_blocks',
        'priority' => 9
    ));

    /**
     * -----------------------------------------------------------------------------------------------------------
     * Add Homepage block settings.
     */
    $wp_customize->add_setting('homepage_text_block_one_icon', array(
        'default' => __('', 'materializer'),
        'sanitize_callback' => 'cs_sanitize_clean',
    ));

    $wp_customize->add_setting('homepage_text_block_one_headline', array(
        'default' => __('', 'materializer'),
        'sanitize_callback' => 'cs_sanitize_clean',
    ));

    $wp_customize->add_setting('homepage_text_block_one', array(
        'default' => __('', 'materializer'),
        'sanitize_callback' => 'cs_sanitize_clean',
    ));

    /**
     * -----------------------------------------------------------------------------------------------------------
     */
    $wp_customize->add_setting('homepage_text_block_two_icon', array(
        'default' => __('', 'materializer'),
        'sanitize_callback' => 'cs_sanitize_clean',
    ));

    $wp_customize->add_setting('homepage_text_block_two_headline', array(
        'default' => __('', 'materializer'),
        'sanitize_callback' => 'cs_sanitize_clean',
    ));

    $wp_customize->add_setting('homepage_text_block_two', array(
        'default' => __('', 'materializer'),
        'sanitize_callback' => 'cs_sanitize_clean',
    ));

    /**
     * -----------------------------------------------------------------------------------------------------------
     */
    $wp_customize->add_setting('homepage_text_block_three_icon', array(
        'default' => __('', 'materializer'),
        'sanitize_callback' => 'cs_sanitize_clean',
    ));

    $wp_customize->add_setting('homepage_text_block_three_headline', array(
        'default' => __('', 'materializer'),
        'sanitize_callback' => 'cs_sanitize_clean',
    ));

    $wp_customize->add_setting('homepage_text_block_three', array(
        'default' => __('', 'materializer'),
        'sanitize_callback' => 'cs_sanitize_clean',
    ));

    /**
     * -----------------------------------------------------------------------------------------------------------
     * Add Homepage call to action settings.
     */
    $wp_customize->add_setting('homepage_call_to_action_text', array(
        'default' => __('Modern and responsive', 'materializer'),
        'sanitize_callback' => 'cs_sanitize_clean',
    ));

    $wp_customize->add_setting('homepage_call_to_action_btn_text', array(
        'default' => __('GET STARTED', 'materializer'),
        'sanitize_callback' => 'cs_sanitize_clean',
    ));

    $wp_customize->add_setting('homepage_call_to_action_btn_link', array(
        'default' => __('about-us', 'materializer'),
        'sanitize_callback' => 'cs_sanitize_clean',
    ));

    /**
     * -----------------------------------------------------------------------------------------------------------
     * Add Homepage block control
     */
    $wp_customize->add_control(new WP_Customize_Control(
            $wp_customize, 'custom_homepage_text_one_icon', array(
                'label' => __('Block Text One Icon (e.g. flash_on)', 'materializer'),
                'section' => 'custom_homepage_text',
                'settings' => 'homepage_text_block_one_icon',
                'type' => 'text'
            )
        )
    );


    $wp_customize->add_control(new WP_Customize_Control(
            $wp_customize, 'custom_homepage_text_one_headline', array(
                'label' => __('Block Text One Headline', 'materializer'),
                'section' => 'custom_homepage_text',
                'settings' => 'homepage_text_block_one_headline',
                'type' => 'text'
            )
        )
    );


    $wp_customize->add_control(new WP_Customize_Control(
            $wp_customize, 'custom_homepage_text_one', array(
                'label' => __('Block text one', 'materializer'),
                'section' => 'custom_homepage_text',
                'settings' => 'homepage_text_block_one',
                'type' => 'textarea'
            )
        )
    );

    /**
     * -----------------------------------------------------------------------------------------------------------
     */
    $wp_customize->add_control(new WP_Customize_Control(
            $wp_customize, 'custom_homepage_text_two_icon', array(
                'label' => __('Block Text Two Icon (e.g. group)', 'materializer'),
                'section' => 'custom_homepage_text',
                'settings' => 'homepage_text_block_two_icon',
                'type' => 'text'
            )
        )
    );

    $wp_customize->add_control(new WP_Customize_Control(
            $wp_customize, 'custom_homepage_text_two_headline', array(
                'label' => __('Block Text Two Headline', 'materializer'),
                'section' => 'custom_homepage_text',
                'settings' => 'homepage_text_block_two_headline',
                'type' => 'text'
            )
        )
    );

    $wp_customize->add_control(new WP_Customize_Control(
            $wp_customize, 'custom_homepage_text_two', array(
                'label' => __('Block Text Two', 'materializer'),
                'section' => 'custom_homepage_text',
                'settings' => 'homepage_text_block_two',
                'type' => 'textarea'
            )
        )
    );

    /**
     * -----------------------------------------------------------------------------------------------------------
     */
    $wp_customize->add_control(new WP_Customize_Control(
            $wp_customize, 'custom_homepage_text_three_icon', array(
                'label' => __('Block Text Three Icon (e.g. settings)', 'materializer'),
                'section' => 'custom_homepage_text',
                'settings' => 'homepage_text_block_three_icon',
                'type' => 'text'
            )
        )
    );

    $wp_customize->add_control(new WP_Customize_Control(
            $wp_customize, 'custom_homepage_text_three_headline', array(
                'label' => __('Block Text Three Headline', 'materializer'),
                'section' => 'custom_homepage_text',
                'settings' => 'homepage_text_block_three_headline',
                'type' => 'text'
            )
        )
    );

    $wp_customize->add_control(new WP_Customize_Control(
            $wp_customize, 'custom_homepage_text_three', array(
                'label' => __('Block Text Three', 'materializer'),
                'section' => 'custom_homepage_text',
                'settings' => 'homepage_text_block_three',
                'type' => 'textarea'
            )
        )
    );

    /**
     * -----------------------------------------------------------------------------------------------------------
     * Add Homepage call to action control
     */
    $wp_customize->add_control(new WP_Customize_Control(
            $wp_customize, 'homepage_call_to_action_text', array(
                'label' => __('Textarea', 'materializer'),
                'section' => 'homepage_call_to_action',
                'settings' => 'homepage_call_to_action_text',
                'type' => 'textarea'
            )
        )
    );

    $wp_customize->add_control(new WP_Customize_Control(
            $wp_customize, 'homepage_call_to_action_button_text', array(
                'label' => __('Button Text', 'materializer'),
                'section' => 'homepage_call_to_action',
                'settings' => 'homepage_call_to_action_btn_text',
                'type' => 'text'
            )
        )
    );

    $wp_customize->add_control(new WP_Customize_Control(
            $wp_customize, 'homepage_call_to_action_button_link', array(
                'label' => __('Button Link', 'materializer'),
                'section' => 'homepage_call_to_action',
                'settings' => 'homepage_call_to_action_btn_link',
                'type' => 'text'
            )
        )
    );

    /**
     * -----------------------------------------------------------------------------------------------------------
     *  Add Footer Text add action.
     */
    $wp_customize->add_section('custom_footer_text', array(
        'title' => __('Change Footer Copyright', 'materializer'),
        'panel' => 'custom_text_blocks',
        'priority' => 10
    ));


    // Add setting
    $wp_customize->add_setting('footer_text_block', array(
        'default' => __('', 'materializer'),
        'sanitize_callback' => 'cs_sanitize_clean',
    ));


    // Add control
    $wp_customize->add_control(new WP_Customize_Control(
            $wp_customize, 'custom_footer_text', array(
                'label' => __('Footer Text', 'materializer'),
                'section' => 'custom_footer_text',
                'settings' => 'footer_text_block',
                'type' => 'text'
            )
        )
    );
}

/**
 * UPDATE the CSS
 */
function materializer_customizer_css()
{
    ?>
    <style type="text/css">
        .light-blue.lighten-1 {
            background-color: <?php echo get_theme_mod('materializer_header_background'); ?> !important;
        }

        nav .brand-logo {
            color: <?php echo get_theme_mod('materializer_header_logo_link'); ?> !important;
        }

        nav ul a {
            color: <?php echo get_theme_mod('materializer_header_nav_link'); ?> !important;
        }

        h1.orange-text {
            color: <?php echo get_theme_mod('materializer_header_tagline'); ?> !important;
        }

        .btn-large {
            background-color: <?php echo get_theme_mod('materializer_btn_large'); ?> !important;
        }

        .page-footer {
            background-color: <?php echo get_theme_mod('materializer_footer_background'); ?> !important;
        }

        .page-footer p {
            color: <?php echo get_theme_mod('materializer_footer_text'); ?> !important;
        }

        .page-footer li a {
            color: <?php echo get_theme_mod('materializer_footer_text_link'); ?> !important;
        }

        .footer-copyright p a {
            color: <?php echo get_theme_mod('materializer_footer_copyright_link'); ?> !important;
        }

        .breadcrumbs {
            background-color: <?php echo get_theme_mod('materializer_breadcrumbs'); ?> !important;
            color: <?php echo get_theme_mod('materializer_breadcrumbs_text_link'); ?> !important;
        }

        .breadcrumbs a {
            color: <?php echo get_theme_mod('materializer_breadcrumbs_text_link'); ?> !important;
        }
    </style>
    <?php
}

/**
 * Add logo theme support
 */
function materializer_theme_prefix_setup()
{

    add_theme_support('custom-logo', array(
        'height' => 49,
        'width' => 220,
        'flex-width' => true,
    ));
}

/**
 * Output the logo
 */
function materializer_theme_prefix_the_custom_logo()
{

    if (function_exists('the_custom_logo')) {
        the_custom_logo();
    }
}

/**
 * Footer copyright support
 */
function materializer_footer_text()
{
    if (function_exists('get_theme_mod')) {

        if (get_theme_mod('footer_text_block') != "") {
            echo get_theme_mod('footer_text_block');
        } else {
            echo 'Copyright &copy; 2017 Materializer WP Theme on WordPress'; // Add you default footer text here
        }
    }
}


/**
 * @param $value
 * @return mixed
 */
function cs_sanitize_clean($value ) {
    return $value;
}