<?php

if(!defined('ABSPATH'))
    exit;

class SyrusCustomizer {

    public function initialize($wp_customize) {
        global $syrus;
        $current_layout = $syrus->get_current_layout();

        if($current_layout == 1) {
            $this->add_sections($wp_customize);
    
            $this->registra_impostazioni_motto($wp_customize);
    
            $this->registra_impostazioni_header_logo($wp_customize);
          
            $this->registra_impostazioni_header_menu($wp_customize);
          
            $this->registra_impostazioni_footer($wp_customize);
          
            $this->registra_impostazioni_error_404($wp_customize);
          
            $this->registra_impostazioni_generali($wp_customize);
          
            $this->registra_impostazioni_homepage($wp_customize);
        } 

        if($current_layout == 2) {
            $this->add_sections_layout_2($wp_customize);

            $this->registra_impostazioni_colori_layout_2($wp_customize);

            $this->registra_impostazioni_footer_layout_2($wp_customize);
            $this->registra_impostazioni_footer_menu_layout_2($wp_customize);
        }

    }

    private function add_sections($wp_customize) {
        $wp_customize->add_section('sezione-topnav',[
            'title' => __("Header settings",'syrus'),
            'description' => __("Header section",'syrus')
        ]);
        
        $wp_customize->add_section('sezione-footer',[
            'title' => __("Footer settings",'syrus'),
            'description' => __("Footer section",'syrus')
        ]);
    
        $wp_customize->add_section('sezione-error-404',[
            'title' => __("404 page settings",'syrus'),
            'description' => __("404 page section",'syrus')
        ]);
    
        $wp_customize->add_section('sezione-generali',[
            'title' => __("Theme settings",'syrus'),
            'description' => __("Theme settings",'syrus')
        ]);
    }

    private function add_sections_layout_2($wp_customize) {
        
        $wp_customize->add_section('sezione-footer',[
            'title' => __("Footer settings",'syrus'),
            'description' => __("Footer section",'syrus')
        ]);
    
        $wp_customize->add_section('sezione-generali',[
            'title' => __("Theme settings",'syrus'),
            'description' => __("Theme settings",'syrus')
        ]);
    }

