<?php

class Intense_Button extends Intense_Shortcode {

    function __construct() {
        parent::__construct();
        
        global $intense_visions_options;

        $this->title = __( 'Button', 'intense' );
        $this->description = __( 'The button shortcode adds a clickable button to the page. Buttons can be used to redirect the user to another part of your site or to another site completely. There are many options to change the style of the button such as: rounding the corners, adding a gradient, changing the color, adding a 3D effect, and more.', 'intense');
        $this->category = __( 'Elements', 'intense' );
        $this->show_preview = true;
        $this->preview_content = __( "Button Text", 'intense' );
        $this->vc_map_content = 'textarea';
    }

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

        $random = rand();
        $smooth_scroll = null;

        if ( is_numeric( $font_size ) ) $font_size .= "px";

        $font_size = ( !empty( $font_size ) ? " font-size: " . $font_size . ';' : '' );

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

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

        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;';

        $border = $border_style . $border_top_style . $border_right_style . $border_bottom_style . $border_left_style;

        $hover_border_style = null;
        $hover_border_top_style = null;
        $hover_border_right_style = null;
        $hover_border_bottom_style = null;
        $hover_border_left_style = null;

        if ( isset( $hover_border ) ) $hover_border_style = "border: " . $hover_border . ' !important;';
        if ( isset( $hover_border_top ) ) $hover_border_top_style = "border-top: " . $hover_border_top . ' !important;';
        if ( isset( $hover_border_right ) ) $hover_border_right_style = "border-right: " . $hover_border_right . ' !important;';
        if ( isset( $hover_border_bottom ) ) $hover_border_bottom_style = "border-bottom: " . $hover_border_bottom . ' !important;';
        if ( isset( $hover_border_left ) ) $hover_border_left_style = "border-left: " . $hover_border_left . ' !important;';

        $hover_border = $hover_border_style . $hover_border_top_style . $hover_border_right_style . $hover_border_bottom_style . $hover_border_left_style;

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

        switch ( $size ) {
        case 'mini':
            $size = 'xs';
            break;
        case 'small':
            $size = 'sm';
            break;
        case 'medium':
            $size = 'md';
            break;
        case 'large':
            $size = 'lg';
            break;
        }

        if ( $color == 'link' ) {
            $color = 'transparent';
            $hover_color = 'transparent';
            $font_color = intense_get_plugin_color( 'primary' );
            $hover_font_color = intense_adjust_color_brightness( $font_color, "30" );
        }

        $color = intense_get_plugin_color( $color );

        if ( intense_get_web_color_hex( $color ) ) {
            $color = intense_get_web_color_hex( $color );
        }

        $font_color = intense_get_plugin_color( $font_color );

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

        if ( empty( $hover_color ) ) {
            if ( $color == 'transparent' ) {
                $hover_color = $color;
            } else {
                $hover_color = intense_adjust_color_brightness( $color, $hover_brightness );
            }
        }

        if ( empty( $hover_font_color ) ) {
            $hover_font_color = intense_get_contract_color( $hover_color );
        } else {
            $hover_font_color = intense_get_plugin_color( $hover_font_color );
        }

        $gradient_css = '';

        if ( $gradient && $color != 'transparent' ) {
            $end_color = intense_adjust_color_brightness( $color, -60 );
            $gradient_css = intense_linear_gradient_css( $color, $end_color );
        }

        $icon_left = '';
        $icon_right = '';

        if ( !empty( $icon ) ) {
            $icon = intense_run_shortcode( 'intense_icon', array( 'source' => $icon_source, 'type' => $icon, 'color' => $font_color ) );

            if ( $icon_position == "left" ) {
                $icon_left = $icon . " ";
            } else {
                $icon_right = " " . $icon;
            }
        }

        if ( is_numeric( $border_radius ) ) $border_radius .= "px";
        if ( is_numeric( $hover_border_radius ) ) $hover_border_radius .= "px";

        $radius = '';

        if ( $border_radius != '' ) {
            $radius = ' border-radius:' . $border_radius . ';';
        } else {
            $radius = ' border-radius: 0;';
        }

        $hover_radius = '';

        if ( $hover_border_radius != '' ) {
            $hover_radius = ' border-radius:' . $hover_border_radius . ';';
        }

        $angle_3d_style = '';
        $hover_angle_3d_style = '';

