<?php

/**
 * Class td_block_delete_bookmarks
 */

class td_block_delete_bookmarks extends td_block {

    function __construct() {
        parent::disable_loop_block_features();
    }



	public function get_custom_css() {

        // $unique_block_class
        $in_composer = td_util::tdc_is_live_editor_iframe() || td_util::tdc_is_live_editor_ajax();
        $in_element = td_global::get_in_element();
        $unique_block_class_prefix = '';
        if( $in_element || $in_composer ) {
            $unique_block_class_prefix = 'tdc-row .';

            if( $in_element && $in_composer ) {
                $unique_block_class_prefix = 'tdc-row-composer .';
            }
        }
        $unique_block_class = $unique_block_class_prefix . $this->block_uid;

        $compiled_css = '';

		/** @noinspection CssInvalidAtRule */
		$raw_css =
            "<style>

                /* @style_general_td_block_delete_bookmarks */
                .td_block_delete_bookmarks .td_block_inner {
                    display: flex;
                    flex-direction: column;
                }
                .td_block_delete_bookmarks .td-del-book-btn {
                    position: relative;
                    display: flex;
                    align-items: center;
                    background-color: var(--td_theme_color, #4db2ec);
                    padding: 14px 24px 15px;
                    font-size: 13px;
                    line-height: 1;
                    color: #fff;
                    border: 0;
                    outline: none;
                    -webkit-appearance: none;
                    -webkit-transition: all 0.3s ease;
                    transition: all 0.3s ease;
                    transform: translateZ(0);
                }
                .td_block_delete_bookmarks .td-del-book-btn:before {
                    content: '';
                    position: absolute;
                    top: 0;
                    left: 0;
                    width: 100%;
                    height: 100%;
                    background-color: #222;
                    opacity: 0;
                    transition: opacity 0.3s ease;
                    z-index: -1;
                }
                .td_block_delete_bookmarks .td-del-book-btn:hover:before {
                    opacity: 1;
                }
                .td_block_delete_bookmarks .td-del-book-btn[disabled] {
                    opacity: .7;
                    pointer-events: none;
                }
                .td_block_delete_bookmarks .td-del-book-icon {
                    position: relative;
                }
                .td_block_delete_bookmarks i {
                    font-size: 15px;
                }
                .td_block_delete_bookmarks svg {
                    display: block;
                    width: 15px;
                    height: auto;
                }
                .td_block_delete_bookmarks svg,
                .td_block_delete_bookmarks svg * {
                    fill: #fff;
                    -webkit-transition: fill 0.3s ease;
                    transition: fill 0.3s ease;
                }
                
                /* @logged_in_styles */
                .td_block_delete_bookmarks .td-block-missing-settings {
                    width: 100%;
                }
                
                
                /* @all_border */
                body .$unique_block_class .td-del-book-btn {
                    border: @all_border @all_border_style @all_border_color;
                }
                /* @border_radius */
                body .$unique_block_class .td-del-book-btn,
                body .$unique_block_class .td-del-book-btn:before {
                    border-radius: @border_radius;
                }
                
                /* @padding */
                body .$unique_block_class .td-del-book-btn {
                    padding: @padding;
                }
                
                /* @min_width */
                body .$unique_block_class .td-del-book-btn {
                    min-width: @min_width;
                }
                
                /* @display_default */
                body .$unique_block_class {
                    display: block;
                }
                body .$unique_block_class .td_block_inner {
                    align-items: flex-start;
                }
                /* @display_inline */
                body .$unique_block_class {
                    display: inline-block;
                }
                /* @display_full */
                body .$unique_block_class {
                    display: block;
                }
                body .$unique_block_class .td_block_inner {
                    align-items: stretch;
                }
                
                /* @horiz_align */
                body .$unique_block_class .td-del-book-btn {
                    justify-content: @horiz_align;
                }
                
                 /* @horiz_align2 */
                body .$unique_block_class .td_block_inner {
                    align-items: @horiz_align2;
                }
                
                /* @icon_size */
                body .$unique_block_class i {
                    font-size: @icon_size;
                }
                body .$unique_block_class svg {
                    width: @icon_size;
                }
                
                /* @icon_align */
                body .$unique_block_class .td-del-book-icon {
                    top: @icon_align;
                }
                
                /* @icon_space */
                body .$unique_block_class .td-del-book-icon {
                    margin: @icon_space;
                }
                
                
                /* @text_color */
                body .$unique_block_class .td-del-book-btn {
                    color: @text_color;
                }
                body .$unique_block_class svg,
                body .$unique_block_class svg * {
                    fill: @text_color;
                }
                /* @text_color_h */
                body .$unique_block_class .td-del-book-btn:hover {
                    color: @text_color_h;
                }
                body .$unique_block_class .td-del-book-btn:hover svg,
                body .$unique_block_class .td-del-book-btn:hover svg * {
                    fill: @text_color_h;
                }
                
                /* @icon_color */
                body .$unique_block_class i {
                    color: @icon_color;
                }
                html body .$unique_block_class svg,
                html body .$unique_block_class svg * {
                    fill: @icon_color;
                }
                /* @icon_color_h */
                body .$unique_block_class .td-del-book-btn:hover i {
                    color: @icon_color_h;
                }
                html body .$unique_block_class .td-del-book-btn:hover svg,
                html body .$unique_block_class .td-del-book-btn:hover svg * {
                    fill: @icon_color_h;
                }
                
                /* @bg_solid */
                body .$unique_block_class .td-del-book-btn {
                    background: @bg_solid;
                }
                /* @bg_gradient */
                body .$unique_block_class .td-del-book-btn {
                    @bg_gradient
                }
                /* @bg_solid_h */
                body .$unique_block_class .td-del-book-btn:before {
                    background: @bg_solid_h;
                }
                /* @bg_gradient_h */
                body .$unique_block_class .td-del-book-btn:before {
                    @bg_gradient_h
                }
                
                /* @border_color_h */
                body .$unique_block_class .td-del-book-btn:hover {
                    border-color: @border_color_h;
                }
                
                /* @shadow */
                body .$unique_block_class .td-del-book-btn {
                    box-shadow: @shadow;
                }
                /* @shadow_h */
                body .$unique_block_class .td-del-book-btn:hover {
                    box-shadow: @shadow_h;
                }
                
                
                /* @f_txt */
                body .$unique_block_class .td-del-book-btn {
                    @f_txt
                }

            </style>";

		$td_css_res_compiler = new td_css_res_compiler( $raw_css );
		$td_css_res_compiler->load_settings( __CLASS__ . '::cssMedia', $this->get_all_atts() );

		$compiled_css .= $td_css_res_compiler->compile_css();

		return $compiled_css;

	}


	static function cssMedia( $res_ctx ) {

        /*-- GENERAL STYLES -- */
        $res_ctx->load_settings_raw( 'style_general_td_block_delete_bookmarks', 1 );
        if ( is_user_logged_in() ) {
            $res_ctx->load_settings_raw('logged_in_styles', 1);
        }



        /*-- LAYOUT -- */
        // border size
        $border_size = $res_ctx->get_shortcode_att('all_border');
        $res_ctx->load_settings_raw('all_border', $border_size);
        if( $border_size != '' && is_numeric( $border_size ) ) {
            $res_ctx->load_settings_raw('all_border', $border_size . 'px');
        }
        // border style
        $all_border_style = $res_ctx->get_shortcode_att('all_border_style');
        if( $all_border_style != '' ) {
            $res_ctx->load_settings_raw('all_border_style', $res_ctx->get_shortcode_att('all_border_style'));
        } else {
            $res_ctx->load_settings_raw('all_border_style', 'solid');
        }
        // border radius
        $border_radius = $res_ctx->get_shortcode_att('border_radius');
        $res_ctx->load_settings_raw('border_radius', $border_radius);
        if( $border_radius != '' && is_numeric( $border_radius ) ) {
            $res_ctx->load_settings_raw('border_radius', $border_radius . 'px');
        }


        // padding
        $padding = $res_ctx->get_shortcode_att('padd');
        $res_ctx->load_settings_raw('padding', $padding);
        if( $padding != '' && is_numeric( $padding ) ) {
            $res_ctx->load_settings_raw('padding', $padding . 'px');
        }

        // min width
        $min_width = $res_ctx->get_shortcode_att('min_width');
        $res_ctx->load_settings_raw('min_width', $min_width);
        if( $min_width != '' && is_numeric( $min_width ) ) {
            $res_ctx->load_settings_raw('min_width', $min_width . 'px');
        }

        // display
        $display = $res_ctx->get_shortcode_att('display');
        if( $display == 'default' || $display == '' ) {
            $res_ctx->load_settings_raw('display_default', 1);
        } else if( $display == 'inline' ) {
            $res_ctx->load_settings_raw('display_inline', 1);
        } else if ( $display == 'full' ) {
            $res_ctx->load_settings_raw('display_full', 1);
        }

        // horizontal align
        $horiz_align = $res_ctx->get_shortcode_att('horiz_align');
        if( $horiz_align == 'content-horiz-left' ) {
            $res_ctx->load_settings_raw('horiz_align', 'flex-start');
            if ($display == 'default' || $display == '' ) {
                $res_ctx->load_settings_raw('horiz_align2', 'flex-start');
            } else {
                $res_ctx->load_settings_raw('horiz_align2', 'stretch');
            }
        } else if( $horiz_align == 'content-horiz-center' ) {
            $res_ctx->load_settings_raw('horiz_align', 'center');
            if ($display == 'default' || $display == '' ) {
                $res_ctx->load_settings_raw('horiz_align2', 'center');
            } else {
                $res_ctx->load_settings_raw('horiz_align2', 'stretch');
            }
        } else if( $horiz_align == 'content-horiz-right' ) {
            $res_ctx->load_settings_raw('horiz_align', 'flex-end');
            if ($display == 'default' || $display == '' ) {
                $res_ctx->load_settings_raw('horiz_align2', 'flex-end');
            } else {
                $res_ctx->load_settings_raw('horiz_align2', 'stretch');
            }
        }


        // icon size
        $icon_size = $res_ctx->get_shortcode_att('icon_size');
        $res_ctx->load_settings_raw('icon_size', $icon_size);
        if( $icon_size != '' && is_numeric( $icon_size ) ) {
            $res_ctx->load_settings_raw('icon_size', $icon_size . 'px');
        }

        // icon space
        $icon_pos = $res_ctx->get_shortcode_att('icon_pos');
        $icon_space = $res_ctx->get_shortcode_att('icon_space');
        if( $icon_pos == 'before' ) {
            if( $icon_space != '' ){
                if( is_numeric( $icon_space ) ) {
                    $res_ctx->load_settings_raw('icon_space', '0 ' . $icon_space . 'px 0 0');
                } else {
                    $res_ctx->load_settings_raw('icon_space', '0 ' . $icon_space . '0 0');
                }
            } else {
                $res_ctx->load_settings_raw('icon_space', '0 10px 0 0');
            }
        } else {
            if( $icon_space != '' ){
                if( is_numeric( $icon_space ) ) {
                    $res_ctx->load_settings_raw('icon_space', '0 0 0 ' . $icon_space . 'px');
                } else {
                    $res_ctx->load_settings_raw('icon_space', '0 0 0 ' . $icon_space);
                }
            } else {
                $res_ctx->load_settings_raw('icon_space', '0 0 0 10px');
            }
        }

        // icon vertical align
        $res_ctx->load_settings_raw('icon_align', $res_ctx->get_shortcode_att('icon_align') . 'px');



        /*-- STYLE -- */
        $res_ctx->load_settings_raw('text_color', $res_ctx->get_shortcode_att('text_color'));
        $res_ctx->load_settings_raw('text_color_h', $res_ctx->get_shortcode_att('text_color_h'));
        $res_ctx->load_settings_raw('icon_color', $res_ctx->get_shortcode_att('icon_color'));
        $res_ctx->load_settings_raw('icon_color_h', $res_ctx->get_shortcode_att('icon_color_h'));
        $res_ctx->load_color_settings( 'bg_color', 'bg_solid', 'bg_gradient' );
        $res_ctx->load_color_settings( 'bg_color_h', 'bg_solid_h', 'bg_gradient_h' );
        $all_border_color = $res_ctx->get_shortcode_att('all_border_color');
        if( $all_border_color != '' ) {
            $res_ctx->load_settings_raw('all_border_color', $all_border_color);
        } else {
            $res_ctx->load_settings_raw('all_border_color', '#000');
        }
        $res_ctx->load_settings_raw('border_color_h', $res_ctx->get_shortcode_att('border_color_h'));

        $res_ctx->load_shadow_settings( 0, 0, 0, 0, 'rgba(0, 0, 0, 0.2)', 'shadow' );
        $res_ctx->load_shadow_settings( 0, 0, 0, 0, 'rgba(0, 0, 0, 0.2)', 'shadow_h' );



        /*-- FONTS -- */
        $res_ctx->load_font_settings( 'f_txt' );

	}



	function render( $atts, $content = null ) {

        parent::render( $atts );


        /* --
        -- Shortcode params.
        -- */
        /* -- Bookmark type. -- */
        $bookmark_type = $this->get_att( 'bookmark_type' );


        /* -- Button. -- */
        // Button text.
		$button_text = $this->get_att('button_text' );
        if( $button_text == '' ) {
            $button_text = __td('Delete bookmarks', TD_THEME_NAME);
        }

        // Button icon.
        $icon = $this->get_icon_att( 'tdicon' );
        $tdicon_data = '';
        if( td_util::tdc_is_live_editor_iframe() || td_util::tdc_is_live_editor_ajax() ) {
            $tdicon_data = 'data-td-svg-icon="' . $this->get_att('tdicon') . '"';
        }
        $buffy_icon = '';
        if ( !empty($icon) ) {
            if( base64_encode( base64_decode($icon) ) == $icon ) {
                $buffy_icon .= '<span class="td-del-book-icon td-del-book-icon-svg" ' . $tdicon_data . '>' . base64_decode( $icon ) . '</span>';
            } else {
                $buffy_icon .= '<i class="td-del-book-icon ' . $icon . '"></i>';
            }
        }

        // Button icon position.
        $icon_pos = $this->get_att('icon_pos' );
        


        /* --
        -- Render shortcode HTML.
        -- */
        $buffy = '';

		$buffy .= '<div class="' . $this->get_block_classes() . '" ' . $this->get_block_html_atts() . '>';

			$buffy .= $this->get_block_css(); // get block css
			$buffy .= $this->get_block_js(); // get block js


            $buffy .= '<div class="td_block_inner">';

                /* -- Bail conditions. -- */
                $block_error = '';

                if( $bookmark_type == '' ) {
                    // If no bookmark type was selected.
                    $block_error = td_util::get_block_error(
                        'Delete bookmarks button',
                        'Please select a <b>bookmark type</b>.');
                } else {
                    // A bookmark type was selected, but does not meet its specific conditions.
                    if( !defined( 'TD_CLOUD_LIBRARY' ) ) {
                        $block_error = td_util::get_block_error(
                            'Delete bookmarks button',
                            '<b>' . ( $bookmark_type == 'tdb' ? 'Bookmarked posts' : 'Favorited Woo products' ) . '</b> was selected as the bookmark type, which requires that the <b>TagDiv Cloud Library</b> plugin is installed and active.');
                    } else if( $bookmark_type == 'woo' ) {
                        if( !defined( 'TD_WOO' ) ) {
                            $block_error = td_util::get_block_error(
                                'Delete bookmarks button',
                                '<b>Favorited Woo products</b> was selected as the bookmark type, which requires that the <b>TagDiv Shop</b> plugin is installed and active.');
                        } else if ( !in_array('woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
                            $block_error = td_util::get_block_error(
                                'Delete bookmarks button',
                                '<b>Favorited Woo products</b> was selected as the bookmark type, which requires that the <b>WooCommerce</b> plugin is installed and active.');
                        }
                    }
                }

                if( !empty( $block_error ) ) {
                            $buffy .= $block_error;
                        $buffy .= '</div>';
                    $buffy .= '</div>';

                    return $buffy;
                }


                /* -- Render the button HTML. -- */
                $buffy .= '<button class="td-del-book-btn" data-bookmark-type="' . $bookmark_type . '">';
                    if ( $icon_pos == 'before' ) {
                        $buffy .= $buffy_icon;
                    }

                    $buffy .= '<span class="td-del-book-txt">' . $button_text . '</span>';

                    if ( $icon_pos == '' ) {
                        $buffy .= $buffy_icon;
                    }
                $buffy .= '</button>';

            $buffy .= '</div>';


            /* -- Include necessary scripts. -- */
            switch( $bookmark_type ) {
                case 'tdb':
                    td_resources_load::render_script( TDB_SCRIPTS_URL . '/tdbFavourites.js' . TDB_SCRIPTS_VER, 'tdbFavourites-js', '', 'footer' );
                    break;

                case 'woo':
                    td_resources_load::render_script( TD_WOO_SCRIPTS_URL . '/tdwFavourites.js' . TD_WOO_SCRIPTS_VER, 'tdwFavourites-js', '', 'footer' );
                    break;
            }

		$buffy .= '</div>';

		return $buffy;

	}
}
