<?php

namespace Layerdrops\Ostech;

class Customizer
{
    public function __construct()
    {
        add_action("customize_register", [$this, 'ostech_customizer']);
    }
    public function ostech_customizer($wp_customize)
    {

        // add panel
        $wp_customize->add_panel(
            'ostech_theme_opt',
            array(
                'title'      => esc_html__('Ostech Options', 'ostech-addon'),
                'description' => esc_html__('Ostech Theme options panel', 'ostech-addon'),
                'priority'   => 220,
                'capability' => 'edit_theme_options',
            )
        );

        // General Settings
        $wp_customize->add_section('ostech_theme_general', array(
            'title' => __('General Settings', 'ostech-addon'),
            'description' => esc_html__('Ostech General Settings.', 'ostech-addon'),
            'priority' => 10,
            'capability' => 'edit_theme_options',
            'panel'      => 'ostech_theme_opt'
        ));

        $this->customize_type_radio(
            $wp_customize,
            esc_html__(' Enable Dark Mode?', 'ostech-addon'),
            'ostech_theme_general',
            'ostech_dark_mode',
            'no',
            array(
                'yes' => esc_html__('Yes', 'ostech-addon'),
                'no' => esc_html__('No', 'ostech-addon'),
            )
        );

        $this->customize_type_radio(
            $wp_customize,
            esc_html__(' Enable Boxed Mode?', 'ostech-addon'),
            'ostech_theme_general',
            'ostech_boxed_mode',
            'no',
            array(
                'yes' => esc_html__('Yes', 'ostech-addon'),
                'no' => esc_html__('No', 'ostech-addon'),
            )
        );

        $this->customize_type_radio(
            $wp_customize,
            esc_html__(' Enable Rtl Mode?', 'ostech-addon'),
            'ostech_theme_general',
            'ostech_rtl_mode',
            'no',
            array(
                'yes' => esc_html__('Yes', 'ostech-addon'),
                'no' => esc_html__('No', 'ostech-addon'),
            )
        );

        $this->customize_type_radio(
            $wp_customize,
            esc_html__(' Enable Custom Cursor', 'ostech-addon'),
            'ostech_theme_general',
            'custom_cursor',
            'yes',
            array(
                'yes' => esc_html__('Yes', 'ostech-addon'),
                'no' => esc_html__('No', 'ostech-addon'),
            )
        );

        $this->customize_type_radio(
            $wp_customize,
            esc_html__(' Enable Back to top?', 'ostech-addon'),
            'ostech_theme_general',
            'scroll_to_top',
            'yes',
            array(
                'yes' => esc_html__('Yes', 'ostech-addon'),
                'no' => esc_html__('No', 'ostech-addon'),
            )
        );

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Select Back to top text', 'ostech-addon'),
            'ostech_theme_general',
            'scroll_to_top_text',
            'back to top',
            function () {
                return (get_theme_mod('scroll_to_top', 'no') == 'yes' ? true : false);
            }
        );

        $this->customize_type_radio(
            $wp_customize,
            esc_html__(' Enable Preloader?', 'ostech-addon'),
            'ostech_theme_general',
            'preloader',
            'yes',
            array(
                'yes' => esc_html__('Yes', 'ostech-addon'),
                'no' => esc_html__('No', 'ostech-addon'),
            )
        );

        $this->customize_type_image(
            $wp_customize,
            esc_html__('Custom Preloader Image', 'ostech-addon'),
            'ostech_theme_general',
            'preloader_image',
            '',
            function () {
                return (get_theme_mod('preloader', 'no') == 'yes' ? true : false);
            }
        );

        $this->customize_type_image(
            $wp_customize,
            esc_html__('Page Header Image', 'ostech-addon'),
            'ostech_theme_general',
            'page_header_bg_image'
        );

        // Color Settings
        $wp_customize->add_section('ostech_theme_color', array(
            'title' => __('Color Settings', 'ostech-addon'),
            'description' => esc_html__('Ostech Color Settings.', 'ostech-addon'),
            'priority' => 10,
            'capability' => 'edit_theme_options',
            'panel'      => 'ostech_theme_opt'
        ));

        ostech_source_color_meta('set_customizer', $wp_customize, $this);

