<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;

/**
 * Custom columns.
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Custom_Column extends Base implements JIT_Interface {

    /**
     * Add new column to the posts list.
     *
     * @access public
     *
     * @param array $columns Columns to add new column.
     * @return array $columns New columns.
     */
    public function add_column( $columns ) {
        $new_columns = array();
        foreach ( $columns as $key => $value ) {
            if ( 'title' === $key ) {
                $new_columns[ $key ]    = $value;
                $new_columns['type']    = __( 'Type', 'floating-awesome-button' );
                $new_columns['clicked'] = __( 'Clicked', 'floating-awesome-button' );
            } else {
                $new_columns[ $key ] = $value;
            }
        }
        return $new_columns;
    }

    /**
     * Fill the column with data.
     *
     * @access public
     *
     * @param string $column Column name.
     * @param int    $post_id Post ID.
     * @return void Echo the data.
     */
    public function fill_column( $column, $post_id ) {
        if ( 'clicked' === $column ) {
            $clicked = get_post_meta( $post_id, 'fab_total_clicked', true );
            $clicked = $clicked ? $clicked : 0;
            echo esc_html( $clicked );
        } elseif ( 'type' === $column ) {
            $type = get_post_meta( $post_id, 'fab_setting_type', true );
            $type = $type ? $type : __( 'Default', 'floating-awesome-button' );
            echo esc_html( $type );
        }
    }

    /**
     * Make the column sortable.
     *
     * @access public
     *
     * @param array $columns Columns.
     * @return array The modified columns.
     */
    public function make_column_sortable( $columns ) {
        $columns['clicked'] = 'clicked';
        $columns['type']    = 'type';
        return $columns;
    }

    /**
     * Sort the column.
     *
     * @access public
     *
     * @param WP_Query $query Query.
     * @return void Sort the column.
     */
    public function sort_column( $query ) {
        if ( ! is_admin() || ! $query->is_main_query() ) {
            return;
        }

        if ( 'clicked' === $query->get( 'orderby' ) ) {
            $query->set( 'meta_key', 'fab_total_clicked' );
            $query->set( 'orderby', 'meta_value_num' );
        } elseif ( 'type' === $query->get( 'orderby' ) ) {
            $query->set( 'meta_key', 'fab_setting_type' );
            $query->set( 'orderby', 'meta_value' );
        }
    }

    /*
    |--------------------------------------------------------------------------
    | Fulfill implemented interface contracts
    |--------------------------------------------------------------------------
     */

    /**
     * JIT method.
     *
     * @return void
     */
    public static function jit() {
        // Handle fill value column.
        add_action( 'manage_fab_posts_custom_column', array( __CLASS__, 'jit_fill_column' ), 10, 2 );

        // Handle sort column.
        add_action( 'pre_get_posts', array( __CLASS__, 'jit_sort_column' ), 10, 1 );

        // Add column.
        add_filter( 'manage_fab_posts_columns', array( __CLASS__, 'jit_add_column' ), 10, 1 );

        // Handle sortable column.
        add_filter( 'manage_edit-fab_sortable_columns', array( __CLASS__, 'jit_make_column_sortable' ), 10, 1 );
    }
}
