<?php
/**
 * @package Linguator
 */
namespace Linguator\Settings\Tables;

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

use Linguator\Includes\Other\Linguator_Language;
use WP_List_Table;



if ( ! class_exists( 'WP_List_Table' ) ) {
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; // since WP 3.1
}

/**
 * A class to create the languages table in Linguator settings
 *
 *  
 */
class Linguator_Table_Languages extends WP_List_Table {

	/**
	 * Constructor
	 *
	 *  
	 */
	public function __construct() {
		parent::__construct(
			array(
				'plural' => 'Languages', // Do not translate ( used for css class )
				'ajax'   => false,
			)
		);
	}

	/**
	 * Generates content for a single row of the table.
	 *
	 *  
	 *
	 * @param Linguator_Language $item The language item.
	 * @return void
	 */
	public function single_row( $item ) {
		/**
		 * Filter the list of classes assigned a row in the languages list table
		 *
		 *  
		 *
		 * @param array        $classes The list of class names.
		 * @param Linguator_Language $item    The language item.
		 */
		$classes = apply_filters( 'lmat_languages_row_classes', array(), $item );
		echo '<tr' . ( empty( $classes ) ? '>' : ' class="' . esc_attr( implode( ' ', $classes ) ) . '">' );
		$this->single_row_columns( $item );
		echo '</tr>';
	}

	/**
	 * Displays the item information in a column ( default case ).
	 *
	 *  
	 *
	 * @param Linguator_Language $item        The language item.
	 * @param string       $column_name The column name.
	 * @return string|int
	 */
	public function column_default( $item, $column_name ) {
		switch ( $column_name ) {
			case 'locale':
			case 'slug':
				return esc_html( $item->$column_name );

			case 'term_group':
				return (int) $item->$column_name;

			case 'count':
				return $item->get_tax_prop( 'lmat_language', $column_name );

			default:
				// Flag HTML is stored on the language object; keep output safe.
				return wp_kses(
					(string) $item->$column_name,
					array(
						'img' => array(
							'src'   => true,
							'alt'   => true,
							'class' => true,
							'width' => true,
							'height'=> true,
							'title' => true,
						),
						'span' => array(
							'class' => true,
							'title' => true,
						),
					)
				);
		}
	}

	/**
	 * Displays the item information in the column 'name'
	 * Displays the edit and delete action links
	 *
	 *  
	 *
	 * @param Linguator_Language $item The language item.
	 * @return string
	 */
	public function column_name( $item ) {
		return sprintf(
			'<a title="%s" href="%s">%s</a>',
			esc_attr__( 'Edit this language', 'translate-words' ),
			esc_url( wp_nonce_url( admin_url( 'admin.php?page=lmat&amp;lmat_action=edit&amp;lang=' . $item->term_id ), 'edit-lang' ) ),
			esc_html( $item->name )
		);
	}

	/**
	 * Displays the item information in the default language
	 * Displays the 'make default' action link
	 *
	 *  
	 *
	 * @param Linguator_Language $item The language item.
	 * @return string
	 */
	public function column_default_lang( $item ) {
		if ( ! $item->is_default ) {
			$s = sprintf(
				'<div class="row-actions"><span class="default-lang">
				<a class="icon-default-lang" title="%1$s" href="%2$s"><span class="screen-reader-text">%3$s</span></a>
				</span></div>',
				esc_attr__( 'Select as default language', 'translate-words' ),
				wp_nonce_url( '?page=lmat&amp;lmat_action=default-lang&amp;noheader=true&amp;lang=' . $item->term_id, 'default-lang' ),
				/* translators: accessibility text, %s is a native language name */
				esc_html( sprintf( __( 'Choose %s as default language', 'translate-words' ), $item->name ) )
			);

			/**
			 * Filters the default language row action in the languages list table.
			 *
			 *  
			 *
			 * @param string       $s    The html markup of the action.
			 * @param Linguator_Language $item The language item.
			 */
			$s = apply_filters( 'lmat_default_lang_row_action', $s, $item );
		} else {
			$s = sprintf(
				'<span class="icon-default-lang"><span class="screen-reader-text">%1$s</span></span>',
				/* translators: accessibility text */
				esc_html__( 'Default language', 'translate-words' )
			);
		}

		return $s;
	}

