<?php

class Intense_Alert extends Intense_Shortcode {

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

        global $intense_visions_options;

        $this->title = __( 'Alert', 'intense' );
        $this->description = __( 'The alert shortcode can be used to draw attention to important information on your WordPress website. Because of its many options, it can also be used as a general layout element for page building. Alerts are similar to the content box, promotional box, and content section shortcodes but are more basic.', 'intense' );
        $this->category = __( 'Layout', 'intense' );
        $this->show_preview = true;
        $this->preview_content = __( "Alert Content", 'intense' );
        $this->is_container = true;
    }

    function shortcode( $atts, $content = null ) {
        extract( shortcode_atts( $this->get_shortcode_defaults( null, $atts ), $atts ) );

        intense_add_script( 'intense.alert' );

        $output = '';

        if ( is_numeric( $margin_top ) ) $margin_top .= "px";
        if ( is_numeric( $margin_bottom ) ) $margin_bottom .= "px";
        if ( is_numeric( $margin_left ) ) $margin_left .= "px";
        if ( is_numeric( $margin_right ) ) $margin_right .= "px";

        if ( is_numeric( $padding_top ) ) $padding_top .= "px";
        if ( is_numeric( $padding_bottom ) ) $padding_bottom .= "px";
        if ( is_numeric( $padding_left ) ) $padding_left .= "px";
        if ( is_numeric( $padding_right ) ) $padding_right .= "px";

        $padding = ( !empty( $padding_top ) ? ' padding-top: ' . $padding_top . '; ' : '' ) .
            ( !empty( $padding_bottom ) ? ' padding-bottom: ' . $padding_bottom . '; ' : '' ) .
            ( !empty( $padding_left ) ? ' padding-left: ' . $padding_left . '; ' : '' ) .
            ( !empty( $padding_right ) ? ' padding-right: ' . $padding_right . '; ' : '' );

        $margin = ( !empty( $margin_top ) ? ' margin-top: ' . $margin_top . '; ' : '' ) .
            ( !empty( $margin_bottom ) ? ' margin-bottom: ' . $margin_bottom . '; ' : '' ) .
            ( !empty( $margin_left ) ? ' margin-left: ' . $margin_left . '; ' : '' ) .
            ( !empty( $margin_right ) ? ' margin-right: ' . $margin_right . '; ' : '' );

        $box_shadow = ( !empty( $box_shadow ) ? ' box-shadow: ' . $box_shadow . '; ' : '' );

        if ( $close ) {
            intense_add_script( 'bootstrap-alert' );

            if ( isset( $close_memory ) && !empty ( $id ) ) {
                if ( ! empty( $_COOKIE[ 'intense-alert-' . $id ] ) ) {
                    return '';
                }
            } else {
                $close_memory = '';
            }
        }

        $radius = '';
        if ( isset( $border_radius ) ) {
            $radius = ' border-radius:' . $border_radius . ';';
        }

        $color = intense_get_plugin_color( $color );
        $font_color = intense_get_plugin_color( $font_color );

        if ( $font_color == '' ) {
            $font_color = intense_get_contract_color( $color );
        }

        if ( $shadow ) {
            $output .= "<div><div style='margin-bottom: 0;'>";
        }

        $id = ( !empty ( $id ) ) ? ' id="' . $id . '"' : '';

        $alert_style = "color: " . $font_color . "; background: " . $color  . "; " . $radius . ";" . $margin . $padding . $box_shadow;

        $output .= "<div" . $id . " class='intense alert" . ( !empty( $class ) ? ' ' . $class : '' ) . ( $close ? ' alert-dismissable' : '' ) . ( $block ? " alert-block" : "" ) . ( is_rtl() ? " rtl" : "" ) . "' style='" . $alert_style . "'>" .
            ( $close ? '<button type="button" class="intense close" data-dismiss="alert" data-memory="' . $close_memory . '" aria-hidden="true">&times;</button>' : "" ) .
            do_shortcode( $content ) .
            "</div>";

        if ( $shadow ) {
            if ( !empty ( $margin_bottom ) ) {
                $margin_top = ' margin-top: -' . $margin_bottom . '; ';
                $margin_bottom = ' margin-bottom: ' . $margin_bottom . '; ';
            }

            $output .= "</div><img src='" . INTENSE_PLUGIN_URL . "/assets/img/shadow{$shadow}.png' class='intense shadow' style='vertical-align: top;" . $margin_bottom . $margin_top . "' alt='' /></div>";
        }

        if ( !empty( $id ) ) {
            $css_selector = '#' . $id;
        } else if ( !empty( $class ) ) {
                $css_selector = '.' . $class;
            } else {
            $css_selector = ".intense.alert";
        }

        if ( isset( $atts['get_css'] ) && $atts['get_css'] ) {
            return array(
                'css' => $css_selector . '{' . $alert_style . '}',
                'base_classes' => 'intense alert',
                'extra_classes' => ( !empty( $class ) ? ' ' . $class : '' ) . ( $close ? ' alert-dismissable' : '' ) . ( $block ? " alert-block" : "" ) . ( is_rtl() ? " rtl" : "" )
            );
        }

        return $output;
    }

}