        // Blog Layout
        $wp_customize->add_section('ostech_blog_layout_settings', array(
            'title' => __('Blog Layout', 'ostech-addon'),
            'description' => esc_html__('Ostech Blog Layout', 'ostech-addon'),
            'priority' => 20,
            'capability' => 'edit_theme_options',
            'panel'      => 'ostech_theme_opt'
        ));

        $this->customize_type_select(
            $wp_customize,
            'Select Sidebar position',
            'ostech_blog_layout_settings',
            'ostech_blog_layout',
            'right-align',
            array(
                'left-align' => esc_html__('Left Align', 'ostech-addon'),
                'right-align' => esc_html__('Right Align', 'ostech-addon'),
                'full-width' => esc_html__('Full Width', 'ostech-addon'),
            )
        );

        // Shop Layout
        $wp_customize->add_section('ostech_shop_layout_settings', array(
            'title' => __('Shop Layout', 'ostech-addon'),
            'description' => esc_html__('ostech Shop Layout', 'ostech-addon'),
            'priority' => 20,
            'capability' => 'edit_theme_options',
            'panel'      => 'ostech_theme_opt'
        ));


        $this->customize_type_select(
            $wp_customize,
            'Select Shop Column',
            'ostech_shop_layout_settings',
            'ostech_shop_column',
            '3',
            array(
                '2' => esc_html__('2 Column', 'ostech-addon'),
                '3' => esc_html__('3 Column', 'ostech-addon'),
                '4' => esc_html__('4 Column', 'ostech-addon'),
            )
        );

