<?php
/**
 * Created by PhpStorm.
 * User: Extend Studio
 * Date: 2/19/2019
 * Time: 6:21 PM
 */

namespace ColibriWP\Theme\Components\Header;

use ColibriWP\Theme\Core\ComponentBase;
use ColibriWP\Theme\Core\Hooks;
use ColibriWP\Theme\Core\Utils;
use ColibriWP\Theme\Defaults;
use ColibriWP\Theme\Translations;
use ColibriWP\Theme\View;
use WP_Customize_Manager;
use WP_Customize_Setting;


class Logo extends ComponentBase {

    protected static $settings_prefix = "header_front_page.logo.";

    public static function rearrangeControls( $wp_customize ) {

        $prefix = static::$settings_prefix;

        $controls       = array( 'blogname', 'custom_logo', );
        $priority_start = 20;

        foreach ( $controls as $index => $control ) {
            /** @var WP_Customize_Manager $wp_customize */
            $instance = $wp_customize->get_control( $control );

            if ( $instance ) {
                $instance->section             = "{$prefix}section";
                $instance->json['colibri_tab'] = "content";
                $instance->priority            = ( $priority_start + $index * 5 );

                $active_rule_value = "text";

                if ( $control == 'custom_logo' ) {
                    $active_rule_value = "image";

                }

                /** @var WP_Customize_Setting $setting */
                $setting                        = $instance->setting;
                $setting->transport             = 'postMessage';
                $instance->json['active_rules'] = array(
                    array(
                        "setting"  => "{$prefix}props.layoutType",
                        "operator" => "=",
                        "value"    => $active_rule_value,
                    ),
                );
            }

            if ( $wp_customize->selective_refresh ) {
                $id                = static::selectiveRefreshSelector();
                $partial           = $wp_customize->selective_refresh->get_partial( Utils::slugify( $id ) );
                $partial->settings = array_merge(
                    $partial->settings,
                    $controls
                );
            }
        }
    }

    public static function selectiveRefreshSelector() {
        $selector = Defaults::get( static::$settings_prefix . 'selective_selector', false );

        return $selector;
    }

    /**
     * @return array();
     */
    protected static function getOptions() {
        Hooks::prefixed_add_action( 'rearrange_customizer_components', array( __CLASS__, "rearrangeControls" ) );

        $prefix = static::$settings_prefix;

        $custom_logo_args = get_theme_support( 'custom-logo' );

        return array(
            "sections" => array(
                "{$prefix}section" => array(
                    'title'  => Translations::get( 'logo' ),
                    'panel'  => 'header_panel',
                    'type'   => 'colibri_section',
                    'hidden' => true
                )
            ),

            "settings" => array(

                "alternate_logo" => array(
                    'default' => Defaults::get( "dark_logo", "" ),
                    'control' => array(
                        'label'       => Translations::escHtml( "alternate_logo_image" ),
                        'type'        => 'cropped_image',
                        'section'     => "{$prefix}section",
                        'priority'    => 35,
                        'colibri_tab' => "content",

                        'height'      => Utils::pathGet( $custom_logo_args, '0.height', false ),
                        'width'       => Utils::pathGet( $custom_logo_args, '0.width', false ),
                        'flex_height' => Utils::pathGet( $custom_logo_args, '0.flex-height', false ),
                        'flex_width'  => Utils::pathGet( $custom_logo_args, '0.flex-width', false ),

                        'active_rules' => array(
                            array(
                                "setting"  => "{$prefix}props.layoutType",
                                "operator" => "=",
                                "value"    => "image",
                            ),
                        )
                    ),

                ),

                "{$prefix}props.layoutType" => array(
                    'default' => Defaults::get( "{$prefix}props.layoutType" ),
                    'control' => array(
                        'label'       => Translations::get( 'layout_type' ),
                        'focus_alias' => "logo",
                        'type'        => 'select',
                        'section'     => "{$prefix}section",
                        'colibri_tab' => "content",
                        'choices'     => array(
                            'image' => Translations::escHtml( "logo_image_only" ),
                            'text'  => Translations::escHtml( "site_title_text_only" ),/*
							'image_text_v' => Translations::escHtml( "image_with_text_below" ),
							'image_text_h'    => Translations::escHtml( "image_with_text_right" ),
							'text_image_v'    => Translations::escHtml( "image_with_text_above" ),
							'text_image_h'    => Translations::escHtml( "image_with_text_left" ),*/
                        ),
                    ),
                ),
            ),
        );
    }

    public function getPenPosition() {
        return static::PEN_ON_RIGHT;
    }

    public function renderContent() {
        View::partial( 'front-header', 'logo', array(
            "component" => $this,
        ) );
    }

    public function printTextLogo() {

        if ( $this->getLayoutType() == 'text' ) {
            echo sprintf( '<a class="text-logo" data-type="group" data-dynamic-mod="true" href="%1$s">%2$s</a>',
                $this->getHomeurl(), get_bloginfo( 'name' ) );
        }
    }

    public function getLayoutType() {
        $prefix = static::$settings_prefix;

        return $this->mod( "{$prefix}props.layoutType" );
    }

    public function getHomeUrl() {
        return esc_url( home_url( '/' ) );
    }

