<?php
/**
 * Module to render the vertical navbar
 * 
 * @since 0.8.5
 */

namespace Greyd\Navigation;

use \Greyd\Navigation\Render as Navigation;
use \processor as Processor;

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

new Vertical_Navbar();
class Vertical_Navbar {

    /**
     * Constructor
     */
    public function __construct() {

        // register customizer controls
        add_action( 'customize_register', array($this, 'add_customizer_settings'), 98 );
        add_action( 'get_header', array($this, 'add_process_defaults') );

        // render navbar
        add_filter( 'greyd_header_classes', array($this, 'add_header_class') );
        add_action( 'render_vertical_navbar', array($this, 'render_vertical_navbar') );
    }


    /**
     * =================================================================
     *                          CONTROLS
     * =================================================================
     */

    /**
     * Add controls & section to customizer
     */
    public function add_customizer_settings($wp_customize) {

        $wp_customize->add_section('section_navi_vertical', array(
            'title'    => __( 'Vertikale Menüleiste', 'core' ),
            'panel'    => 'panel_navigation',
        ) );
        
        // add processor settings
        $this->add_process_defaults(true);
    }

    /**
     * add controls to processor class var
     */
    public function add_process_defaults($all=false) {

        $settings = array();

        $settings[] = array(
            'sep' => array(
                'type' => 'separator', 'label' => 'Allgemein', 'description' => 'Die vertikale Menüleiste wird nur auf Desktopgeräten angezeigt (ab Header Breakpoint).'
            ),
            'type' => array(
                'type' => 'select', 'description' => 'Position', 'default' => 'none', 'transport' => 'refresh', 'choices' => array( 'none' => 'nicht aktivieren', 'left' => 'links', 'right' => 'rechts' )
            ),
        );

        $type = get_theme_mod('navi_vert_type', 'none');
        if ( $all || is_customize_preview() || $type == 'left' || $type == 'right' ) {

            // vars
            $menus = $this->get_nav_menu_options();

            $settings[] = array(
                '--color' => array(
                    'type' => 'colselect', 'description' => 'Hintergrundfarbe', 'default' => 'color21', 'transport' => 'refresh'
                ),
                // layout
                '--width' => array(
                    'type' => 'range', 'label' => 'Layout', 'lbl' => 'Breite:', 'default' => 100, 'unit' => 'px', 'input_attrs' => array( 'min' => 10, 'max' => 300, 'step'  => 1 )
                ),
                '--paddingy' => array(
                    'type' => 'range', 'lbl' => 'Padding oben/unten:', 'default' => 50, 'unit' => 'px', 'input_attrs' => array( 'min' => 0, 'max' => 200, 'step'  => 1 )
                ),
                // '--paddingx' => array(
                //     'type' => 'range', 'lbl' => 'Padding seitlich:', 'default' => 5, 'unit' => 'px', 'input_attrs' => array( 'min' => 0, 'max' => 100, 'step'  => 1 )
                // ),

                // burger
                'burger' => array(
                    'type' => 'separator', 'label' => 'Burger'
                ),
                'burger_enable' => array(
                    'type' => 'checkbox', 'label' => 'Burger Menü aktivieren?', 'default' => 1, 'transport' => 'refresh', 
                ),
                '--burger_color' => array(
                    'type' => 'colselect', 'description' => 'Farbe', 'default' => 'color23', 'transport' => 'refresh',
                    'dependency' => array( 'navi_vert_burger_enable' )
                ),
                '--burger_colorhv' => array(
                    'type' => 'colselect', 'description' => 'Hover', 'default' => 'color22', 'transport' => 'refresh',
                    'dependency' => array( 'navi_vert_burger_enable' )
                ),
                '--burger_size' => array(
                    'type' => 'range', 'lbl' => 'Größe:', 'default' => 0.7, 'input_attrs' => array( 'min' => 0, 'max' => 2, 'step'  => 0.01 ),
                    'dependency' => array( 'navi_vert_burger_enable' )
                ),
                '--burger_margin' => array(
                    'type' => 'range', 'lbl' => 'Abstand darunter:', 'default' => 20, 'unit' => 'px', 'input_attrs' => array( 'min' => 0, 'max' => 100, 'step'  => 1 ),
                    'dependency' => array( 'navi_vert_burger_enable' )
                ),
                'burger_style' => array(
                    'type' => 'select', 'description' => '<a href="https://jonsuh.com/hamburgers/" target="_blank">Stil</a>', 'default' => 'collapse', 'transport' => 'refresh',
                    'choices' => array( '3dx' => '3dx', '3dy' => '3dy', '3dxy' => '3dxy', 'arrow' => 'arrow', 'arrowalt' => 'arrowalt', 'arrowturn' => 'arrowturn', 'boring' => 'boring', 'collapse' => 'collapse', 'elastic' => 'elastic', 'emphatic' => 'emphatic', 'minus' => 'minus', 'slider' => 'slider', 'spin' => 'spin', 'spring' => 'spring', 'stand' => 'stand', 'squeeze' => 'squeeze', 'vortex' => 'vortex' ),
                    'dependency' => array( 'navi_vert_burger_enable' )
                ),

                // menus
                'menus' => array(
                    'type' => 'separator', 'label' => 'Menüs'
                ),
                'menu_top' => array(
                    'type' => 'select', 'description' => 'Oberes Menü:', 'default' => 'none', 'transport' => 'refresh', 'choices' => $menus
                ),
                'menu_middle' => array(
                    'type' => 'select', 'description' => 'Mittleres Menü:', 'default' => 'none', 'transport' => 'refresh', 'choices' => $menus
                ),
                'menu_bottom' => array(
                    'type' => 'select', 'description' => 'Unteres Menü:', 'default' => 'none', 'transport' => 'refresh', 'choices' => $menus
                ),

                // menu items
                'menuitems' => array(
                    'type' => 'separator', 'label' => 'Menüpunkte'
                ),
                '--font_color' => array(
                    'type' => 'colselect', 'description' => 'Farbe', 'default' => 'color23', 'transport' => 'refresh',
                ),
                '--font_colorhv' => array(
                    'type' => 'colselect', 'description' => 'Hover', 'default' => 'color22', 'transport' => 'refresh',
                ),
                '--font_family' => array(
                    'type' => 'ffselect', 'label' => 'Schrift', 'default' => 'fontFamily1', 'transport' => 'refresh',
                ),
                '--font_size' => array(
                    'type' => 'range', 'default' => 0.85, 'unit' => 'em',
                ),
                '--font_weight' => array(
                    'type' => 'select', 'default' => 400
                ),
                'font_group' => array(
                    'type' => 'group', 'label' => 'erweiterte Optionen', 'input_attrs' => array( 'children' => 4 ),
                ),
                '--line_height' => array(
                    'type' => 'range', 'default' => 140, 'unit' => '%'
                ),
                '--text_transform' => array(
                    'type' => 'select', 'default' => 'none',
                ),
                '--letter_spacing' => array(
                    'type' => 'range', 'default' => '0', 'unit' => 'px',
                ),
                '--word_spacing' => array(
                    'type' => 'range', 'default' => '0', 'unit' => 'px',
                ),
                '--between' => array(
                    'type' => 'range', 'label' => 'Größen & Abstände','lbl' => 'Abstand dazwischen:', 'default' => 10, 'unit' => 'px', 'input_attrs' => array( 'min' => 0, 'max' => 50, 'step'  => 1 ),
                ),

                // icons
                '--icon_size' => array(
                    'type' => 'range', 'label' => 'Icons','lbl' => 'Größe:', 'default' => 2, 'unit' => 'em', 'input_attrs' => array( 'min' => 0, 'max' => 5, 'step'  => 0.05 ),
                ),
                '--icon_margin' => array(
                    'type' => 'range', 'lbl' => 'Abstand darunter:', 'default' => 4, 'unit' => 'px', 'input_attrs' => array( 'min' => 0, 'max' => 20, 'step'  => 1 ),
                ),

                // overlap (section: offmenu)
                'offmenu_overlap' => array(
                    'section' => 'section_navi_header_offcanvas', 'priority' => -1,
                    'type' => 'checkbox', 'label' => 'überlappt vertikale Menüleiste', 'description' => 'Das Off-Menü wird über die gesamte Seitenbreite dargestellt.', 'default' => 0, 'transport' => 'refresh', 
                    'dependency' => array( array( 'element' => 'navi_header_off_type', 'value' => array('full', 'overlay') ) )
                ),
            );
        }

        // add settings to processor
        $all_settings = array();
        foreach ( (array) $settings as $controls) {
            foreach( $controls as $name => $args ) {

                // set key
                if ( strpos($name, '--') == 0 ) {
                    $name = str_replace('--', '', $name);
                    $key = '--HEDVERT'.$name;
                }
                else {
                    $key = 'HEDVERT'.$name;
                }

                // mod args
                $args['section']    = isset($args['section']) ? $args['section'] : 'section_navi_vertical';
                $args['mod']        = 'navi_vert_'.$name;
                if ( $name !== 'sep' && $name !== 'type' ) {
                    if ( isset($args['dependency']) ) {
                        $args['dependency'][] = array( 'element' => 'navi_vert_type', 'not_value' => 'none' );
                    }
                    else {
                        $args['dependency'] = array( array( 'element' => 'navi_vert_type', 'not_value' => 'none' ) );
                    }
                }

                // save in array
                $all_settings[$key] = $args;
            }
        }

        // debug( $all_settings );
        Processor::$root = array_merge( Processor::$root, $all_settings );
    }


