<?php
/**
 * Class Custom_Fields_Home
 *
 * @package dvcrs
 */

namespace DVCRS\Core;

use \Carbon_Fields\Container;
use \Carbon_Fields\Field;

if ( ! defined( 'ABSPATH' ) ) {
	die();
}

/**
 * Custom_Fields_Home Class
 */
class Custom_Fields_Home {

	/**
	 * Custom_Fields_Home constructor
	 */
	public function __construct() {

		\add_filter( 'use_block_editor_for_post_type', [ $this, 'disable_gutenberg_on_home_page' ] );
		\add_action( 'carbon_fields_register_fields', [ $this, 'register_fields' ], 10, 1 );

	}

	public function disable_gutenberg_on_home_page( $can_edit ) {

		if ( 'page' !== get_post_type() ) {
			return $can_edit;
		}

		if ( get_the_ID() === intval( get_option( 'page_on_front' ) ) ) {
			$can_edit = false;
			remove_post_type_support( 'page', 'editor' );
		}

		return $can_edit;
	}

	/**
	 * Register custom fields
	 */
	public function register_fields() {

		Container::make( 'post_meta', __( 'Content', 'dvcrs-core' ) )
				 ->where( 'post_id', '=', get_option( 'page_on_front' ) )
				->add_fields(
					[
						Field::make( 'text', 'video', __( 'Home Video', 'dvcrs-core' ) ),
						Field::make( 'rich_text', 'intro', __( 'Intro', 'dvcrs-core' ) ),
						Field::make( 'html', 'hiw_section_title', __( 'How it Works', 'dvcrs-core' ) )
							 ->set_html( sprintf( '<h1>%s</h1>', __( 'How it Works', 'dvcrs-core' ) ) ),
						Field::make( 'text', 'hiw_pretitle', __( 'Pretitle', 'dvcrs-core' ) ),
						Field::make( 'text', 'hiw_title', __( 'Title', 'dvcrs-core' ) ),
						Field::make( 'complex', 'hiw_cards', __( 'Cards', 'dvcrs-core' ) )
							->add_fields(
								[
									Field::make( 'text', 'title', __( 'Title', 'dvcrs-core' ) ),
									Field::make( 'text', 'text', __( 'Text', 'dvcrs-core' ) ),
									Field::make( 'image', 'photo', __( 'Photo', 'dvcrs-core' ) ),
									Field::make( 'text', 'url', __( 'Link', 'dvcrs-core' ) ),
									Field::make( 'text', 'button_text', __( 'Button Text', 'dvcrs-core' ) ),
								]
							),
						Field::make( 'html', 'diff_section_title', __( 'The DVCRS Difference', 'dvcrs-core' ) )
							 ->set_html( sprintf( '<h1>%s</h1>', __( 'The DVCRS Difference', 'dvcrs-core' ) ) ),
						Field::make( 'text', 'diff_title', __( 'Title', 'dvcrs-core' ) ),
						Field::make( 'complex', 'diff_cards', __( 'Cards', 'dvcrs-core' ) )
							->add_fields(
								[
									Field::make( 'image', 'icon', __( 'Icon', 'dvcrs-core' ) ),
									Field::make( 'text', 'title', __( 'Title', 'dvcrs-core' ) ),
									Field::make( 'text', 'text', __( 'Text', 'dvcrs-core' ) ),
									Field::make( 'text', 'url', __( 'Link', 'dvcrs-core' ) )
										  ->set_attribute( 'type', 'url' ),
								]
							),
						Field::make( 'html', 'rec_section_title', __( 'Affiliate Logos', 'dvcrs-core' ) )
							 ->set_html( sprintf( '<h1>%s</h1>', __( 'Affiliate Logos', 'dvcrs-core' ) ) ),
						Field::make( 'text', 'rec_title', __( 'Title', 'dvcrs-core' ) ),
						Field::make( 'complex', 'rec_logos', __( 'Logos', 'dvcrs-core' ) )
							->add_fields(
								[
									Field::make( 'image', 'logo', __( 'Logo', 'dvcrs-core' ) ),
									Field::make( 'text', 'url', __( 'URL', 'dvcrs-core' ) )
										 ->set_attribute( 'type', 'url' ),
								]
							),
						Field::make( 'html', 'test_section_title', __( 'Client Testimonials', 'dvcrs-core' ) )
							 ->set_html( sprintf( '<h1>%s</h1>', __( 'Client Testimonials', 'dvcrs-core' ) ) ),
						Field::make( 'text', 'test_title', __( 'Title', 'dvcrs-core' ) ),
						Field::make( 'text', 'test_intro', __( 'Intro', 'dvcrs-core' ) ),
						Field::make( 'complex', 'testimonials', __( 'Testimonials', 'dvcrs-core' ) )
							->add_fields(
								[
									Field::make( 'image', 'avatar', __( 'Avatar', 'dvcrs-core' ) ),
									Field::make( 'text', 'quote', __( 'Quote', 'dvcrs-core' ) ),
									Field::make( 'text', 'name', __( 'Name', 'dvcrs-core' ) ),
								]
							),
						Field::make( 'html', 'blog_section_title', __( 'Blog', 'dvcrs-core' ) )
							 ->set_html( sprintf( '<h1>%s</h1>', __( 'Blog', 'dvcrs-core' ) ) ),
						Field::make( 'text', 'blog_title', __( 'Title', 'dvcrs-core' ) ),
						Field::make( 'text', 'blog_button_text', __( 'Button Text', 'dvcrs-core' ) ),
						Field::make( 'html', 'cta_section_title', __( 'Call to Action', 'dvcrs-core' ) )
							 ->set_html( sprintf( '<h1>%s</h1>', __( 'Call to Action', 'dvcrs-core' ) ) ),
						Field::make( 'text', 'cta_title', __( 'Title', 'dvcrs-core' ) ),
						Field::make( 'text', 'cta_text', __( 'Text', 'dvcrs-core' ) ),
						Field::make( 'text', 'cta_button_text', __( 'Button Text', 'dvcrs-core' ) )
							 ->set_width( 50 ),
						Field::make( 'text', 'cta_url', __( 'URL', 'dvcrs-core' ) )
							 ->set_attribute( 'type', 'url' )
							 ->set_width( 50 ),
						Field::make( 'image', 'cta_image', __( 'Background Image', 'dvcrs-core' ) ),
					]
				);
	}
}

new Custom_Fields_Home();