    public function printImageLogo( $class = '' ) {

        $class = $class ? "{$class}-image" : '';

        if ( $this->getLayoutType() == 'image' ) : ?>
            <a href="<?php echo $this->getHomeUrl(); ?>" class="d-flex align-items-center">
                <img src="<?php echo $this->customLogoUrl(); ?>"
                     class="h-logo__image h-logo__image_h logo-image <?php echo esc_attr( $class ); ?>"/>
                <img src="<?php echo $this->alternateLogoUrl(); ?>"
                     class="h-logo__alt-image h-logo__alt-image_h logo-alt-image <?php echo esc_attr( $class ); ?>"/>
            </a>
        <?php endif;
    }

    public function customLogoUrl() {
        $custom_logo_id = get_theme_mod( 'custom_logo', - 1 );

        if ( $custom_logo_id == - 1 || empty( $custom_logo_id ) ) {
            $placeholder =
                'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAAEH5aXCAAAAAXNSR0IArs4c6QAAFIxJREFUeAHtXQl4VNW9PzOZzGQmrGEPymbZyvJEEBAVEUGq7AUiPEQtr+99fbW1LVb72b5au2Ft+1WktmqtbX36WBICCaAQQPZ9C4sIQSBhXyIkQGZf7vv9751zc+/MnZk7mUky4ZvzfTPn3HP+53/+y1nuvf/zP5exZARBI0zd5vqdJu4h65xzNOBVWWEVVaURLs47AgJVNNAfwVCsJxgQjHoAlTDAb5MrmfLtjH4U5uxxy+kXD3nEvKM3ZWLO6SLvolNgna0iKCF4TVclsangn8xT7krHfmVBrLTcph4JUiuEUBYEZdz2RW6DV4gIMWO70OWmV7iO1n8REajOBcC6jfgKDUqEugXAeYlLN1QprgpEmrHaI3iVNMZKG1tmMhMB3bXKKcO+dNgjdkzqqPOPe1kV+ibvtAaSCIekxDWXwDpkybLgRXJsbFZg78SvCCwaMMHFxTQJSexTXFq8Ja3Y4ZdyVZ1QC5Dy/ueo53y2SVKBJkyFXSiELMo1C/VkQuSjtlX6vaF97I3jnjPmfPeAWDgiylypolhIlOVaMpRlRYAtVwutObXKivGko9ZHoeaw45XqEpuXuQfKBBZXCs3rg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sdkeOAB7AWCe7CRNf3S8OS3qtIb84d8kt+Wh613sYJWfTcg1saKHLPKiHkr38z0zxfYo/5/lPvrwqbiukZDpBPG/D7Uwpe95DWj+EU4IWIrRCKdhcfSQb/vv7zWzUVAsD3pwcdhI8ZQd7lMlV9k417SsMwSjUkjXVfaj+8fa+rfVfMsSCWU6P0EJzMJziDxCatUNrDiaahiUEfuWJUEK0tUlCRRe9Ptx6pzqMy6qEULf6+1pdu3eNzarV1poDSKB1zE6fqpsSTGL46t/MMbvE4R7AVCFX3riUkoqyek1l/0enBwQto9LNWUF23SN3uxemaIurEkWS+Ohe6JTxuzKKbawjyKFKQRDSNj0aFYeDrB4r/HIvbNbfvNL37qcAu0Ph6jWEKUYsouFDsuH+Ysf75AxTJmfTicsgXXo9OMiYYmoEF4BzybbkH6IX6fjhCSwHcp4OBqGsCkrFDiIID80P32tXwL0qPvmSd9u63LX07FqxRwhhIBuhx9t6V6MJ9pxWFvSIX4JvIiO/af4q8WogemrG37k65wOOiTwHj7EAB/xf8UQa2LF1nxnl+8f9GzGKXnpEF0CCxKTdJy1B5e4++CgoYPpfY1hWqGPATTe91LQeC5+n4cemxhG5h2egSMpT4zbqvNb7XF2/jqBt5J8zZPylfumoju4dgvvn/bibVMjjgg92ur1qWML7Ah37OJ/wRmohhKm2ArtnfTII2VgQLQJX2ldeuCGv94/OlSfo4o+T41AH7Ccl13oGpMyAk6UEDA0etxmZyVxl+qhUvo84DbQOdpU6ByZKO9Nov6Ere6X55V6toPpRp3e6GxnfO+JRvG7+A3BkRoDm4QA65tIc7776wNL3P0hFNquP/+5va5LODwbS2fiAZsWCAnh2oW9VQs647wVpBvv9rS+hdkQ+DuUCNmD9wvyPqPMpfa57fIF8cBV01L7z5U0mPJrvqG8TqfTEqiTBP4f/Nu7m4GGni8AAAAASUVORK5CYII=';

            return $placeholder;
        }

        return esc_url( wp_get_attachment_image_url( $custom_logo_id, 'full' ) );
    }

    public function alternateLogoUrl() {
        $alternate_logo_id = get_theme_mod( 'alternate_logo', - 1 );

        if ( $alternate_logo_id == - 1 || empty( $alternate_logo_id ) ) {
            return $this->customLogoUrl();
        }
        if ( is_numeric( $alternate_logo_id ) ) {
            return esc_url( wp_get_attachment_image_url( $alternate_logo_id, 'full' ) );
        } else {
            return esc_url( $alternate_logo_id );
        }
    }
}
