<?php
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Add admin notice for timeline express migration
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
function ctl_admin_notice_for_migration() {

	// Check if we're on the Cool Timeline settings page (read-only screen detection).
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	if ( ! isset( $_GET['page'] ) || 'cool_timeline_settings' !== sanitize_key( wp_unslash( $_GET['page'] ) ) ) {
		return;
	}
    // Check if timeline express is installed and migration is not completed
    if (file_exists(WP_PLUGIN_DIR . '/timeline-express/timeline-express.php')) {
        $migration_completed = get_option('timeline_express_migrated');
        
        // Only show notice if migration is not completed
        if (!$migration_completed) {
            ?>
            <div class="notice  ctl_migration notice-info is-dismissible">
                <div class="migration_message_container">
                    <p>
                        <?php echo esc_html__('Timeline Express plugin is installed on your site. To move your announcements into Cool Timeline, you can now start the migration process.', 'cool-timeline'); ?> 
                        <a href="admin.php?page=cool_timeline_settings#tab=migration-settings" class="button button-small ctl_migration_btn"><?php echo esc_html__('Start Migration', 'cool-timeline'); ?></a>
                    </p>
                </div>
            </div>
            <?php
        }
    }
}
add_action('admin_notices', 'ctl_admin_notice_for_migration');

// Control core classes for avoid errors
if ( class_exists( 'CSF' ) ) {
	
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$prefix = 'cool_timeline_settings';
	include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

	// Create options
	CSF::createOptions(
		$prefix,
		array(
			'framework_title'    => 'Timeline Settings',
			'menu_title'         => 'Timeline Settings',
			'menu_slug'          => 'cool_timeline_settings',
			'menu_type'          => 'submenu',
			'menu_parent'        => 'cool-plugins-timeline-addon',
			'menu_capability'    => 'manage_options', // The capability needed to view the page
			'menu_icon'          => CTL_PLUGIN_URL . 'assets/images/cool-timeline-icon.svg',
			'menu_position'      => 6,
			'nav'                => 'inline',
			'show_reset_section' => false,
			'show_reset_all'     => false,
			'show_bar_menu'      => false,

			'footer_text'        => '',
		)
	);

	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$fields=array(



				// Create a Fieldset
				array(
					'id'     => 'timeline_header',
					'type'   => 'fieldset',
					'title'  => 'Content Before Timeline',
					'fields' => array(
						array(
							'id'    => 'title_text',
							'type'  => 'text',
							'title' => __( 'Timeline Title', 'cool-timeline' ),
						),
					),
				), // End Fieldset


			// Create a Fieldset
				array(
					'id'     => 'story_content_settings',
					'type'   => 'fieldset',
					'title'  => 'Story Content',
					'fields' => array(
						array(
							'id'      => 'content_length',
							'type'    => 'text',
							'title'   => __( 'Content Length', 'cool-timeline' ),
							'default' => '50',
							'desc'    => __( 'Please enter no of words', 'cool-timeline' ),
						),
						array(
							'id'      => 'display_readmore',
							'type'    => 'radio',
							'title'   => __( 'Display read more?', 'cool-timeline' ),
							'options' => array(
								'yes' => __( 'Yes', 'cool-timeline' ),
								'no'  => __( 'No', 'cool-timeline' ),
							),
							'desc'    => __( 'It will also disable link from story title.', 'cool-timeline' ),
							'inline'  => true,
							'default' => 'yes',
						),
						array(
							'id'         => 'story_link_target',
							'type'       => 'radio',
							'title'      => __( 'Open read more link in?', 'cool-timeline' ),
							'options'    => array(
								'_self'  => __( 'Same Tab', 'cool-timeline' ),
								'_blank' => __( 'New Tab', 'cool-timeline' ),
							),
							'inline'     => true,
							'default'    => '_self',
							'dependency' => array( 'display_readmore', '==', 'yes' ),
						),

					),
				), // End Fieldset


				array(
					'id'     => 'story_date_settings',
					'type'   => 'fieldset',
					'title'  => 'Story Date',
					'fields' => array(

						array(
							'id'         => 'year_label_visibility',
							'type'       => 'switcher',
							'title'      => __( 'Year Label', 'cool-timeline' ),
							'text_on'    => __( 'Show', 'cool-timeline' ),
							'text_off'   => __( 'Hide', 'cool-timeline' ),
							'text_width' => 100,
							'default'    => true,
							'desc'       => __( 'Only for Vertical and One sided layout', 'cool-timeline' ),
						),

					),
				), // End Fieldset

				array(
					'id'      => 'first_story_position',
					'type'    => 'button_set',
					'title'   => 'Vertical Timeline Stories Starts From',
					'desc'    => 'Not for Compact and Horizontal layout',
					'options' => array(
						'left'  => 'Left',
						'right' => 'Right',
					),
					'default' => 'right',
				)
                
			);

					// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
					$review_option = get_option( 'cpfm_opt_in_choice_cool-timeline' );
					
					if($review_option){

		// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
		$fields[]= array(
			'id'      => 'ctl_cpfm_feedback_data',
			'type'    => 'checkbox',
			// phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
			'title'   => __('Usage Data Sharing', 'ccpw1'),
			'default' => $review_option === 'yes' ? true : false,
			'desc'    => 'Help us make this plugin more compatible with your site by sharing non-sensitive site data. 
				<a href="#" class="cpfm-see-terms">[See terms]</a>
				<div id="termsBox" style="display: none; margin-top: 10px; ">
					' . sprintf(
       // translators: %s: minimum number of characters
        __('Opt in to receive email updates about security improvements, new features, helpful tutorials, and occasional special offers. We\'ll collect: <a href="%s" target="_blank" rel="noopener noreferrer">click here</a>', 'ccpw'), // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch 
        esc_url('https://my.coolplugins.net/terms/usage-tracking/')
    ) . '
					<ul class="ctl_data_share" >
						' . 
						// phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
						'<li>' . esc_html__('1. Your website home URL and WordPress admin email.', 'ccpw') . '</li>
						' . 
						// phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
						'<li>' . esc_html__('2. To check plugin compatibility, we will collect the following: list of active plugins and themes, server type, MySQL version, WordPress version, memory limit, site language and database prefix.', 'ccpw') . '</li>
					</ul>
				</div>',
		);
	}


	// Create a section
	CSF::createSection(
		$prefix,
		array(
			'title'  => 'General Settings',
		'fields'=>$fields
		));


	


	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$timeline_express_installed = file_exists(WP_PLUGIN_DIR . '/timeline-express/timeline-express.php');
	
	
	
	if ($timeline_express_installed  && ! get_option( 'timeline_express_migrated')) {
		CSF::createSection(
			$prefix,
			array(
				'title'  => 'Migration Settings',
				'fields' => array(
					array(
						'id'     => 'migration_fieldset',
						'type'   => 'fieldset',
						'title'  => 'Timeline Express Migration',
						'fields' => array(
							array(
								'id'      => 'migrate_stories',
								'type'    => 'content',								
									'content' => '
										<div class="ctl-buttons-migrate">
										<button class="button button-primary ctl-migrate">Migrate Content</button>
										<div class="ctl-progress-bar">
											<div class="ctl-progress-bar-inner">
											</div>
										</div>
									</div>',

								'desc'    => is_plugin_active('timeline-express/timeline-express.php') 
									? 'Timeline Express is active. You can migrate stories now.' 
									: 'Timeline Express is installed but not active. Please activate it to migrate stories.',
								
								
							)
						)
					)
				)
			)
		);
	}

	// Create a section
	CSF::createSection(
		$prefix,
		array(
			'title'  => 'Style Settings',
			'fields' => array(

				array(
					'id'      => 'content_bg_color',
					'type'    => 'color',
					'title'   => 'Story Background Color',
					'default' => '#ffffff',
				),

				array(
					'id'      => 'circle_border_color',
					'type'    => 'color',
					'title'   => 'Circle Color',
					'default' => '#38aab7',
				),

				array(
					'id'      => 'line_color',
					'type'    => 'color',
					'title'   => 'Line Color',
					'default' => '#025149',
				),

				array(
					'id'      => 'first_post',
					'type'    => 'color',
					'title'   => 'First Color',
					'default' => '#29b246',
				),

				array(
					'id'      => 'second_post',
					'type'    => 'color',
					'title'   => 'Second Color',
					'default' => '#ce792f',
				),

				array(
					'id'       => 'custom_styles',
					'type'     => 'code_editor',
					'title'    => 'Custom Styles',
					'settings' => array(
						'theme' => 'mbo',
						'mode'  => 'css',
					),

				),

			),
		)
	);

	// Create a section
	CSF::createSection(
		$prefix,
		array(
			'title'  => 'Typography Setings',
			'fields' => array(

				array(
					'id'         => 'ctl_date_typo',
					'type'       => 'typography',
					'title'      => 'Story Date',
					'default'    => array(
						'font-family' => 'Maven Pro',
						'font-size'   => '21',
						'line-height' => '',
						'unit'        => 'px',
						'type'        => 'google',
						'font-weight' => '700',
					),
					'text_align' => false,
					'color'      => false,
				),

				array(
					'id'      => 'post_title_typo',
					'type'    => 'typography',
					'title'   => 'Story Title',
					'default' => array(
						'font-family' => 'Maven Pro',
						'font-size'   => '20',
						'line-height' => '',
						'unit'        => 'px',
						'type'        => 'google',
						'font-weight' => '700',
					),
					'color'   => false,
				),

				array(
					'id'      => 'post_content_typo',
					'type'    => 'typography',
					'title'   => 'Post Content',
					'default' => array(
						'font-family' => 'Maven Pro',
						'font-size'   => '16',
						'line-height' => '',
						'unit'        => 'px',
						'type'        => 'google',
					),
					'color'   => false,
				),


			),


		)
	);

	// Create a section
	CSF::createSection(
		$prefix,
		array(
			'title'  => 'Advance Settings',
			'fields' => array(

				array(
					'id'      => 'advanced-features',
					'type'    => 'content',
					'content' => '<div class="advance_options" style="text-align:center"><a target="_blank" href="' . esc_url( CTL_BUY_PRO . '?utm_source=ctl_plugin&utm_medium=inside&utm_campaign=get_pro&utm_content=settings' ) . '">
          <img src="' . esc_url( CTL_PLUGIN_URL . 'assets/images/pro-features-list.png' ) . '" alt=""></a></div>',

				),

			),
		)
	);


}
