<?php

class Intense_Device_Mockups extends Intense_Shortcode {

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

		global $intense_visions_options;

		$this->title = __( 'Device Mockups', 'intense' );
		$this->description = __( 'The Device Mockups shortcode allows you to create a mobile device, tablet, or browser mockup. This shortcode is used in conjunction with the Device Mockups WordPress plugin.', 'intense' );
		$this->category = __( 'Media', 'intense' );
		$this->show_preview = false;
		//$this->preview_content = __( "Device Mockup Content", 'intense' );
		$this->is_container = true;
	}

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

		global $intense_device_array;

		$output = '';

		$intense_device_array = array();

		//gather array of mockups
		do_shortcode( $content );

		$i = 0;

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

		$output .= '<div' . $id . ' class="intense device-mockup' . ( !empty( $class ) ? ' ' . $class : '' ) . '" style="display: inline;">';

		foreach ($intense_device_array as $key => $device) {
			$content = $device['content'];
			unset( $device['content'] );

			if ( $i == 0 && count( $intense_device_array ) > 1 ) {
				$device['stacked'] = 'open';
			} else if ( $i == count( $intense_device_array ) - 1 && count( $intense_device_array ) > 1 ) {
				$device['stacked'] = 'closed';
			}

			if ( !empty( $device['link'] ) &&  is_numeric( $device['link'] ) ) {
	            $device['link'] = get_permalink( $device['link'] );
	        }

			switch ( $device['type'] ) {
				case 'iphone6':
				case 'iphone6-plus':
				case 'iphone5s':
				case 'iphone5':
				case 'ipad':
				case 'macbook':
				case 'imac':
				case 's3':
				case 'nexus7':
				case 'surface':
				case 'lumia920':
					if ( function_exists( "device_wrapper") ) {
						$output .= device_wrapper( $device, $content );					
					} else if ( function_exists( 'device_mockups_device_wrapper' ) ) {
						wp_enqueue_style( 'device-mockups-styles', DEVICE_MOCKUPS_URL . '/css/device-mockups.css', array(), DEVICE_MOCKUPS_VERSION, false );
						$output .= device_mockups_device_wrapper( $device, $content );
					}
					break;
				case 'chrome':
				case 'firefox':
				case 'safari':
					if ( function_exists( "browser_wrapper") ) {
						$output .= browser_wrapper( $device, $content );
					} else if ( function_exists( 'device_mockups_browser_wrapper' ) ) {
						wp_enqueue_style( 'device-mockups-styles', DEVICE_MOCKUPS_URL . '/css/device-mockups.css', array(), DEVICE_MOCKUPS_VERSION, false );
						$output .= device_mockups_browser_wrapper( $device, $content );
					}
					break;
			}

			$i++;
		}

		$output .= '</div>';	

		return $output;
	}	
}
