<?php

class Wiaas_Admin_Delivery_Process_List {

	public static function init() {

		add_filter('gform_form_list_columns', array( __CLASS__, 'filter_gform_form_list_columns' ));

		add_filter('gform_form_list_forms', array( __CLASS__, 'filter_gform_form_list_forms' ), 10, 6);

		add_filter('gform_form_actions', array( __CLASS__, 'filter_gform_form_actions' ), 10, 2);

		add_action('gform_form_list_column_wiaas_country', array( __CLASS__, 'render_gform_form_list_wiaas_country_column' ));

		add_action('gform_form_list_column_wiaas_actions', array( __CLASS__, 'render_gform_form_list_wiaas_actions_column' ));

	}

	/**
     * Hide not needed columns for delivery process list
     * Add country column
     * Add actions column
     *
	 * @param array $columns
	 *
	 * @return mixed
	 */
	public static function filter_gform_form_list_columns($columns) {

		unset($columns['entry_count']);
		unset($columns['conversion']);
		unset($columns['view_count']);

		$columns['wiaas_country'] =  esc_html__( 'Country', 'wiaas' );
		$columns['wiaas_actions'] =  esc_html__( 'Actions', 'wiaas' );

		return $columns;
	}

	/**
     * Filter delivery process forms list with search query by title and country
	 * @param array $forms
	 * @param string $search_query
	 * @param bool $active
	 * @param string $sort_column
	 * @param string $sort_direction
	 * @param bool $trash
	 *
	 * @return array
	 */
	public static function filter_gform_form_list_forms($forms, $search_query, $active, $sort_column, $sort_direction, $trash) {

		$forms = GFFormsModel::get_forms( $active, $sort_column, $sort_direction, $trash );

		if ( ! rgblank( $search_query ) ) {

			$filtered_forms = array();
			$search_query = strtolower($search_query);

			// filter forms
			foreach ($forms as $form) {

				$form_details = GFAPI::get_form( $form->id );

				$title = strtolower($form_details['title']);

				$delivery_settings = rgar($form_details, 'wiaas_delivery_process');
				$country_code = ! empty($delivery_settings) ? $delivery_settings['delivery_country'] : '';
				$country_name = Wiaas_Countries::get_available_country_name_by_code($country_code);
				$country_name = strtolower($country_name);

				if (strpos($title, $search_query) !== false || strpos($country_code, $search_query) !== false ||
				    strpos($country_name, $search_query) !== false) {
					$filtered_forms[] = $form;
				}
			}

			return $filtered_forms;
		}

		return $forms;
	}

	/**
     * Remove unused actions and add workflow action for delivery forms list
     *
	 * @param array $actions
	 * @param int $form_id
	 *
	 * @return mixed
	 */
	public static function filter_gform_form_actions($actions, $form_id) {
		unset($actions['entries']);
		unset($actions['preview']);
		unset($actions['edit']);

		$actions['wiaas_workflow'] =  array(
			'label'        => __( 'Workflow', 'wiaas' ),
			'short_label'  => esc_html__( 'Workflow', 'wiaas' ),
			'title'        => __( 'Edit workflow', 'wiaas' ),
			'url'          => '?page=gf_edit_forms&view=settings&id=' . $form_id . '&subview=gravityflow',
			'priority'     => 1000,
		);

		return $actions;
	}

	/**
     * Render country column for delivery process forms list
     *
	 * @param mixed $form
	 */
	public static function render_gform_form_list_wiaas_country_column($form) {

		$form_details = GFAPI::get_form($form->id);
		$delivery_settings = rgar($form_details, 'wiaas_delivery_process');

		$country_code = ! empty($delivery_settings) ? $delivery_settings['delivery_country'] : '';

		$country_name = Wiaas_Countries::get_available_country_name_by_code($country_code);

		echo '<strong>' . esc_html_e($country_name) . '</strong>';
	}

	/**
     * Render actions column for delivery process forms list
	 * @param mixed $form
	 */
	public static function render_gform_form_list_wiaas_actions_column($form) {

		$form_details = GFAPI::get_form($form->id);
		$delivery_settings = rgar($form_details, 'wiaas_delivery_process');

		$form_type = ! empty($delivery_settings) ? $delivery_settings['delivery_form_type'] : '';

		?>
		<a href="<?php echo '?page=gf_edit_forms&view=settings&id=' . $form->id . '&subview=gravityflow' ?>">Workflow</a>
		<?php

		if ($form_type !== 'action') {

			?>
			<span style="margin: 0 10px; opacity: 0.3;"> | </span>
			<a href="<?php echo '?page=gf_edit_forms&view=settings&id=' . $form->id . '&subview=wiaas_delivery_process' ?>">Change Country</a>
			<?php
		}
	}
}

Wiaas_Admin_Delivery_Process_List::init();
