<?php
/**
 * Admin Settings for Native Blocks
 */

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

/**
 * Add admin menu
 */
function ntvb_add_admin_menu() {
	add_options_page(
		__( 'Native Blocks', 'native-blocks' ),
		__( 'Native Blocks', 'native-blocks' ),
		'manage_options',
		'native-blocks-settings',
		'ntvb_settings_page'
	);
}
add_action( 'admin_menu', 'ntvb_add_admin_menu' );

/**
 * Initialize settings
 */
function ntvb_settings_init() {
	register_setting( 'ntvb_settings', 'ntvb_breakpoints', array(
		'sanitize_callback' => 'ntvb_sanitize_breakpoints'
	) );


	add_settings_section(
		'ntvb_breakpoints_section',
		__( 'Responsive Breakpoints', 'native-blocks' ),
		'ntvb_breakpoints_section_callback',
		'ntvb_settings'
	);

	add_settings_field(
		'mobile_breakpoint',
		__( 'Mobile Breakpoint (px)', 'native-blocks' ),
		'ntvb_mobile_breakpoint_render',
		'ntvb_settings',
		'ntvb_breakpoints_section'
	);

	add_settings_field(
		'tablet_breakpoint',
		__( 'Tablet Breakpoint (px)', 'native-blocks' ),
		'ntvb_tablet_breakpoint_render',
		'ntvb_settings',
		'ntvb_breakpoints_section'
	);
}
add_action( 'admin_init', 'ntvb_settings_init' );

/**
 * Section callback
 */
function ntvb_breakpoints_section_callback() {
	echo '<p>' . esc_html( __( 'Set your responsive breakpoints. These will be used across all Native Blocks.', 'native-blocks' ) ) . '</p>';
}

/**
 * Render mobile breakpoint field
 */
function ntvb_mobile_breakpoint_render() {
	$breakpoints = get_option( 'ntvb_breakpoints', ntvb_get_default_breakpoints() );
	echo '<input type="number" name="ntvb_breakpoints[mobile]" value="' . esc_attr( $breakpoints['mobile'] ) . '" min="1" max="9999" placeholder="767"/>';
	echo '<p class="description">' . esc_html__( 'Maximum width for mobile devices', 'native-blocks' ) . '</p>';
}

/**
 * Render tablet breakpoint field
 */
function ntvb_tablet_breakpoint_render() {
	$breakpoints = get_option( 'ntvb_breakpoints', ntvb_get_default_breakpoints() );
	echo '<input type="number" name="ntvb_breakpoints[tablet]" value="' . esc_attr( $breakpoints['tablet'] ) . '" min="1" max="9999" placeholder="1024"/>';
	echo '<p class="description">' . esc_html__( 'Maximum width for tablet devices', 'native-blocks' ) . '</p>';
}


/**
 * Settings page content
 */
function ntvb_settings_page() {
	?>
	<div class="wrap">
		<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
		<div class="explore-products" style="display: flex; align-items: center; margin-bottom: 20px;">
			<?php esc_html_e( 'Explore our products:', 'native-blocks' ); ?>
			<a href="https://gutslider.com" target="_blank" class="button button-secondary" style="margin-left: 10px;"><?php esc_html_e( 'GutSlider', 'native-blocks' ); ?></a>
			<a href="https://accordion.gutenbergkits.com" target="_blank" class="button button-secondary" style="margin-left: 10px;"><?php esc_html_e( 'Easy Accordion', 'native-blocks' ); ?></a>
			<a href="https://gutenlayouts.com" target="_blank" class="button button-secondary" style="margin-left: 10px;"><?php esc_html_e( 'Guten Layouts', 'native-blocks' ); ?></a>
		</div>
		<form action="options.php" method="post">
			<?php
				settings_fields( 'ntvb_settings' );
				do_settings_sections( 'ntvb_settings' );
				submit_button();
			?>
		</form>
	</div>
	<?php
}


/**
 * Get default breakpoints
 */
function ntvb_get_default_breakpoints() {
	return array(
		'mobile' => '767',
		'tablet' => '1024',
	);
}



/**
 * Sanitize breakpoints
 */
function ntvb_sanitize_breakpoints( $input ) {
	$defaults = ntvb_get_default_breakpoints();
	$output = array();

	foreach ( $defaults as $key => $value ) {
		$output[ $key ] = isset( $input[ $key ] ) ? absint( $input[ $key ] ) : $value;
	}

	return $output;
}	
