<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
if ( ! function_exists( 'cf7cmlsIsMoanaActivated' ) ) {
	function cf7cmlsIsMoanaActivated() {
		return defined( 'CF7_VC_DIR' );
	}
}
// Add Go Pro Action Link
add_filter( 'plugin_action_links_' . CF7MLS_PLUGIN_BASENAME, 'cf7mls_plugin_action_links' );
function cf7mls_plugin_action_links( $links ) {
	$links[] = '<a target="_blank" href="https://1.envato.market/CF7-Multi-Step-Go-Pro" style="color: #43B854; font-weight: bold">' . __( 'Go Pro', 'cf7-multi-step' ) . '</a>';
	return $links;
}
// Add Document link
add_filter( 'plugin_row_meta', 'cf7mls_plugin_row_meta', 10, 2 );
function cf7mls_plugin_row_meta( $links, $file ) {
	if ( strpos( $file, 'contact-form-7-multi-step.php' ) !== false ) {
		$new_links = array(
			'doc' => '<a href="https://ninjateam.org/contact-form-7-multi-step-pro-doc/" target="_blank">' . __( 'Documentation', 'cf7-multi-step' ) . '</a>',
		);

		$links = array_merge( $links, $new_links );
	}
	return $links;
}

add_filter( 'wpcf7_editor_panels', 'cf7mls_wpcf7_editor_panels' );
function cf7mls_wpcf7_editor_panels( $panels ) {
	$panels['form-panel'] = array(
		'title'    => __( 'Form', 'contact-form-7' ),
		'callback' => 'cf7mls_wpcf7_editor_panel_form',
	);
	return $panels;
}

function cf7mls_wpcf7_editor_panel_form( $post ) {
	$desc_link   = wpcf7_link(
		__( 'https://contactform7.com/editing-form-template/', 'contact-form-7' ),
		__( 'Editing Form Template', 'contact-form-7' )
	);
	$description = __( 'You can edit the form template here. For details, see %s.', 'contact-form-7' );
	$description = sprintf( esc_html( $description ), $desc_link );
	?>

  <h2 class="cf7mls-title-form"><?php echo esc_html( __( 'Form', 'contact-form-7' ) ); ?></h2>
  <fieldset class="cf7mls-wrap-form">
	<legend class="cf7mls-description-form"><?php echo $description; ?></legend>
	<?php
	  $tag_generator = WPCF7_TagGenerator::get_instance();
	  $tag_generator->print_buttons();
	?>
	<textarea id="wpcf7-form" name="wpcf7-form" cols="100" rows="24" class="large-text code" data-config-field="form.body"><?php echo esc_textarea( $post->prop( 'form' ) ); ?></textarea>
	<div id="cf7mls-app"></div>
	<div id="cf7mls_PostBoxUpgradePro" style="display:none;"></div>
  </fieldset>
  
	<?php
}

// add js, css
add_action( 'admin_enqueue_scripts', 'cf7mls_admin_scripts_callback' );
function cf7mls_admin_scripts_callback( $hook_suffix ) {
	$load_js_css = false;
	if ( ( substr( $hook_suffix, -15 ) == '_page_wpcf7-new' ) || ( $hook_suffix == 'toplevel_page_wpcf7' ) ) {
		$load_js_css = true;
	}
	if ( true === $load_js_css ) {
		wp_enqueue_style( 'wp-color-picker' );
		wp_enqueue_script( 'wp-color-picker' );

		wp_enqueue_style( 'vue-css', CF7MLS_PLUGIN_URL . 'assets/dist/css/main.css', array(), CF7MLS_NTA_VERSION );
		wp_register_script( 'vue-js', CF7MLS_PLUGIN_URL . 'assets/dist/js/main.js', array( 'jquery' ), CF7MLS_NTA_VERSION, true );
		wp_enqueue_script( 'vue-js' );
		wp_localize_script(
			'vue-js',
			'cf7_mls_vue_js',
			array(
				'textInputStep'           => __( 'Step Name', 'cf7-multi-step' ),
				'textInputBack'           => __( 'Back Button', 'cf7-multi-step' ),
				'textInputNext'           => __( 'Next Button', 'cf7-multi-step' ),
				'textEditMoana'           => __( 'Edit With Moana', 'cf7-multi-step' ),
				'cf7cmlsIsMoanaActivated' => cf7cmlsIsMoanaActivated(),
				'pluginUrl'               => CF7MLS_PLUGIN_URL,
				'isCF7Version6'           => version_compare( WPCF7_VERSION, '6.0', '>=' ),
			)
		);

		wp_register_script( 'cf7mls', CF7MLS_PLUGIN_URL . 'assets/admin/js/cf7mls.js', array( 'jquery' ), CF7MLS_NTA_VERSION, true );
		wp_enqueue_script( 'cf7mls' );

		$form_content = '';
		$manager      = WPCF7_FormTagsManager::get_instance();
		if ( isset( $_GET['post'] ) && (int) $_GET['post'] > 0 ) {
			$form = WPCF7_ContactForm::get_instance( sanitize_text_field( $_GET['post'] ) );
			if ( $form ) {
				$form_content = $form->prop( 'form' );
			} else {
				$form_content = WPCF7_ContactFormTemplate::get_default( 'form' );
			}
		} else {
			$form_content = WPCF7_ContactFormTemplate::get_default( 'form' );
		}
			$scan = $manager->scan( $form_content );

			$steps = array();

			$numberStep = 0;
		foreach ( $scan as $k => $v ) {
			if ( 'cf7mls_step' == $v->type ) {
				if ( count( $v->values ) == 2 ) {
						$numberStep = (int) ( explode( '-', $v->name )[1] );

					if ( $numberStep === 1 ) {
						$steps[] = array(
							'back'  => '',
							'next'  => $v->values[0],
							'title' => $v->values[1],
						);
					} else {
						$steps[] = array(
							'back'  => $v->values[0],
							'next'  => '',
							'title' => $v->values[1],
						);
					}
				} elseif ( count( $v->values ) == 3 ) {
							  $steps[] = array(
								  'back'  => $v->values[0],
								  'next'  => $v->values[1],
								  'title' => $v->values[2],
							  );
				}
			}
		}
			wp_localize_script(
				'cf7mls',
				'cf7mls',
				array(
					'steps'      => $steps,
					'cf7mls_app' => null,
				)
			);

		wp_register_style( 'cf7mls', CF7MLS_PLUGIN_URL . 'assets/admin/css/cf7mls.css' );
		wp_enqueue_style( 'cf7mls' );

		wp_register_style( 'cf7mls_progress_bar', CF7MLS_PLUGIN_URL . 'assets/frontend/css/progress_bar.css' );
		wp_enqueue_style( 'cf7mls_progress_bar' );
	}
}