	/**
	 * Gets the list of columns
	 *
	 *  
	 *
	 * @return string[] The list of column titles.
	 */
	public function get_columns() {
		return array(
			'name'         => esc_html__( 'Full name', 'translate-words' ),
			'locale'       => esc_html__( 'Locale', 'translate-words' ),
			'slug'         => esc_html__( 'Code', 'translate-words' ),
			'default_lang' => sprintf( '<span title="%1$s" class="icon-default-lang"><span class="screen-reader-text">%2$s</span></span>', esc_attr__( 'Default language', 'translate-words' ), esc_html__( 'Default language', 'translate-words' ) ),
			'term_group'   => esc_html__( 'Order', 'translate-words' ),
			'flag'         => esc_html__( 'Flag', 'translate-words' ),
			'count'        => esc_html__( 'Posts', 'translate-words' ),
		);
	}

	/**
	 * Gets the list of sortable columns
	 *
	 *  
	 *
	 * @return array
	 */
	public function get_sortable_columns() {
		return array(
			'name'       => array( 'name', true ), // sorted by name by default
			'locale'     => array( 'locale', false ),
			'slug'       => array( 'slug', false ),
			'term_group' => array( 'term_group', false ),
			'count'      => array( 'count', false ),
		);
	}

	/**
	 * Gets the name of the default primary column.
	 *
	 *  
	 *
	 * @return string Name of the default primary column, in this case, 'name'.
	 */
	protected function get_default_primary_column_name() {
		return 'name';
	}

	/**
	 * Generates and display row actions links for the list table.
	 *
	 *  
	 *
	 * @param Linguator_Language $item        The language item being acted upon.
	 * @param string       $column_name Current column name.
	 * @param string       $primary     Primary column name.
	 * @return string The row actions output.
	 */
	protected function handle_row_actions( $item, $column_name, $primary ) {
		if ( $primary !== $column_name ) {
			return '';
		}

		$actions = array(
			'edit'   => sprintf(
				'<a title="%s" href="%s">%s</a>',
				esc_attr__( 'Edit this language', 'translate-words' ),
				esc_url( wp_nonce_url( admin_url( 'admin.php?page=lmat&amp;lmat_action=edit&amp;lang=' . $item->term_id ), 'edit-lang' ) ),
				esc_html__( 'Edit', 'translate-words' )
			),
			'delete' => sprintf(
				'<a title="%s" href="%s" onclick = "return confirm( \'%s\' );">%s</a>',
				esc_attr__( 'Delete this language and all its associated data', 'translate-words' ),
				wp_nonce_url( '?page=lmat&amp;lmat_action=delete&amp;noheader=true&amp;lang=' . $item->term_id, 'delete-lang' ),
				esc_js( __( 'You are about to permanently delete this language. Are you sure?', 'translate-words' ) ),
				esc_html__( 'Delete', 'translate-words' )
			),
		);

		/**
		 * Filters the list of row actions in the languages list table.
		 *
		 *  
		 *
		 * @param array        $actions A list of html markup actions.
		 * @param Linguator_Language $item    The language item.
		 */
		$actions = apply_filters( 'lmat_languages_row_actions', $actions, $item );

		return $this->row_actions( $actions );
	}

	/**
	 * Sorts language items.
	 *
	 *  
	 *
	 * @param Linguator_Language $a The first language to compare.
	 * @param Linguator_Language $b The second language to compare.
	 * @return int -1 or 1 if $a is considered to be respectively less than or greater than $b.
	 */
	protected function usort_reorder( $a, $b ) {
		$orderby = ! empty( $_GET['orderby'] ) ? sanitize_key( wp_unslash( $_GET['orderby'] ) ) : 'name'; // phpcs:ignore WordPress.Security.NonceVerification
		$order   = ! empty( $_GET['order'] ) ? sanitize_key( wp_unslash( $_GET['order'] ) ) : 'asc'; // phpcs:ignore WordPress.Security.NonceVerification
		$order   = in_array( $order, array( 'asc', 'desc' ), true ) ? $order : 'asc';
		// Determine sort order
		if ( is_numeric( $a->$orderby ) ) {
			$result = $a->$orderby > $b->$orderby ? 1 : -1;
		} else {
			$result = strcmp( $a->$orderby, $b->$orderby );
		}
		// Send final sort direction to usort.
		return ( 'asc' === $order ) ? $result : -$result;
	}

	/**
	 * Prepares the list of languages for display.
	 *
	 *  
	 *
	 * @param Linguator_Language[] $data The list of languages.
	 * @return void
	 */
	public function prepare_items( $data = array() ) {
		$per_page = $this->get_items_per_page( 'lmat_lang_per_page' );
		$this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() );

		usort( $data, array( $this, 'usort_reorder' ) );

		$total_items = count( $data );
		$this->items = array_slice( $data, ( $this->get_pagenum() - 1 ) * $per_page, $per_page );

		$this->set_pagination_args(
			array(
				'total_items' => $total_items,
				'per_page'    => $per_page,
				'total_pages' => (int) ceil( $total_items / $per_page ),
			)
		);
	}
}