    /**
     * =================================================================
     *                          FRONTEND
     * =================================================================
     */

    /**
     * add custom class to the header
     */
    public function add_header_class($classes) {

        $type = get_theme_mod('navi_vert_type', 'none');
        if ( $type === 'left' || $type === 'right' ) {
            $classes .= " nav_vert_$type";

            $off_overlap = get_theme_mod('navi_vert_offmenu_overlap', 0);
            if ( !$off_overlap ) $classes .= " vert_move_off";
        }
        return $classes;
    }

    /**
     * render the navbar
     */
    public function render_vertical_navbar() {
        
        $type = get_theme_mod('navi_vert_type', 'none');
        if ( $type !== 'left' && $type !== 'right' ) return "";

        echo "<div id='mainmenuvert' class='vertical_navbar $type'>";

        echo "<div>";

        $burgermode = get_theme_mod( 'navi_vert_burger_enable', 1 );
        if ($burgermode) {
            $burger = Navigation::add_headerburger( array(
                'burgerstyle' => get_theme_mod('navi_vert_burger_style', 'collapse'),
                'burgermode'  => 'on',
                'echo'        => false,
                'id'          => 'mainmenu',
            ) );
            echo '<div class="burger_wrapper on">'.$burger.'</div>';
        }

        $menutop = get_theme_mod('navi_vert_menu_top', 'none');
        if ( $menutop && $menutop !== 'none' ) {
            if ( wp_get_nav_menu_object($menutop) ) {
                wp_nav_menu( array(
                    'menu' => $menutop,
                    'fallback_cb' => false
                ) );
            }
        }

        echo "</div><div>";

        $menumiddle = get_theme_mod('navi_vert_menu_middle', 'none');
        if ( $menumiddle && $menumiddle !== 'none' ) {
            if ( wp_get_nav_menu_object($menumiddle) ) {
                wp_nav_menu( array(
                    'menu' => $menumiddle,
                    'fallback_cb' => false
                ) );
            }
        }

        echo "</div><div>";

        $menubottom = get_theme_mod('navi_vert_menu_bottom', 'none');
        if ( $menubottom && $menubottom !== 'none' ) {
            if ( wp_get_nav_menu_object($menubottom) ) {
                wp_nav_menu( array(
                    'menu' => $menubottom,
                    'fallback_cb' => false
                ) );
            }
        }

        echo "</div></div>";
    }


    /**
     * =================================================================
     *                          HELPER
     * =================================================================
     */

    /**
     * get all nav menu options
     */
    public function get_nav_menu_options() {
        $menus = array('none' => _x('kein Menü', 'small', 'core'));
        $terms = get_terms('nav_menu');
        if (is_array($terms)) {
            foreach($terms as $term) {
                $menus[$term->term_id] = $term->name;
            }
        }
        return $menus;
    }
}