<?php

namespace Tech360;

defined( 'ABSPATH' ) or die;

class Section extends Cpt {

	static function _id( $id ) {

		$output = '';

		if( get_field( 'css_id', $id ) && get_field( 'enable_advanced_settings', $id ) ) {
			$output = Utils::sluggify( Utils::trim_all( get_field( 'css_id', $id ) ) );
		} else {
			$output = 'section-' . $id;
		}

		return $output;

	}

	static function _class( $id, $has_wrapper ) {

		$output = [];

		$output[] = 't360-section';

		if( get_field( 'css_classes', $id ) && get_field( 'enable_advanced_settings', $id ) ) {
			$output[] = Utils::trim_all( get_field( 'css_classes', $id ) );
		}

		if( get_field( 'layout_width', $id ) == 'boxed' ) {
			$output[] = 'container';
		} else {
			$output[] = 'container-fluid';
		}

		$single_fields = [ 'text_align', 'text_color', 'top_padding', 'bottom_padding', 'top_margin', 'bottom_margin' ];
		foreach( $single_fields as $field ) {
			if( get_field( $field, $id ) ) {
				if( get_field( $field, $id ) != 'none' ) {
					$output[] = get_field( $field, $id );
				}
			}
		}

		if( ! $has_wrapper && get_field( 'bg_enable', $id ) && get_field( 'bg_background', $id ) && get_field( 'bg_parallax', $id ) ) {
			$output[] = 'parallax-bg';
		}

		return implode( ' ', $output );

	}

	static function _style( $id, $has_wrapper ) {

		$output = [];

		if( ( 'boxed' == get_field( 'layout_width', $id ) ) && ( 'none' != get_field( 'layout_max_width', $id ) ) ) {
			$output[] = sprintf( 'max-width: %s', get_field( 'layout_max_width', $id ) );
		}

		if( ! $has_wrapper ) {
			$o = explode( '; ', self::_wrapper_style( $id ) );
			$output = array_merge( $output, $o );
		}

		return implode( '; ', $output );

	}

	static function _wrapper_class( $id ) {

		$output = [];

		if( get_field( 'bg_parallax', $id ) && has_post_thumbnail( $id ) ) {
			$output[] = 'parallax-bg';
		}

		return implode( ' ', $output );

	}

	static function _wrapper_style( $id ) {

		$output = [];

		if( get_field( 'bg_color', $id ) ) {
			$output[] = sprintf( 'background-color: %s', get_field( 'bg_color', $id ) );
		}

		if( get_field( 'bg_enable', $id ) && get_field( 'bg_background', $id ) && has_post_thumbnail( $id ) ) {
			$output[] = sprintf( 'background-image: url(%s)', get_the_post_thumbnail_url( $id, 'full' ) );
		}

		if( ! get_field( 'bg_parallax', $id ) && get_field( 'bg_background', $id ) && has_post_thumbnail( $id ) ) {
			$output[] = sprintf( 'background-repeat: %s', get_field( 'bg_repeat', $id ) );
			$output[] = sprintf( 'background-size: %s', get_field( 'bg_size', $id ) );
			$output[] = sprintf( 'background-position: %s', get_field( 'bg_position', $id ) );
		}

		return implode( '; ', $output );

	}

	static function _row_class( $settings ) {

		$output = [];
		$output[] = 'row';

		if( $settings[ 'row_css_class' ] ) {
			$classes = explode( ' ', Utils::trim_all( $settings[ 'row_css_class' ] ) );
			$output = array_merge( $output, $classes );
		}

		if( $settings[ 'max_width' ] ) {
			$output[] = 'mx-auto';
		}

		$single_settings = [ 'text_align', 'text_color', 'col_vertical_align', 'top_spacing', 'bottom_spacing' ];

		foreach( $single_settings as $setting ) {
			if( ! empty( $settings[ $setting ] ) ) {
				$output[] = $settings[ $setting ];
			}
		}

		return implode( ' ', $output );

	}

	static function _row_style( $settings ) {

		$output = [];

		if( $settings[ 'max_width' ] ) {
			$output[] = sprintf( 'max-width: %s', $settings[ 'max_width' ] );
		}

		return implode( '; ', $output );

	}

	static function _row_data( $settings ) {

		$output = [];

		if( $settings[ 'col_inview' ] ) {
			$output[] = sprintf( 'data-col-inview="%s"', $settings[ 'col_inview' ] );
		}

		if( ! empty( $output ) ) {
			return ' ' . implode( ' ', $output );
		} else {
			return '';
		}

	}