        if ( !empty( $angle_3d ) || $angle_3d == 0 ) {
            $x_offset = 0;
            $y_offset = 0;
            $x_hover_offset = 0;
            $y_hover_offset = 0;
            $color_3d = intense_adjust_color_brightness( $color, -50 );
            $hover_color_3d = intense_adjust_color_brightness( $hover_color, -50 );
            $angle_3d_style = '';            

            if ( is_null( $hover_size_3d ) ) $hover_size_3d = $size_3d;

            switch ( $angle_3d ) {
            case '0':
                $x_offset = $size_3d;
                $x_hover_offset = $hover_size_3d;
                $hover_margin_left = ( $size_3d - $hover_size_3d );
                break;
            case '45':
                $x_offset = $size_3d;
                $y_offset = -$size_3d;
                $x_hover_offset = $hover_size_3d;
                $y_hover_offset = -$hover_size_3d;
                $hover_margin_left = ( $size_3d - $hover_size_3d );
                $hover_margin_top = -( $size_3d - $hover_size_3d );
                break;
            case '90':
                $y_offset = -$size_3d;
                $y_hover_offset = -$hover_size_3d;
                $hover_margin_top = -( $size_3d - $hover_size_3d );
                break;
            case '135':
                $x_offset = -$size_3d;
                $y_offset = -$size_3d;
                $x_hover_offset = -$hover_size_3d;
                $y_hover_offset = -$hover_size_3d;
                $hover_margin_left = -( $size_3d - $hover_size_3d );
                $hover_margin_top = -( $size_3d - $hover_size_3d );
                break;
            case '180':
                $x_offset = -$size_3d;
                $x_hover_offset = -$hover_size_3d;
                $hover_margin_left = -( $size_3d - $hover_size_3d );
                break;
            case '225':
                $x_offset = -$size_3d;
                $y_offset = $size_3d;
                $x_hover_offset = -$hover_size_3d;
                $y_hover_offset = $hover_size_3d;
                $hover_margin_left = -( $size_3d - $hover_size_3d );
                $hover_margin_top = ( $size_3d - $hover_size_3d );
                break;
            case '270':
                $y_offset = $size_3d;
                $y_hover_offset = $hover_size_3d;
                $hover_margin_top = ( $size_3d - $hover_size_3d );
                break;
            case '315':
                $x_offset = $size_3d;
                $y_offset = $size_3d;
                $x_hover_offset = $hover_size_3d;
                $y_hover_offset = $hover_size_3d;
                $hover_margin_left = ( $size_3d - $hover_size_3d );
                $hover_margin_top = ( $size_3d - $hover_size_3d );
                break;
            }

            if ( $x_offset != 0 || $y_offset != 0 ) {
                $angle_3d_style = ' box-shadow: ' . $x_offset . 'px ' . $y_offset . 'px 0px 0px ' . $color_3d . ';';
            }

            if ( $x_hover_offset != 0 || $y_hover_offset != 0 || $hover_size_3d != $size_3d ) {
                $hover_angle_3d_style = ' box-shadow: ' . $x_hover_offset . 'px ' . $y_hover_offset . 'px 0px 0px ' . $hover_color_3d . ' !important;';
            }

            if ( $hover_size_3d != $size_3d ) {
                if ( !empty( $hover_margin_top ) ) $hover_angle_3d_style .= ' margin-top: ' . $hover_margin_top . 'px !important;';
                if ( !empty( $hover_margin_bottom ) )$hover_angle_3d_style .= ' margin-bottom: ' . $hover_margin_bottom . 'px !important;';
                if ( !empty( $hover_margin_left ) ) $hover_angle_3d_style .= ' margin-left: ' . $hover_margin_left . 'px !important;';
                if ( !empty( $hover_margin_right ) ) $hover_angle_3d_style .= ' margin-right: ' . $hover_margin_right . 'px !important;';
            }
        }

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

        if ( empty( $id ) ) $id = "btn_$random";

        $text_transform = ( !empty( $text_transform ) ? " text-transform: " . $text_transform . ';' : '' );

        $GLOBALS['IntensePluginInlineStyle']['#btn_' . $random] =
            "
        $css_selector {
            color: " . $font_color . " !important;
            $radius $border $angle_3d_style $padding $font_size 
            background: " . $color  . "; " . $gradient_css . ";" .
            $text_transform  . "
        }
        $css_selector:hover {
            filter: none !important;
            background: none !important;
            background-color: $hover_color !important;
            color: $hover_font_color !important;
            $hover_radius $hover_angle_3d_style $hover_border
        }";

        if ( isset( $link ) && $link[0] == '#' ) {
            intense_add_script( "smooth-scroll" );
            $smooth_scroll = '<script>jQuery(document).ready(function() { jQuery(\'#btn_' . $random . '\').smoothScroll({ speed: 1200 }); });</script>';
        }

        if ( is_numeric( $link ) ) {
            $link = get_permalink( $link );
        }        

        if ( isset( $atts['get_css'] ) && $atts['get_css'] ) {
            $get_css = $GLOBALS['IntensePluginInlineStyle']['#btn_' . $random];

            return array( 
                'css' => $get_css,
                'base_classes' => 'intense btn',
                'extra_classes' => $class . ' ' . ( $size != '' ? " btn-$size" : "" ) . ( $block ? ' btn-block': '' )
            );
        }      

        $startalign = '';
        $endalign = '';
        $alignCSS = '';

        if ( isset( $align ) && $align !== '' ) {
            switch ( $align ) {
                case 'left':
                    $alignCSS = 'float:left;';
                    break;
                case 'right':
                    $alignCSS = 'float:right;';
                    break;
                case 'center':
                    $startalign = '<center>';
                    $endalign = '</center>';
                    break;
            }
        }

        if ( !empty( $rel ) ) $rel = ' rel="' . $rel . '"';  

        return $startalign . "<a id='$id' style='" . $alignCSS . "' href='$link'" . $rel . " target='$target' " . ( !empty( $title ) ? 'title="' . $title . '"' : '' ) . " class='intense $class btn" . ( $size != '' ? " btn-$size" : "" ) . ( $block ? ' btn-block': '' ) . " '>$icon_left" . do_shortcode( $content ) . "$icon_right</a>" . $endalign . "$smooth_scroll";


    }   
}