        $this->customize_type_select(
            $wp_customize,
            'Select Shop Sidebar position',
            'ostech_shop_layout_settings',
            'ostech_shop_layout',
            'left-align',
            array(
                'left-align' => esc_html__('Left Align', 'ostech-addon'),
                'right-align' => esc_html__('Right Align', 'ostech-addon'),
                'full-width' => esc_html__('Full Width', 'ostech-addon'),
            )
        );

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Shop Product Per page', 'ostech-addon'),
            'ostech_shop_layout_settings',
            'product_per_page',
            esc_html(12)
        );

        // Header options
        $wp_customize->add_section('ostech_theme_header', array(
            'title' => __('Header Settings', 'ostech-addon'),
            'description' => esc_html__('Ostech Header Settings', 'ostech-addon'),
            'priority' => 20,
            'capability' => 'edit_theme_options',
            'panel'      => 'ostech_theme_opt'
        ));

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Add Logo size in px', 'ostech-addon'),
            'ostech_theme_header',
            'header_logo_width',
            esc_html(198)
        );

        $this->customize_type_radio(
            $wp_customize,
            esc_html__('Enable Sticky Header?', 'ostech-addon'),
            'ostech_theme_header',
            'header_sticky_menu',
            'yes',
            array(
                'yes' => esc_html__('Yes', 'ostech-addon'),
                'no' => esc_html__('No', 'ostech-addon'),
            )
        );

        $this->customize_type_radio(
            $wp_customize,
            esc_html__(' Enable Breadcrumb?', 'ostech-addon'),
            'ostech_theme_header',
            'breadcrumb_opt',
            'yes',
            array(
                'yes' => esc_html__('Yes', 'ostech-addon'),
                'no' => esc_html__('No', 'ostech-addon'),
            )
        );

        $this->customize_type_radio(
            $wp_customize,
            esc_html__('Enable Custom Header?', 'ostech-addon'),
            'ostech_theme_header',
            'header_custom',
            'no',
            array(
                'yes' => esc_html__('Yes', 'ostech-addon'),
                'no' => esc_html__('No', 'ostech-addon'),
            )
        );

        $this->customize_type_select(
            $wp_customize,
            esc_html__('Select Header Type', 'ostech-addon'),
            'ostech_theme_header',
            'header_custom_post',
            '',
            ostech_post_query('header'),
            function () {
                return (get_theme_mod('header_custom', 'no') == 'yes' ? true : false);
            }
        );

        //  Mobile Menu
        $wp_customize->add_section('ostech_theme_mobile_menu', array(
            'title' => esc_html__('Mobile Menu Settings', 'ostech-addon'),
            'description' => esc_html__('Ostech Header Settings', 'ostech-addon'),
            'priority' => 20,
            'capability' => 'edit_theme_options',
            'panel'      => 'ostech_theme_opt'
        ));

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Mobile Menu Email', 'ostech-addon'),
            'ostech_theme_mobile_menu',
            'ostech_mobile_menu_email',
            esc_html__('needhelp@ostech.com', 'ostech-addon')
        );

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Mobile Menu Phone', 'ostech-addon'),
            'ostech_theme_mobile_menu',
            'ostech_mobile_menu_phone',
            esc_html__('666 888 0000', 'ostech-addon')
        );

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Facebook url', 'ostech-addon'),
            'ostech_theme_mobile_menu',
            'facebook_url',
            esc_html('#')
        );

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Twitter url', 'ostech-addon'),
            'ostech_theme_mobile_menu',
            'twitter_url',
            esc_html('#')
        );

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Linkedin url', 'ostech-addon'),
            'ostech_theme_mobile_menu',
            'linkedin_url',
            esc_html('#')
        );

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Pinterest url', 'ostech-addon'),
            'ostech_theme_mobile_menu',
            'pinterest_url',
            esc_html('#')
        );

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Youtube url', 'ostech-addon'),
            'ostech_theme_mobile_menu',
            'youtube_url',
        );


        $this->customize_type_text(
            $wp_customize,
            esc_html__('dribbble url', 'ostech-addon'),
            'ostech_theme_mobile_menu',
            'dribble_url',
        );

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Instagram url', 'ostech-addon'),
            'ostech_theme_mobile_menu',
            'instagram_url',
        );

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Reddit url', 'ostech-addon'),
            'ostech_theme_mobile_menu',
            'reddit_url',
        );

        // Footer options
        $wp_customize->add_section('ostech_theme_footer', array(
            'title' => esc_html__('Footer Settings', 'ostech-addon'),
            'description' => esc_html__('Ostech Footer Settings.', 'ostech-addon'),
            'priority' => 20,
            'capability' => 'edit_theme_options',
            'panel'      => 'ostech_theme_opt'
        ));

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Footer Text', 'ostech-addon'),
            'ostech_theme_footer',
            'footer_copytext',
            esc_html__('&copy; All right reserved', 'ostech'),
            function () {
                return (get_theme_mod('footer_custom', 'no') == 'yes' ? false : true);
            }
        );

        $this->customize_type_radio(
            $wp_customize,
            esc_html__('Enable Custom Footer ?', 'ostech-addon'),
            'ostech_theme_footer',
            'footer_custom',
            'no',
            array(
                'yes' => esc_html__('Yes', 'ostech-addon'),
                'no' => esc_html__('No', 'ostech-addon'),
            )
        );

        $this->customize_type_select(
            $wp_customize,
            esc_html__('Select Footer Type', 'ostech-addon'),
            'ostech_theme_footer',
            'footer_custom_post',
            '',
            ostech_post_query('footer'),
            function () {
                return (get_theme_mod('footer_custom', 'no') == 'yes' ? true : false);
            }
        );

        // register
        $wp_customize->add_section('ostech_login_settings', array(
            'title' => __('Login/Register Settings', 'ostech-addon'),
            'description' => esc_html__('Login/Register Settings', 'ostech-addon'),
            'priority' => 20,
            'capability' => 'edit_theme_options',
            'panel'      => 'ostech_theme_opt'
        ));

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Redirect url After Login', 'ostech-addon'),
            'ostech_login_settings',
            'login_redirect_url',
        );

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Redirect url After Registration', 'ostech-addon'),
            'ostech_login_settings',
            'registration_redirect_url',
        );

        $this->customize_type_text(
            $wp_customize,
            esc_html__('Redirect url After Log Out', 'ostech-addon'),
            'ostech_login_settings',
            'logout_redirect_url',
        );

        // error page
        $wp_customize->add_section('ostech_error_page_settings', array(
            'title' => __('404 Settings', 'ostech-addon'),
            'description' => esc_html__('Change 404 Text', 'ostech-addon'),
            'priority' => 20,
            'capability' => 'edit_theme_options',
            'panel'      => 'ostech_theme_opt'
        ));

        $this->customize_type_radio(
            $wp_customize,
            esc_html__('Enable Custom 404 ?', 'ostech-addon'),
            'ostech_error_page_settings',
            'error_custom',
            'no',
            array(
                'yes' => esc_html__('Yes', 'ostech-addon'),
                'no' => esc_html__('No', 'ostech-addon'),
            )
        );

        $this->customize_type_select(
            $wp_customize,
            esc_html__('Select 404 Page', 'ostech-addon'),
            'ostech_error_page_settings',
            'error_custom_post',
            '',
            ostech_post_query('page'),
            function () {
                return (get_theme_mod('error_custom', 'no') == 'yes' ? true : false);
            }
        );

        $this->customize_type_image(
            $wp_customize,
            esc_html__('404 Page Title Image', 'ostech-addon'),
            'ostech_error_page_settings',
            '404_page_title_image'
        );
    }

    //type text
    public function customize_type_text($wp_customize, $label, $section_id, $name,  $default = "", $callback = null)
    {
        // add settings
        $wp_customize->add_setting($name, array(
            'default'  => $default,
            'type'     => 'theme_mod'
        ));

        $wp_customize->add_control(new \WP_Customize_Control(
            $wp_customize,
            $name,
            array(
                "label" => $label,
                "section" => $section_id,
                "settings" => $name,
                "type" => "text",
                "active_callback" => $callback,
            )
        ));
    }


    //type color
    //type color
    public function customize_type_color($wp_customize, $section_id, $name,  $default = false)
    {
        $prefix_name = str_replace("-", "_", $name);
        // add settings
        $wp_customize->add_setting($prefix_name, array(
            'default'  => sanitize_hex_color($default),
            'type'     => 'theme_mod'
        ));

        // Add control
        $wp_customize->add_control(new \WP_Customize_Color_Control($wp_customize, $prefix_name, array(
            'label'    => ucwords(str_replace("-", " ", $name)),
            'section'  => $section_id,
            'setting' => $prefix_name,
            'priority' => 1
        )));
    }

    // type checkbox
    public function customize_type_checkbox($wp_customize, $label, $section_id, $name,  $default, $callback = null)
    {
        $wp_customize->add_setting($name, array(
            "default" => $default,
            "transport" => "refresh",

        ));

        $wp_customize->add_control(new \WP_Customize_Control(
            $wp_customize,
            $name,
            array(
                "label" => $label,
                "section" => $section_id,
                "settings" => $name,
                "type" => "checkbox",
                "active_callback" => $callback,
            )
        ));
    }

    // type Image
    public function customize_type_image($wp_customize, $label, $section_id, $name,  $default = '', $callback = null)
    {
        $wp_customize->add_setting($name, array(
            "default" => $default,
            "transport" => "refresh",

        ));

        $wp_customize->add_control(new \WP_Customize_Upload_Control($wp_customize, $name, array(
            'label'    => $label,
            'section'  => $section_id,
            'setting' => $name,
            'priority' => 20,
            "active_callback" => $callback,
        )));
    }

    public function customize_type_select($wp_customize, $label, $section_id, $name,  $default, $select_value,  $callback = null)
    {
        $wp_customize->add_setting($name, array(
            'default'     => $default,
            "transport" => "refresh",

        ));

        $wp_customize->add_control(new \WP_Customize_Control(
            $wp_customize,
            $name,
            array(
                "label" => $label,
                "section" => $section_id,
                "settings" => $name,
                "type" => "select",
                'choices'     => $select_value,
                "active_callback" => $callback,
            )
        ));
    }

    public function customize_type_radio($wp_customize, $label, $section_id, $name,  $default, $radio_value, $callback = null)
    {
        $wp_customize->add_setting($name, array(
            'default'     => $default,
            "transport" => "refresh",

        ));

        $wp_customize->add_control(new \WP_Customize_Control(
            $wp_customize,
            $name,
            array(
                "label" => $label,
                "section" => $section_id,
                "settings" => $name,
                "type" => "radio",
                'choices'     => $radio_value,
                "active_callback" => $callback,
            )
        ));
    }
}