    private function registra_impostazioni_header_menu($wp_customize){

        $wp_customize->add_setting('bg-color-main-menu',[
          'type' => "theme_mod",
          'default' => "#333743",
          'sanitize_callback' => 'sanitize_text_field',
        ]);
      
        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'button-bg-color-main-menu',[
          'description' => "Seleziona il colore di sfondo del menu principale",
          'section' => "colors",
          'settings' => "bg-color-main-menu"
        ]));
      
        $wp_customize->add_setting('text-color-main-menu',[
          'type' => "theme_mod",
          'default' => "#cedce9",
          'sanitize_callback' => 'sanitize_text_field',
        ]);
      
        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'button-text-color-main-menu',[
          'description' => "Seleziona il colore del testo del menu principale",
          'section' => "colors",
          'settings' => "text-color-main-menu"
        ]));
      
        $wp_customize->add_setting('bg-color-after-main-menu',[
          'type' => "theme_mod",
          'default' => "orange",
          'sanitize_callback' => 'sanitize_text_field',
        ]);
      
        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'bg-color-after-main-menu',[
          'description' => "Seleziona il colore della sottolineatura delle voci di menu",
          'section' => "colors",
          'settings' => "bg-color-after-main-menu"
        ]));
      
        $wp_customize->add_setting('reading-time-progressbar-color',[
          'type' => "theme_mod",
          'default' => "#0077df",
          'sanitize_callback' => 'sanitize_text_field',
        ]);
      
        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'bg-color-after-main-menu',[
          'description' => __("Select color for the reading time progress bar on the post",'syrus'),
          'section' => "colors",
          'settings' => "reading-time-progressbar-color"
        ]));
      
    }
      
    private function registra_impostazioni_header_logo($wp_customize){
        
        $wp_customize->add_setting('bg-color-logo',[
            'type' => "theme_mod",
            'default' => "#FFFFFF",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'button-bg-color-logo',[
            'description' => "Seleziona il colore di sfondo del logo",
            'section' => "colors",
            'settings' => "bg-color-logo"
        ]));
        
        
    }
    
    private function registra_impostazioni_motto($wp_customize){
    
        $wp_customize->add_setting('background-color-motto',[
            'type' => "theme_mod",
            'default' => "#e5e8ef",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'button-bg-color-motto',[
            // 'label' => "Colore sfondo motto",
            'description' => "Seleziona il colore di sfondo del motto",
            'section' => "colors",
            'settings' => "background-color-motto"
        ]));
        
        $wp_customize->add_setting('text-color-motto',[
            'type' => "theme_mod",
            'default' => "#000000",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'button-text-color-motto',[
            'description' => "Seleziona il colore del testo del motto",
            'section' => "colors",
            'settings' => "text-color-motto"
        ]));
    }
    
    private function registra_impostazioni_footer($wp_customize){
        $wp_customize->add_setting('bg-color-footer',[
            'type' => "theme_mod",
            'default' => "#1e1e1e",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'button-bg-color-footer',[
            'label' => "Impostazioni footer",
            'description' => "Seleziona il colore di sfondo del footer",
            'section' => "colors",
            'settings' => "bg-color-footer"
        ]));
        
        
        $wp_customize->add_setting('text-color-footer',[
            'type' => "theme_mod",
            'default' => "#ffffff",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'button-text-color-footer',[
            'description' => "Seleziona il colore del testo del footer",
            'section' => "colors",
            'settings' => "text-color-footer"
        ]));
        
        
        
        $wp_customize->add_setting('text-copyright',[
            'type' => "theme_mod",
            'default' => "Syrus Industry Srl",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('text-copyright',[
            'description' => "Inserisci il testo del copyright",
            'section' => "sezione-footer",
            'type' => 'text'
        ]);
        
        $wp_customize->add_setting('copyright-link',[
            'type' => "theme_mod",
            'default' => "",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('copyright-link',[
            'description' => "Inserisci il link del copyright",
            'section' => "sezione-footer",
            'type' => 'text',
            'input_attrs' => [
            'placeholder' => "Ex. ".home_url(),
            ]
        ]);
        
        $testi_footer = self::get_impostazioni_testi_footer();
        
        foreach($testi_footer as $testo){
            $wp_customize->add_setting($testo->handle,[
            'type' => "theme_mod",
            'default' => "$testo->default",
            'sanitize_callback' => 'sanitize_text_field',
            ]);
        
            $wp_customize->add_control($testo->handle,[
            'description' => $testo->desc,
            'section' => "sezione-footer",
            'type' => 'text'
            ]);
        }
        
        $wp_customize->add_setting('text-privacy',[
            'type' => "theme_mod",
            'default' => "Privacy Policy",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('text-privacy',[
            'description' => "Inserisci il testo della privacy",
            'section' => "sezione-footer",
            'type' => 'text'
        ]);
        
        $wp_customize->add_setting('privacy-link',[
            'type' => "theme_mod",
            'default' => "",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('privacy-link',[
            'description' => "Inserisci il link delle privacy policy",
            'section' => "sezione-footer",
            'type' => 'text',
            'input_attrs' => [
            'placeholder' => "Ex. ".home_url()."/privacy-policy",
            ]
        ]);
    
    }
    
    private function registra_impostazioni_error_404($wp_customize){
    
        $wp_customize->add_setting('error-title',[
            'type' => "theme_mod",
            'default' => "Oops! That page can’t be found.",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('error-title',[
            'description' => "Inserisci il testo del titolo",
            'section' => "sezione-error-404",
            'type' => 'text'
        ]);
        
        $wp_customize->add_setting('error-description',[
            'type' => "theme_mod",
            'default' => "It looks like nothing was found at this location. Maybe try a search?",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('error-description',[
            'description' => "Inserisci il testo della descrizione",
            'section' => "sezione-error-404",
            'type' => 'text'
        ]);
        
        $wp_customize->add_setting('use-static-page-404',[
            'default'   => 'no',
            'transport' => 'refresh',
            'sanitize_callback' => 'sanitize_text_field',
        ]);

        $wp_customize->add_control('use-static-page-404',[
            'label'    => __('Usa pagina statica?', 'syrus'),
            'section'  => 'sezione-error-404',
            'settings' => 'use-static-page-404',
            'type'     => 'radio',
            'choices'  => [
                'yes' => __('Sì', 'syrus'),
                'no'  => __('No', 'syrus'),
            ],
        ]);

        $wp_customize->add_setting('static-page-404',[
            'default'   => '',
            'transport' => 'refresh',
            'sanitize_callback' => 'sanitize_text_field',
        ]);

        $wp_customize->add_control('static-page-404',[
            'label'    => __('Scegli una pagina da mostrare al posto della 404', 'syrus'),
            'section'  => 'sezione-error-404',
            'settings' => 'static-page-404',
            'type'     => 'dropdown-pages',
        ]);
        
        $wp_customize->add_control('btn-test-404',[
            'type' => 'button',
            'settings' => [],
            'priority' => 10,
            'section' => 'sezione-error-404',
            'input_attrs' => [
                'value' => "Visualizza la pagina 404",
                'class' => 'button button-primary btn-customizer',
                'onclick' => "jQuery('#customize-preview').find('iframe').attr('src','".home_url()."/test-404/?customize_theme=syrus-theme&customize_messenger_channel=preview-2')",
            ],
        ]);
    }
    
    private function registra_impostazioni_generali($wp_customize){
        $wp_customize->add_setting('general-font-size',[
            'type' => "theme_mod",
            'default' => "small",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('general-font-size',[
            'description' => "Seleziona la grandezza del font",
            'section' => "sezione-generali",
            'type' => 'select',
            'choices' => array(
            'small' => "Piccolo",
            'medium' => "Medio",
            'large' => "Grande",
            ),
        ]);
        
        $wp_customize->add_setting('reading-time-progressbar',[
            'type' => "theme_mod",
            'default' => "yes",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('reading-time-progressbar',[
            'description' => __("Show reading time progress bar on post",'syrus'),
            'section' => "sezione-generali",
            'type' => 'select',
            'choices' => array(
            'yes' => "yes",
            'no' => "no",
            ),
        ]);

        $wp_customize->add_setting('show-sidebar-homepage',[
            'type' => "theme_mod",
            'default' => "1",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('show-sidebar-homepage',[
            'label' => __("Show sidebar on home page",'syrus'),
            'section' => "sezione-generali",
            'type' => 'checkbox',
        ]);

        $wp_customize->add_setting('show-sidebar-single-post',[
            'type' => "theme_mod",
            'default' => "1",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('show-sidebar-single-post',[
            'label' => __("Show sidebar on single post",'syrus'),
            'section' => "sezione-generali",
            'type' => 'checkbox',
        ]);

        $wp_customize->add_setting('show-sidebar-single-page',[
            'type' => "theme_mod",
            'default' => "1",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('show-sidebar-single-page',[
            'label' => __("Show sidebar on single page",'syrus'),
            'section' => "sezione-generali",
            'type' => 'checkbox',
        ]);

        
        
        $wp_customize->add_setting('color-underline-title-article',[
            'type' => "theme_mod",
            'default' => "#ffa700",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'color-underline-title-article',[
            // 'label' => "Colore sfondo motto",
            'description' => "Seleziona il colore della sottolineatura del titolo dell'articolo",
            'section' => "colors",
            'settings' => "color-underline-title-article"
        ]));
    }
      
    private function registra_impostazioni_homepage($wp_customize) {
        $wp_customize->add_setting('reading-time',[
            'type' => "theme_mod",
            'default' => "yes",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('reading-time',[
            'label' => __("Post reading time",'syrus'),
            'section' => "static_front_page",
            'type' => 'select',
            'choices' => array(
            'yes' => "yes",
            'no' => "no",
            ),
        ]);
    }

    private function registra_impostazioni_colori_layout_2($wp_customize) {

        // Setting: Main Menu Color
        $wp_customize->add_setting('main-menu-color',[
            'type' => "theme_mod",
            'default' => "#ffffff",
            'sanitize_callback' => 'sanitize_text_field',
        ]);

        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'main-menu-color',[
            'label' => __("Main Menu Color",'syrus'),
            'section' => "colors",
            'settings' => "main-menu-color"
        ]));

        // Setting: Main Menu Hover Color
        $wp_customize->add_setting('main-menu-hover-color',[
            'type' => "theme_mod",
            'default' => "#feb200",
            'sanitize_callback' => 'sanitize_text_field',
        ]);

        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'main-menu-hover-color',[
            'label' => __("Main Menu Hover Color",'syrus'),
            'section' => "colors",
            'settings' => "main-menu-hover-color"
        ]));

        // Setting: Site Title Color
        $wp_customize->add_setting('site-title-color',[
            'type' => "theme_mod",
            'default' => "#feb200",
            'sanitize_callback' => 'sanitize_text_field',
        ]);

        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'site-title-color',[
            'label' => __("Site title Color",'syrus'),
            'section' => "colors",
            'settings' => "site-title-color"
        ]));

        // Setting: Tagline Color
        $wp_customize->add_setting('tagline-color',[
            'type' => "theme_mod",
            'default' => "#ffffff",
            'sanitize_callback' => 'sanitize_text_field',
        ]);

        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'tagline-color',[
            'label' => __("Tagline Color",'syrus'),
            'section' => "colors",
            'settings' => "tagline-color"
        ]));

        // Setting: Home Page Banner Background Color
        $wp_customize->add_setting('home-page-banner-bg-color',[
            'type' => "theme_mod",
            'default' => "#000000",
            'sanitize_callback' => 'sanitize_text_field',
        ]);

        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'home-page-banner-bg-color',[
            'label' => __("Home Page Banner Background Color",'syrus'),
            'section' => "colors",
            'settings' => "home-page-banner-bg-color"
        ]));

        // Setting: Home Page Banner Color
        $wp_customize->add_setting('home-page-banner-color',[
            'type' => "theme_mod",
            'default' => "#000000",
            'sanitize_callback' => 'sanitize_text_field',
        ]);

        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'home-page-banner-color',[
            'label' => __("Home Page Banner Color",'syrus'),
            'section' => "colors",
            'settings' => "home-page-banner-color"
        ]));
    }

    private function registra_impostazioni_footer_layout_2($wp_customize){
        $wp_customize->add_setting('bg-color-footer',[
            'type' => "theme_mod",
            'default' => "#1e1e1e",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'button-bg-color-footer',[
            'label' => "Impostazioni footer",
            'description' => "Seleziona il colore di sfondo del footer",
            'section' => "colors",
            'settings' => "bg-color-footer"
        ]));
        
        
        $wp_customize->add_setting('text-color-footer',[
            'type' => "theme_mod",
            'default' => "#ffffff",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'button-text-color-footer',[
            'description' => "Seleziona il colore del testo del footer",
            'section' => "colors",
            'settings' => "text-color-footer"
        ]));
        
        
        
        $wp_customize->add_setting('text-copyright',[
            'type' => "theme_mod",
            'default' => "Syrus Industry Srl",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('text-copyright',[
            'description' => "Inserisci il testo del copyright",
            'section' => "sezione-footer",
            'type' => 'text'
        ]);
        
        $wp_customize->add_setting('copyright-link',[
            'type' => "theme_mod",
            'default' => "",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('copyright-link',[
            'description' => "Inserisci il link del copyright",
            'section' => "sezione-footer",
            'type' => 'text',
            'input_attrs' => [
            'placeholder' => "Ex. ".home_url(),
            ]
        ]);
        
        $testi_footer = self::get_impostazioni_testi_footer();
        
        foreach($testi_footer as $testo){
            $wp_customize->add_setting($testo->handle,[
            'type' => "theme_mod",
            'default' => "$testo->default",
            'sanitize_callback' => 'sanitize_text_field',
            ]);
        
            $wp_customize->add_control($testo->handle,[
            'description' => $testo->desc,
            'section' => "sezione-footer",
            'type' => 'text'
            ]);
        }
        
        $wp_customize->add_setting('text-privacy',[
            'type' => "theme_mod",
            'default' => "Privacy Policy",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('text-privacy',[
            'description' => "Inserisci il testo della privacy",
            'section' => "sezione-footer",
            'type' => 'text'
        ]);
        
        $wp_customize->add_setting('privacy-link',[
            'type' => "theme_mod",
            'default' => "",
            'sanitize_callback' => 'sanitize_text_field',
        ]);
        
        $wp_customize->add_control('privacy-link',[
            'description' => "Inserisci il link delle privacy policy",
            'section' => "sezione-footer",
            'type' => 'text',
            'input_attrs' => [
            'placeholder' => "Ex. ".home_url()."/privacy-policy",
            ]
        ]);
    
    }

    private function registra_impostazioni_footer_menu_layout_2($wp_customize){
      
        $wp_customize->add_setting('text-color-footer-menu',[
          'type' => "theme_mod",
          'default' => "#cedce9",
          'sanitize_callback' => 'sanitize_text_field',
        ]);
      
        $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize,'button-text-color-footer-menu',[
          'description' => "Seleziona il colore del testo del menu del footer",
          'section' => "colors",
          'settings' => "text-color-footer-menu"
        ]));
      
      }

    public static function get_impostazioni_testi_footer(){
        $defaults = [
          "Via Tal dei Tali",
          "P.IVA 123456789 - NUMERO REA XX123456",
          "info@example.com"
        ];
      
        $testi = [];
      
        foreach(range(1,3) as $i){
          $testi[] = (object) array(
            'handle' => "footer-testo-$i",
            'class' => "testo-$i",
            'desc' => "Inserisci testo #$i",
            'default' => $defaults[$i - 1],
          );
        }
      
        return $testi;
    }




}