<?php

/**
 * Class that handle all the options in WordPress Theme Customize
 */
class Aidoo_Customize
{
    public function __construct()
    {
        add_action('customize_register', array($this, 'register'));
        add_action('customize_controls_enqueue_scripts', array($this, 'customize_control_js'));
        add_action('customize_preview_init', array($this, 'customize_preview_js'));
        add_action('customize_controls_print_styles', array($this, 'customize_style'));
    }

    public function register($wp_customize)
    {

        $customize_options = array();

        $customize_arr = new Aidoo_Customize_Options();
        $customize_options['footer'] = $customize_arr->footer();
        $customize_options['blog'] = $customize_arr->blog();
        $customize_options['color'] = $customize_arr->color();
        $customize_options['elements'] = $customize_arr->elements();
        $customize_options['header'] = $customize_arr->header();
        $customize_options['typo'] = $customize_arr->typo();
        $customize_options['pages'] = $customize_arr->pages();
        $customize_options['layout'] = $customize_arr->nblayout();

        include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
        if ( class_exists( 'WooCommerce' ) ) {
            $customize_options['shop'] = $customize_arr->woocommerce();
        }
        if ( class_exists( 'Nbdesigner_IO' ) ) {
            $customize_options['onlinedesign'] = $customize_arr->onlinedesign();
        }

        if ($wp_customize) {
            $wp_customize->remove_section('colors');
        }

        foreach($customize_options as $section => $define) {
            $options = isset($define['options']) ? $define['options'] : array();
            unset($define['options']);

            if(isset($define['sections'])) {
                $sections = $define['sections'];

                unset($define['sections']);

                if ($wp_customize) {
                    if($wp_customize->get_panel($section)) {
                        foreach($define as $k => $v) {
                            $wp_customize->get_panel($section)->$k = $v;
                        }

                        foreach($sections as $section_in_panel => $section_define) {
                            $wp_customize->add_section($section_in_panel, array_merge(array('panel' => $section), $define));
                        }
                    } else {
                        $wp_customize->add_panel($section, $define);

                        foreach($sections as $section_in_panel => $section_define) {
                            $wp_customize->add_section($section_in_panel, array_merge(array('panel' => $section), $section_define));
                        }
                    }
                }
            } else {
                if ($wp_customize) {
                    $wp_customize->add_section($section, $define);
                }
            }

            foreach($options as $option => $option_define) {
                $default_arr = aidoo_default_options();
                if(array_key_exists($option, $default_arr)) {
                    $default = $default_arr[$option];
                } else {
                    $default = '';
                }

                if ($wp_customize) {
                    $wp_customize->add_setting($option, array_merge(array('default' => $default, 'sanitize_callback' => null), $option_define['settings']));

                    if (isset($option_define['controls']['type']) && class_exists($class = $option_define['controls']['type'])) {

                        unset($option_define['controls']['type']);

                        $wp_customize->add_control(
                            new $class(
                                $wp_customize,
                                $option,
                                array_merge(array('section' => $section), $option_define['controls'])
                            )
                        );
                    } else {
                        $wp_customize->add_control($option, array_merge(array('section' => $section), $option_define['controls']));
                    }
                }
            }
        }
    }

    // Todo change to minified version
    public function customize_control_js()
    {
        //this is minified version
        wp_enqueue_script('nbcore-customizer-control', get_template_directory_uri() . '/assets/netbase/js/admin/control.min.js', array('customize-controls', 'jquery'), AIDOO_VER, true);
        wp_enqueue_script('nbcore-customize', get_template_directory_uri() . '/assets/netbase/js/admin/customize.min.js', array('jquery'), AIDOO_VER, true);

        wp_localize_script('nbcore-customizer-control', 'nbUpload', array(
            'google_fonts' => Aidoo_Helper::google_fonts(),
        ));

        $product_link   = $this->get_product_cutomize_link();
        $cat_link       = $this->get_blog_category_customize_link();
        $blog_link      = $this->get_single_product_customize_link();

        $nb_link = array();
        $nb_link['home'] = site_url();
        $nb_link['blog'] = $cat_link;
        $nb_link['od_user_template'] = site_url('designer/?id=1');
        
        if ( class_exists( 'WooCommerce' ) ) {
            $nb_link['shop']            = wc_get_page_permalink( 'shop' );
            $nb_link['checkout']        = wc_get_page_permalink( 'checkout' );
            $nb_link['cart']            = get_permalink(woocommerce_get_page_id('cart'));
            $nb_link['single_product']  = $blog_link;
        }

        wp_localize_script('nbcore-customizer-control', 'nbLink', $nb_link);
    }

    // Todo change to minified version
    public function customize_preview_js()
    {
        //this is minified version
        wp_enqueue_script('nbcore-customizer-preview', get_template_directory_uri() . '/assets/netbase/js/admin/preview.min.js', array('customize-preview', 'jquery'), AIDOO_VER, true);
    }

    // Todo change to minified version
    public function customize_style()
    {
        //this is minified version
        wp_enqueue_style('nbcore-customizer-style', get_template_directory_uri() . '/assets/netbase/css/admin/customizer/customizer.css', NULL, AIDOO_VER, 'all');
    }

    public function get_product_cutomize_link() {

        $args = array(
            'posts_per_page'   => 1,
            'post_type'        => 'product' ); 
    
        $first_product = get_posts( $args );

        $p_link = isset( $first_product[0] ) ? get_the_permalink( $first_product[0] ): '#';

        return $p_link;
    
    }

    public function get_blog_category_customize_link() {
        $categories = get_categories();
        
        $category_link = ! empty( $categories ) ? get_category_link($categories[0]->term_id) : '#';

        return $category_link;
    }

    public function get_single_product_customize_link() {

        $args = array(
            'posts_per_page'   => 1,
        ); 
    
        $first_blog = get_posts( $args );

        $b_link = isset( $first_blog[0] ) ? get_the_permalink( $first_blog[0] ): '#';

        return $b_link;
    }

}