<?php
/**
 * Zeen block columns
 *
 * @since 1.0.0
 */

class ZeenBlockColumns extends ZeenBlocks {

	/**
	 * Constructor
	 *
	 * @since 1.0.0
	 *
	*/
	public function __construct( $args ) {
		$this->type = 'columns';
		parent::__construct( $args );
	}

	/**
	 * Block output
	 *
	 * @since 1.0.0
	 *
	*/
	public function output( $echo = true ) {

		if ( $this->enabled() != true ) {
			return;
		}

		if ( empty( $echo ) ) {
			ob_start();
		}
		$length = $this->args['columns'];
		if ( 3 == $this->args['layout'] && 3 == $length ) {
			$length = 4;
			$pieces = true;
		}
		$classes = '';
		$responsive = $this->responsive_check();
		if ( empty( $responsive['mobile'] ) ) {
			$classes .= ' mob-off';
		}

		if ( empty( $responsive['desktop'] ) ) {
			$classes .= ' dt-off';
		}
		echo '<div id="block-wrap-' . (int) $this->block_js_id() . '" class="block-wrap-no-' . (int) $this->block_id() . ' block-css-' . (int) $this->block_js_id() . ' block-wrap block-wrap-110 content-bg block-wrap-cols-' . (int) $this->args['columns'] . ' block-wrap-cols-' . (int) $this->args['columns'] . '-' . (int) $this->args['layout'] . esc_attr( $classes ) . ' clearfix tipi-row"';
		echo ' data-id="' . (int) $this->block_js_id() . '"';
		echo '>';

		$i = 0;
		$is_110 = parent::$is_110;
		parent::$is_110 = true;
		if ( ! empty( $pieces ) ) {
			echo '<div class="tipi-col-piece tipi-col-piece-1">';
		}
		foreach ( $this->args['nested'] as $key ) {
			if ( 2 == $this->args['columns'] ) {
				$size = 50;
				if ( 1 == $this->args['layout'] ) {
					$size = 0 == $i ? 66 : 33;
				} elseif ( 2 == $this->args['layout'] ) {
					$size = 0 == $i ? 33 : 66;
				}
			} elseif ( 3 == $this->args['columns'] ) {
				$size = 33;
				if ( 1 == $this->args['layout'] ) {
					if ( 0 == $i ) {
						$size = 20;
					} elseif ( 1 == $i ) {
						$size = 46;
					}
				} elseif ( 2 == $this->args['layout'] ) {
					if ( 2 == $i ) {
						$size = 20;
					} elseif ( 1 == $i ) {
						$size = 46;
					}
				} elseif ( 3 == $this->args['layout'] ) {
					if ( 2 == $i ) {
						$size = 66;
					}
				}
			}
			echo '<div class="tipi-col zeen-col zeen-col-' . (int) $i . ' zeen-col-sz-' . (int) $size;
			if ( $size < 50 ) {
				echo ' zeen-col--narrow';
			} else {
				echo ' zeen-col--wide';
			}
			echo '">';
			TipiBuilder\ZeenHelpers::zeen_print_content( $key, true );
			echo '</div>';
			$i++;
			if ( ! empty( $pieces ) && 3 == $i ) {
				echo '</div><div class="tipi-col-piece tipi-col-piece-2">';
			}
			if ( $i == $length ) {
				break;
			}
		}
		if ( ! empty( $pieces ) ) {
			echo '</div>';
		}
		if ( empty( $is_110 ) ) {
			parent::$is_110 = '';
		}

		echo '</div>';

		if ( empty( $echo ) ) {
			return ob_get_clean();
		}
	}

}
