<?php

class Intense_Panel extends Intense_Shortcode {

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

        global $intense_visions_options;

        $this->title = __( 'Panel', 'intense' );
        $this->description = __( 'The panel shortcode can be used to put content in a box on your WordPress website. Because of its many options, it can also be used as a general layout element for page building.', 'intense' );
        $this->category = __( 'Layout', 'intense' );
        $this->show_preview = true;
        $this->preview_content = __( "Panel Content", 'intense' );
        $this->is_container = true;
    }

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

        $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 . '; ' : '' );

        $border_style = null;
        $border_top_style = null;
        $border_right_style = null;
        $border_bottom_style = null;
        $border_left_style = null;
        $radius = '';

        if ( isset( $border ) ) $border_style = "border: " . $border . ' !important; ';
        if ( isset( $border_top ) ) $border_top_style = "border-top: " . $border_top . ' !important; ';
        if ( isset( $border_right ) ) $border_right_style = "border-right: " . $border_right . ' !important; ';
        if ( isset( $border_bottom ) ) $border_bottom_style = "border-bottom: " . $border_bottom . ' !important; ';
        if ( isset( $border_left ) ) $border_left_style = "border-left: " . $border_left . ' !important; ';
        
        if ( isset( $top_left_border_radius ) ) $top_left_border_radius = ' border-top-left-radius:' . $top_left_border_radius . ';';
        if ( isset( $top_right_border_radius ) ) $top_right_border_radius = ' border-top-right-radius:' . $top_right_border_radius . ';';
        if ( isset( $bottom_left_border_radius ) ) $bottom_left_border_radius = ' border-bottom-left-radius:' . $bottom_left_border_radius . ';';
        if ( isset( $bottom_right_border_radius ) ) $bottom_right_border_radius = ' border-bottom-right-radius:' . $bottom_right_border_radius . ';';

        $title_color = intense_get_plugin_color( $title_color );
        $title_font_color = intense_get_plugin_color( $title_font_color );
        $color = intense_get_plugin_color( $color );
        $font_color = intense_get_plugin_color( $font_color );
        $footer_color = intense_get_plugin_color( $footer_color );
        $footer_font_color = intense_get_plugin_color( $footer_font_color );

        if ( $title_font_color == '' ) $title_font_color = intense_get_contract_color( $title_color );            
        if ( $font_color == '' ) $font_color = intense_get_contract_color( $color );
        if ( $footer_font_color == '' ) $footer_font_color = intense_get_contract_color( $footer_color );

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

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

        $panel_style = $margin . $border_style . $border_top_style . $border_right_style . $border_bottom_style . $border_left_style . $top_left_border_radius . $top_right_border_radius . $bottom_left_border_radius . $bottom_right_border_radius;
        $panel_heading_style = "color: " . $title_font_color . "; background: " . $title_color  . "; " . $top_left_border_radius . $top_right_border_radius;
        $panel_body_style = "color: " . $font_color . "; background: " . $color  . "; " . $padding;
        $footer_style = "color: " . $footer_font_color . "; background: " . $footer_color  . "; " . $bottom_left_border_radius . $bottom_right_border_radius;

        if ( empty( $title ) ) $panel_body_style .= $top_left_border_radius . $top_right_border_radius;
        if ( empty( $footer ) ) $panel_body_style .= $bottom_left_border_radius . $bottom_right_border_radius;
        
        $output .= "<div" . $id . " class='intense ipanel" . ( !empty( $class ) ? ' ' . $class : '' ) . ( is_rtl() ? " rtl" : "" ) . "' style='" . $panel_style . "'>" .
            ( !empty( $title ) ? '<div class="intense ipanel-heading" style="' . $panel_heading_style . '">' : '' ) .
            ( !empty( $title_tag ) ? '<' . $title_tag . ' class="intense ipanel-title">' : '' ) .
            ( !empty( $title ) ? $title : '' ) .
            ( !empty( $title_tag ) ? '</' . $title_tag . '>' : '') .
            ( !empty( $title ) ? '</div>' : '' ) .
            '<div class="intense ipanel-body" style="' . $panel_body_style . '">' . do_shortcode( $content ) . '</div>' .
            ( !empty( $footer ) ? '<div class="intense ipanel-footer" style="' . $footer_style . '">' . $footer . '</div>' : "" ) .
            "</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.ipanel";
        }

        if ( isset( $atts['get_css'] ) && $atts['get_css'] ) {
            return array(
                'css' => $css_selector . '{' . $panel_style . '} ' . 
                    $css_selector . ' ipanel-heading{' . $panel_heading_style . '} ' .
                    $css_selector . ' ipanel-body{' . $panel_body_style . '} ' . 
                    $css_selector . ' ipanel-footer{' . $footer_style . '}',
                'base_classes' => 'intense ipanel',
                'extra_classes' => ( !empty( $class ) ? ' ' . $class : '' ) . ( is_rtl() ? " rtl" : "" )
            );
        }

        return $output;
    }

}