	static function _col_class( $settings, $type, $col_index ) {

		$output = [];
		$output[] = Builder::$column_classes[ $type ][ $col_index ];

		if( $settings[ 'col_css_class' ] ) {
			$output = array_merge( $output, explode( ' ', Utils::trim_all( $settings[ 'col_css_class' ] ) ) );
		}

		return implode( ' ', $output );

	}

	static function render( $id ) {

		$rows = Builder::content( $id );

		if( ! is_array( $rows ) )
			return;

		$has_wrapper = get_field( 'bg_enable', $id ) && get_field( 'bg_fullwidth', $id ) && ( get_field( 'layout_width', $id ) == 'boxed' ) && ( ( get_field( 'bg_background', $id ) && has_post_thumbnail( $id ) ) || get_field( 'bg_color', $id ) );

		$section_id = self::_id( $id );
		$section_class = self::_class( $id, $has_wrapper );
		$section_style = self::_style( $id, $has_wrapper );

		if( $has_wrapper ) { // Render the section wrapper
			$wrapper_class = self::_wrapper_class( $id );
			$wrapper_style = self::_wrapper_style( $id );

	?>
		<div class="<?php echo $wrapper_class ?>" style="<?php echo $wrapper_style ?>">
	<?php
		}

		// Render the section

	?>
		<section id="<?php echo $section_id ?>" class="<?php echo $section_class ?>" style="<?php echo $section_style ?>">
		<?php

			// Render the section title

			if( get_field( 'section_display_title', $id ) ) {
				$hl = get_field( 'section_title_heading_level', $id );
				$the_title = apply_filters( 'the_title', get_the_title( $id ), $id );
		?>
			<div class="row">
				<div class="col-12">
					<<?php echo $hl ?> class="section-heading"><?php echo $the_title ?></<?php echo $hl ?>>
				</div>
			</div>
		<?php } ?>
		<?php

			// Render the rows

			foreach( $rows as $row_index => $row ) {
				extract( $row );
				if( $settings[ 'row_css_id' ] ) {
					$row_id = Utils::sluggify( Utils::trim_all( $settings[ 'row_css_id' ] ) );
				} else {
					$row_id = 'row-' . $id . '-' . $row_index;
				}
				$row_class = self::_row_class( $settings );
				$row_style = self::_row_style( $settings );
				$row_data = self::_row_data( $settings );
		?>
			<div id="<?php echo $row_id ?>" class="<?php echo $row_class ?>" style="<?php echo $row_style ?>"<?php echo $row_data ?>>
		<?php

				// Render the columns

				foreach( $content as $col_index => $column ) {
					// $col_class = Builder::$column_classes[ $type ];
					$col_class = self::_col_class( $settings, $type, $col_index );
					$col_content = apply_filters( 'the_content', $column );
		?>
				<div class="<?php echo $col_class ?>">
					<?php echo $col_content ?>
				</div>
		<?php
				}
		?>
			</div>
		<?php
			}

			// Render the button

			if( get_field( 'section_button', $id ) && get_field( 'section_button_link', $id ) ) {
				$btn_wrapper_class = [];
				$btn_wrapper_class[] = 'row';
				if( 'none' != get_field( 'section_button_alignment', $id ) ) {
					$btn_wrapper_class[] = get_field( 'section_button_alignment', $id );
				}
				if( 'none' != get_field( 'section_button_spacing', $id ) ) {
					$btn_wrapper_class[] = get_field( 'section_button_spacing', $id );
				}
				$btn_class = [];
				$btn_class[] = 'btn';
				$btn_class[] = get_field( 'section_button_type', $id );
		?>
			<div class="<?php echo implode( ' ', $btn_wrapper_class ) ?>">
				<div class="col-12">
					<a class="<?php echo implode( ' ', $btn_class ) ?>" href="<?php echo get_field( 'section_button_link', $id ) ?>"><?php echo get_field( 'section_button_label', $id ) ?></a>
				</div>
			</div>
		<?php
			}
		?>
		</section>
	<?php
		if( $has_wrapper ) {
	?>
		</div>
	<?php
		}

	}

}

$section_labels = [
	'singular_name' => 'Section',
	'name' => 'Sections'
];

$section_args = [
	'menu_icon' => 'dashicons-format-aside',
	'menu_position' => 32,
	'supports' => [ 'title', 'thumbnail', 'page-attributes' ]
];

$section_columns = [
	'width' => [
		'field' => 'section_width',
		'label' => 'Width',
		'sortable' => true
	],
	'shortcode' => [
		'label' => 'Shortcode<br><i style="font-size: 85%">Click to select, CTRL+C to copy</i>',
		'content' => '[section id=%s]'
	],
	'menu_order' => [
		'label' => 'Priority',
		'sortable' => true
	]
];

$section = new Section(
	'section',
	$section_labels,
	$section_args
);

$section->columns( $section_columns, false );
