<?php defined( 'ABSPATH' ) or die;

define( 'BUILDER_INCLUDES', CODE_PATH . 'builder/inc/' );
define( 'ONE_COL', '1_column' );
define( 'TWO_COL', '2_columns' );
define( 'THREE_COL', '3_columns' );
define( 'FOUR_COL', '4_columns' );
define( 'ONE_TWO', '1_2_thirds' );
define( 'TWO_ONE', '2_1_thirds' );
define( 'ONE_THREE', '1_3_quarters' );
define( 'THREE_ONE', '3_1_quarters' );
define( 'COL_NAMES', [
	ONE_COL => '1 Column',
	TWO_COL => '2 Equal Columns',
	THREE_COL => '3 Equal Columns',
	FOUR_COL => '4 Equal Columns',
	ONE_TWO => 'One third - Two thirds',
	TWO_ONE => 'Two thirds - One third',
	ONE_THREE => 'One quarter - Three quarters',
	THREE_ONE => 'Three quarters - One quarter'
] );

include_once BUILDER_INCLUDES . 'render_rows.php';

/**
 * Load builder assets
 */
function tech360_builder_assets( $hook_suffix ) {

    global $post_type;

    if( 'page-section' == $post_type && in_array( $hook_suffix, array( 'post.php', 'post-new.php' ) ) ) {
		wp_enqueue_editor();
		wp_enqueue_media();
		wp_enqueue_style( 'tech360-builder-admin', CODE_URI . '/builder/assets/tech360_builder.css' );
		wp_enqueue_script( 'tech360-builder-admin', CODE_URI . '/builder/assets/tech360_builder.js', [ 'jquery', 'jquery-ui-sortable', 'wp-tinymce' ], 1.0, true );
    }
	
}
add_action( 'admin_enqueue_scripts', 'tech360_builder_assets' );

/**
 * Builder function
 */
function tech360_builder( $post ) {

	if( 'page-section' !== $post->post_type ) return;
?>
	<div id="tech360-builder" class="tech360-builder">
		<span class="tech360-loader">Loading builder...</span>
		<?php wp_nonce_field( "tech360_nonce_action", "tech360_nonce" ) ?>

		<div class="tech360-builder-rows">
			<?php tech360_render_rows( $post ) ?>
		</div>
<?php
	require_once BUILDER_INCLUDES . 'render_actions.php';
	require_once BUILDER_INCLUDES . 'render_templates.php';
?>
	</div><!-- /#tech360-builder -->
<?php

}
add_action( 'edit_form_after_editor', 'tech360_builder' );

/**
 * Save data
 */
function tech360_builder_save( $post_id, $post ) {

	$request = stripslashes_deep( $_POST );

	// var_dump($request['tech360']); die;

	if( !isset( $request[ 'tech360_nonce' ] ) || !wp_verify_nonce( $request[ 'tech360_nonce' ], 'tech360_nonce_action' ) ) {
        return $post_id;
    }

	if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

	$post_type = get_post_type_object( $post->post_type );

    if( 'page-section' != $post->post_type || !current_user_can( $post_type->cap->edit_post, $post_id ) ) {
        return $post_id;
    }

	$saved_data = get_post_meta( $post_id, 'tech360', true );

    $submitted_data = isset( $request[ 'tech360' ] ) ? tech360_sanitize( $request[ 'tech360' ] ) : null;

	// var_dump($saved_data, $submitted_data); die;

    if( $submitted_data && '' == $saved_data ) {
        add_post_meta( $post_id, 'tech360', $submitted_data, true );
    }
    elseif( $submitted_data && ( $submitted_data != $saved_data ) ) {
        update_post_meta( $post_id, 'tech360', $submitted_data );
    }
    elseif( empty( $submitted_data ) && $saved_data ) {
        delete_post_meta( $post_id, 'tech360' );
    }

}
add_action( 'save_post', 'tech360_builder_save', 10, 2 );

/**
 * Render rows in admin view
 */
function tech360_render_rows( $post ) {

	$rows = tech360_sanitize( get_post_meta( $post->ID, 'tech360', true ) );
	// var_dump($rows);
	$default_message = 'Use the buttons below to add sections.';

	if( ! $rows ) {
	?>
		<p class="tech360-builder-msg"><?php echo $default_message ?></p>
	<?php
		return;
	} else {
	?>
		<p class="tech360-builder-msg" style="display: none"><?php echo $default_message ?></p>
	<?php
	}

	foreach( $rows as $order => $row ) {

		$order = intval( $order );

		render_rows( $order, $row );

	}

}

/**
 * Sanitize function
 */
function tech360_sanitize( $input ) {

	// var_dump($input);

    if( !is_array( $input ) ) {
        return null;
    }

    $output = array();

    foreach( $input as $row_order => $row_data ) {

        if( isset( $row_data[ 'type' ] ) && $row_data[ 'type' ] ) {

            $row_type = esc_attr( $row_data[ 'type' ] );

			$output[ $row_order ][ 'type' ] = $row_type;
			$output[ $row_order ][ 'settings' ] = [
				'row_class' => $row_data[ 'settings' ][ 'row_class' ],
				'column_class' => $row_data[ 'settings' ][ 'column_class' ]
			];

			switch( $row_type ) {
				case ONE_COL:
					$output[ $row_order ][ 'content' ] = wp_kses_post( $row_data[ 'content' ] );
					break;
				case TWO_COL:
				case ONE_TWO:
				case TWO_ONE:
				case ONE_THREE:
				case THREE_ONE:
					$output[ $row_order ][ 'content-1' ] = wp_kses_post( $row_data[ 'content-1' ] );
					$output[ $row_order ][ 'content-2' ] = wp_kses_post( $row_data[ 'content-2' ] );
					break;
				case THREE_COL:
					$output[ $row_order ][ 'content-1' ] = wp_kses_post( $row_data[ 'content-1' ] );
					$output[ $row_order ][ 'content-2' ] = wp_kses_post( $row_data[ 'content-2' ] );
					$output[ $row_order ][ 'content-3' ] = wp_kses_post( $row_data[ 'content-3' ] );
					break;
				case FOUR_COL:
					$output[ $row_order ][ 'content-1' ] = wp_kses_post( $row_data[ 'content-1' ] );
					$output[ $row_order ][ 'content-2' ] = wp_kses_post( $row_data[ 'content-2' ] );
					$output[ $row_order ][ 'content-3' ] = wp_kses_post( $row_data[ 'content-3' ] );
					$output[ $row_order ][ 'content-4' ] = wp_kses_post( $row_data[ 'content-4' ] );
					break;
				default:
					return null;
			}

        }

    }

	// var_dump($output); die;

    return $output;

}
