<?php

namespace EDPW\Pro\Controllers\Admin;

use EDPW\Core\Helpers\Input;
use EDPW\Core\Helpers\WC;
use EDPW\Core\Models\CustomBlocks as CustomBlocksModel;
use EDPW\Pro\Helpers\Validation;

defined( 'ABSPATH' ) || exit;

class CustomBlocks {
	/**
	 * Get Custom Blocks.
	 * @return void
	 */
	public static function getCustomBlocks() {
		if ( ! WC::isSecurityValid( 'edpw-template-nonce' ) ) {
			wp_send_json_error( [ 'message' => __( 'Basic check failed', 'email-editor-plus' ) ] );
		}
		wp_send_json_success( [
			'items' => CustomBlocksModel::getAll()
		] );
	}

	/**
	 * Save custom block.
	 * @return void
	 */
	public static function saveCustomBlock() {
		if ( ! WC::isSecurityValid( 'edpw-template-nonce' ) ) {
			wp_send_json_error( [ 'message' => __( 'Basic check failed', 'email-editor-plus' ) ] );
		}
		$save_data     = [
			'title'    => Input::get( 'title' ),
			//'title_content' => ! empty( $_POST['title_content'] ) ? stripslashes( $_POST['title_content'] ) : '',
			//'block_id'      => Input::get( 'block_id' ),
			//phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended
			'content'  => ! empty( $_POST['content'] ) ? stripslashes( $_POST['content'] ) : '',
			'category' => Input::get( 'category' ),
		];
		$validate_data = Validation::validateCustomBlocks( $save_data );
		if ( is_array( $validate_data ) ) {
			foreach ( $validate_data as $key => $validate ) {
				$validate_data[ $key ] = current( $validate );
			}
			wp_send_json_error( [ 'field_errors' => $validate_data ] );
		}
		$custom_block_id = CustomBlocksModel::saveCustomBlock( $save_data );
		if ( $custom_block_id <= 0 ) {
			wp_send_json_error( [ 'message' => __( 'Custom block save failed.', 'email-editor-plus' ) ] );
		}
		wp_send_json_success( [
			'message' => __( 'Custom block saved successfully.', 'email-editor-plus' ),
			'item'    => CustomBlocksModel::getWhere( 'id =' . $custom_block_id )
		] );
	}

	/**
	 * Delete custom block.
	 * @return void
	 */
	public static function deleteCustomBlock() {
		if ( ! WC::isSecurityValid( 'edpw-template-nonce' ) ) {
			wp_send_json_error( [ 'message' => __( 'Basic check failed', 'email-editor-plus' ) ] );
		}
		$block_id = Input::get( 'block_id' );
		$id       = substr( $block_id, 11 );
		if ( ! empty( $block_id ) && $id > 0 ) {
			if ( ! CustomBlocksModel::delete( [ 'id' => $id, 'block_id' => $block_id ] ) ) {
				wp_send_json_error( [ 'message' => __( 'Custom block delete failed', 'email-editor-plus' ) ] );
			} else {
				wp_send_json_success( [
					'message' => __( 'Custom block delete successfully.', 'email-editor-plus' )
				] );
			}
		}
		wp_send_json_error( [ 'message' => __( 'Basic check failed', 'email-editor-plus' ) ] );
	}

	/**
	 * Edit custom block title.
	 * @return void
	 */
	public static function editCustomBlock() {
		if ( ! WC::isSecurityValid( 'edpw-template-nonce' ) ) {
			wp_send_json_error( [ 'message' => __( 'Basic check failed', 'email-editor-plus' ) ] );
		}
		$id = (int) Input::get( 'id', 0 );
		if ( empty( $id ) ) {
			wp_send_json_error( [ 'message' => __( 'Basic check failed', 'email-editor-plus' ) ] );
		}
		$title = Input::get( 'title', '' );
		if ( empty( $title ) ) {
			wp_send_json_error( [
				'field_errors' => [
					'title' => __( 'This field required', 'email-editor-plus' )
				],
				'message'      => __( 'Basic check failed', 'email-editor-plus' )
			] );
		}
		if ( CustomBlocksModel::update( [ 'title' => $title, ], [ 'id' => $id ] ) ) {
			wp_send_json_success( [
				'message' => __( 'Custom block title changed successfully.', 'email-editor-plus' )
			] );
		}
		wp_send_json_error( [ 'message' => __( 'Custom block title change failed', 'email-editor-plus' ) ] );
	}
}