<?php
/** 
 * @package WordPress
 * @subpackage Campoal
 */

/**
 * Add custom color css to enqueue style theme
 *
 * @since Campoal 1.0.0
 */
if( !function_exists('conikal_custom_style_css') ): 
    function conikal_custom_style_css() {
        global $campoal_options;
        global $post;

        // get appearance settings
        $container_width            = isset($campoal_options['conikal_container_width_field']) ? $campoal_options['conikal_container_width_field'] : '';
        $page_hero_bg_image         = isset($campoal_options['conikal_page_hero_bg_image_field']['url']) ? $campoal_options['conikal_page_hero_bg_image_field']['url'] : get_template_directory_uri().'/images/cover.svg';
        $petition_to_text           = isset($campoal_options['conikal_petition_to_text_field']) ? $campoal_options['conikal_petition_to_text_field'] : 1;

        // get homepage settings
        $caption_aligment           = isset($campoal_options['conikal_home_caption_alignment_field']) ? $campoal_options['conikal_home_caption_alignment_field'] : '';

        // home slider height
        $home_header                = isset($campoal_options['conikal_home_header_field']) ? $campoal_options['conikal_home_header_field'] : 'slideshow';
        $home_slideshow_images      = isset($campoal_options['conikal_home_slideshow_images']) ? $campoal_options['conikal_home_slideshow_images'] : '';
        $home_slideshow_hieght      = isset($campoal_options['conikal_hieght_slideshow_field']) ? $campoal_options['conikal_hieght_slideshow_field'] : '675';
        $home_header_hieght         = intval($home_slideshow_hieght) - 130;
        $home_header_hide_logined   = isset($campoal_options['conikal_home_header_hide_logined_field']) ? $campoal_options['conikal_home_header_hide_logined_field'] : 0;
        $home_caption_margin_top    = isset($campoal_options['conikal_home_caption_top_field']) ? $campoal_options['conikal_home_caption_top_field'] : '225';

        // home spotlight
        $home_spotlight  = isset($campoal_options['conikal_home_spotlight_field']) ? $campoal_options['conikal_home_spotlight_field'] : 0;
        $home_spotlight_hide_logined  = isset($campoal_options['conikal_home_spotlight_hide_logined_field']) ? $campoal_options['conikal_home_spotlight_hide_logined_field'] : 0;

        // hero section hieght
        $page_hero_height           = isset($campoal_options['conikal_page_hero_height_field']) ? $campoal_options['conikal_page_hero_height_field'] : '300px';
        $post_hero_height           = isset($campoal_options['conikal_post_hero_height_field']) ? $campoal_options['conikal_post_hero_height_field'] : '465px'; 

        // home spotlight
        $home_victory               = isset($campoal_options['conikal_home_victory_field']) ? $campoal_options['conikal_home_victory_field'] : 0;
        $home_victory_hide_logined  = isset($campoal_options['conikal_home_victory_hide_logined_field']) ? $campoal_options['conikal_home_victory_hide_logined_field'] : 0;
        
        // header menu
        $top_menu                   = isset($campoal_options['conikal_top_menu_field']) && !wp_is_mobile() ? $campoal_options['conikal_top_menu_field'] : 0;
        $type_menu                  = isset($campoal_options['conikal_type_header_menu_field']) ? $campoal_options['conikal_type_header_menu_field'] : 'none';
        $height_menu                = isset($campoal_options['conikal_height_header_menu_field']) ? $campoal_options['conikal_height_header_menu_field'] : '65px';
        $style_menu                 = isset($campoal_options['conikal_style_header_menu_field']) ? $campoal_options['conikal_style_header_menu_field'] : 'flat';
        $logo_height                = isset($campoal_options['conikal_header_logo_height_field']) ? $campoal_options['conikal_header_logo_height_field'] : '40px';

        // level badge image
        $level_badge_image          = isset($campoal_options['conikal_level_badge_image_field']['url']) ? $campoal_options['conikal_level_badge_image_field']['url'] : '';

        // decision maker badge image
        $verified_badge             = isset($campoal_options['conikal_decision_maker_verified_badge_field']['url']) ? $campoal_options['conikal_decision_maker_verified_badge_field']['url'] : get_template_directory_uri() . '/images/verified-badge.svg';

        // reCaptcha visibility
        $recaptcha_position         = isset($campoal_options['conikal_google_recaptcha_position_field']) ? $campoal_options['conikal_google_recaptcha_position_field'] : 'hidden';

        // get colors settings
        $main_color                 = isset($campoal_options['conikal_main_color_field']) ? $campoal_options['conikal_main_color_field'] : '#6059C9';

        $custom_style = '';
        if ($container_width != '' && !wp_is_mobile()) {
            $custom_style .= '.ui.container {width: ' . esc_attr($container_width) . 'px}';
        }

        if ($petition_to_text == 0) {
            $custom_style .= '
                .petition-to, 
                .letter-receiver {
                    display: none !important}
                .petition-list-card 
                .place {
                    display: block}';
        }

        // home hight slideshow
        $padding_top = $top_menu == 1 && $type_menu != 'none' ? (int)$height_menu + 40 : (int)$height_menu;

        $header_height_px = 0;
        $height_slideshow_px = 0;
        if ($home_header != 'none' && $home_header_hide_logined != 1) {
            if (!is_user_logged_in()) {
                $header_height_px = $home_header_hieght . 'px';
                $height_slideshow_px = $home_slideshow_hieght . 'px';
            } else if ($home_victory_hide_logined != 0) {
                $height_slideshow_px = '320px';
            }
        } elseif ($home_header != 'none') {
            $header_height_px = $home_header_hieght . 'px';
            $height_slideshow_px = $home_slideshow_hieght . 'px';
        } elseif (is_home()) {
            $header_height_px = $home_header_hieght . 'px';
            $height_slideshow_px = $home_slideshow_hieght . 'px';
        } else {
            if ( !is_user_logged_in() && $home_victory != 0 ) {
                $height_slideshow_px = '320px';
            } else if ($home_header == 'none' && $home_victory != 0 && $home_victory_hide_logined != 0) {
                $height_slideshow_px = '320px';
            } else if ($home_spotlight != 0) {
                $height_slideshow_px = '128px';
            }
        }

        if ($header_height_px != 0) {
            $custom_style .= '.home-feature.height {top: ' . esc_attr($header_height_px) . ' !important} ';
        }
        if ($height_slideshow_px != 0) {
            $custom_style .= '.slideshow-height {height: ' . esc_attr($height_slideshow_px) . ' !important}';
        }

        // home margin spotlioght
        $margin_spotlight = 'margin-top: 60px';
        if($home_victory == 1 && $home_victory_hide_logined != 1) {
            if (!is_user_logged_in()) {
                $margin_spotlight = '';
            }
        } elseif ($home_victory == 1) {
            $margin_spotlight = '';
        }

        if ($margin_spotlight != '') {
            $custom_style .= '.home-spotlight {' . esc_attr($margin_spotlight) . ' !important}';
        }

        // header menu
        if ($style_menu === 'flat') {
            $custom_style .= '
            .header-shadow, 
            .search-menu {
                border-bottom: .5px solid rgba(34,36,38,.08) !important; 
                box-shadow: none !important; }';
        } elseif ($style_menu === 'strong') {
            $custom_style .= '
            .header-shadow, 
            .search-menu {
                -webkit-box-shadow: 2.75px 7.52px 20px 0px rgba(0, 0, 0, 0.14) !important; 
                box-shadow: 2.75px 7.52px 20px 0px rgba(0, 0, 0, 0.14) !important}';
        } elseif ($style_menu === 'soft') {
            $custom_style .= '
            .header-shadow, 
            .search-menu {
                -webkit-box-shadow: 0 4px 12px 0 rgba(0,0,0,.05)!important; 
                box-shadow: 0 4px 12px 0 rgba(0,0,0,.05)!important; }';
        } else {
            $custom_style .= '
            .header-shadow, 
            .search-menu {
                -webkit-box-shadow: 0 0 2px 0 rgba(34,36,38,.1),0 2px 2px 0 rgba(34,36,38,.1)!important; 
                box-shadow: 0 0 2px 0 rgba(34,36,38,.1),0 2px 2px 0 rgba(34,36,38,.1)!important; }';
        }

        if ($height_menu != '') {
            $height_menu_unit = substr($height_menu, strlen((int)$height_menu));
            $custom_style .= '
            .header-menu, 
            .search-menu, 
            .home-menu, 
            .app-menu {
                height: ' . esc_attr((int)$height_menu) . esc_attr($height_menu_unit) . ' !important; }
            .nav-up {
                top: -' . esc_attr((int)$height_menu + 3) . esc_attr($height_menu_unit) . ' !important}';
            
            $custom_style .= '
            .editor-preview-side, 
            .CodeMirror-fullscreen, 
            .editor-toolbar.fullscreen {
                margin-top: ' . esc_attr($height_menu) . ' !important}';
            if ($type_menu != 'none') {
                $custom_style .= '
                    .wrapper.read.petition, 
                    .wrapper.read.update, 
                    .wrapper.petition, 
                    .wrapper.docs, 
                    .wrapper.woocommerce, 
                    .single-post-center, 
                    .single-post-simple, 
                    .ticket.type-ticket,
                    .woocommerce-cart,
                    .blog-feature-section {
                        padding-top: ' . esc_attr($padding_top - 2) . esc_attr($height_menu_unit) . ' !important; }';

                if ($top_menu == 1) {
                    $custom_style .= '
                    @media only screen and (max-width: 991px) {
                        .wrapper.read.petition, 
                        .wrapper.read.update, 
                        .wrapper.petition, 
                        .wrapper.docs, 
                        .wrapper.woocommerce, 
                        .single-post-center, 
                        .single-post-simple, 
                        .ticket.type-ticket,
                        .woocommerce-cart,
                        .blog-feature-section {
                            padding-top: ' . esc_attr($padding_top - 40) . esc_attr($height_menu_unit) . ' !important; }
                    }';
                }

                $custom_style .= '
                .wrapper.read.search, 
                .wrapper.search {
                    padding-top: ' . esc_attr($padding_top + 48) . esc_attr($height_menu_unit) . ' !important; }
                .search-menu-item {
                    padding-top: ' . esc_attr( ((int)$height_menu / 2) - 6 ) . esc_attr($height_menu_unit) . ' !important; }';
            } else if ($type_menu == 'none') {
                $custom_style .= '
                .wrapper.read.petition, 
                .wrapper.read.update, 
                .wrapper.petition, 
                .wrapper.docs, 
                .wrapper.woocommerce, 
                .single-post-center, 
                .single-post-simple,
                .blog-feature-section {
                    padding-top: 0 !important; }
                .sign-success {
                    padding-top: 10% !important; }
                .wrapper.read.search, 
                .wrapper.search {
                    padding-top: 24px !important; }';
            }
        }

        if ($top_menu == 1 && $type_menu != 'none') {
            $custom_style .= '
            @media only screen and (min-width: 992px) {
                .header-menu, 
                .search-menu, 
                .home-menu, 
                .app-menu {
                    margin-top: 40px !important; }
            }';
        }

        if ($logo_height) {
            $custom_style .= '
            .inverted-logo img, 
            .fixed-logo img, 
            .home-header .home-header-menu .logo img, 
            .blog-header .blog-header-menu .logo img {
                height: ' . esc_attr($logo_height) . ' !important; }';
        }

        // Right to Left
        if ( is_rtl() ) {
            $custom_style .= '
            .ui.toast-container .toast-box .ui.toast:not(.vertical)>.close.icon:not(.left) {
                right: unset;
                left: 1rem;
            }';
        }
        
        // home caption
        if (!empty($home_caption_margin_top)) {
            $custom_style .= '.home-caption {
                top: ' . esc_attr($home_caption_margin_top . 'px') . ' !important; }';
        }

        // One tap Google sign in
        $custom_style .= '#credential_picker_container {
            top: ' . esc_attr($padding_top) . 'px !important;
        }';
        

        // page hero background 
        if ((is_author() || is_page_template('my-petitions.php')) && is_user_logged_in()) {
            if (is_author()) {
                $curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
            } else {
                $curauth = wp_get_current_user();
            }

            $user_cover = get_user_meta($curauth->ID, 'user_cover', true);

            if ($user_cover) {
                $cover = $user_cover;
            } else {
                if($post) {
                    $cover = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large');
                    if ($cover) {
                        $cover = $cover[0];
                    } else {
                        $cover = $page_hero_bg_image;
                    }
                } else {
                    $cover = $page_hero_bg_image;
                } 
            }
        } elseif (is_tax('petition_category') || is_tax('petition_topics')) { 
            $term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
            if ($term->count) {
                $cover = get_term_meta($term->term_id, 'petition_category_image', true);
                if (!$cover) {
                    $cover = get_term_meta($term->term_id, 'petition_topics_image', true);
                }
                if(!$cover) {
                    $cover = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large');
                    if ($cover) {
                        $cover = $cover[0];
                    } else {
                        $cover = $page_hero_bg_image;
                    }
                }
            } else {
                $cover = $page_hero_bg_image;
            }
        } else {
            if ($post) {
                $cover = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large');
                if ($cover) {
                    $cover = $cover[0];
                } else {
                    $cover = $page_hero_bg_image;
                }
            } else {
                $cover = $page_hero_bg_image;
            } 
        }
        $custom_style .= '
        .page-hero-background, 
        .post-center-image {
            background-image:url(' . esc_url($cover) . ');}';

        // blog hero background
        $blog_cover = wp_get_attachment_image_src(get_post_thumbnail_id(get_queried_object_id()), 'large');
        if ($blog_cover) {
            $blog_cover = $blog_cover[0];
        } else {
            $blog_cover = $page_hero_bg_image;
        }
        $custom_style .= $blog_cover ? '
        .blog-hero-background {
            background-image:url(' . esc_url($blog_cover) . ');}' : '';

        // slideshow background image
        $home_slideshow_images = explode(',', $home_slideshow_images);
        $i = 0;
        foreach ($home_slideshow_images as $attachment_id) {
            $image_url = wp_get_attachment_image_src($attachment_id, 'full', true);
            $custom_style .= '
            .slideshow-background-image-' . esc_attr($i) . ' {background-image: url(' . esc_url($image_url[0]) . ')}';

            $i++;
        }

        // hero section height
        if ($page_hero_height != '') {
            $custom_style .= '
            .page-hero-container,
            .page-hero {
                height : ' . esc_attr($page_hero_height) . '; }';
        }

        // page hero height
        if ($post_hero_height != '') {
            $custom_style .= '
            .post-hero-container, 
            .post-hero {
                min-height : ' . esc_attr($post_hero_height) . '; }';

            $custom_style .= '
            .post-center-image {
                height : ' . esc_attr(wp_is_mobile() ? '300px' : $post_hero_height) . ' !important; }';
        }

        // page hero caption alignment
        if ($caption_aligment != '') {
            $custom_style .= '
            .home-caption {
                text-align: ' . esc_attr($caption_aligment) . '}';
        }

        // level badge image css
        if ($level_badge_image != '') {
            $custom_style .= '
            .level-badge::before, 
            .level-badge.large::before { 
                background-image: url(' . esc_url($level_badge_image) . '); 
                background-repeat: no-repeat; background-size: cover; }';

        } else if( $main_color != '' ) {
            $custom_style .= '
            .level-badge::before, 
            .level-badge.large::before { 
                background-color: ' . esc_attr($main_color) . ' !important; }';
        }

        // level badge image css
        if ($verified_badge != '') {
            $custom_style .= '
            .decision-maker-verified::before, 
            .decision-maker-verified.large::before { 
                background-image: url(' . esc_url($verified_badge) . '); 
                background-repeat: no-repeat; 
                background-size: cover; }';

        } else if($main_color != '') {
            $custom_style .= '
            .decision-maker-verified::before, 
            .decision-maker-verified.large::before { 
                background-color: ' . esc_attr($main_color) . ' !important; }';
        }

        // Hidde reCaptcha Badge
        if ($recaptcha_position == 'hidden') {
            $custom_style .= '
            .grecaptcha-badge { 
                visibility: hidden; }';
        }

        $custom_style .= conikal_custom_colors_css();
        $custom_style .= conikal_custom_border_css();
        $custom_style .= conikal_custom_typography_css();

        return $custom_style;
    }
endif;

?>