<?php

namespace PWH_DCFH\App\Admin\Controllers;

use WP_List_Table;

if (!class_exists('PWH_DCFH_Contact_Forms_List')) {
    class PWH_DCFH_Contact_Forms_List extends WP_List_Table
    {

        public $_page;

        private $_post_type;

        /**
         * Initial Class Constructor
         *
         * @param array $args
         */
        public function __construct($args = [])
        {
            parent::__construct([
                'singular' => __('Contact Form', 'pwh-dcfh'),
                'plural' => __('Contact Forms', 'pwh-dcfh'),
                'ajax' => false
            ]);
            $this->_page = isset($_GET['page']) ? sanitize_key($_GET['page']) : null; // phpcs:ignore
            $this->_post_type = 'pwh_dcfh';
        }

        /**
         * Prepare Table Items
         */
        public function prepare_items()
        {
            ## Columns
            $this->_column_headers = $this->get_column_info();
            ## Process bulk action
            $this->process_bulk_action();
            ## Get Record
            $record = $this->get_record();
            $data = $record['items'] ?? [];
            usort($data, [&$this, 'sort_data']);
            $this->items = $data;
        }

        /**
         * Get Records From Database
         * @return array
         */
        public function get_record()
        {
            $record = [];
            $forms = self::get_contact_forms();
            if (!empty($forms)) {
                $total_items = 0;
                foreach ($forms as $form_id => $form) {
                    ++$total_items;
                    $record['items'][] = [
                        'ID' => $form_id,
                        'title' => $form['title'],
                        'entries' => $form['entries'],
                        'entries_threshold' => $form['entries_threshold'],
                        'views' => $form['views'],
                        'unique_views' => $form['unique_views'],
                        'conversion' => $form['conversion'],
                        'unique_conversion' => $form['unique_conversion'],
                        'last_entry_date' => $form['last_entry_date'],
                        'last_post_id' => $form['last_post_id'],
                        'page_id' => $form['page_id'],
                    ];
                }
                $record['total_items'] = $total_items;
            }

            return $record;
        }

        /**
         * @return array[]
         */
        public function get_sortable_columns()
        {
            return [
                "title" => ["title", true],
                "entries" => ["entries", true],
                "views" => ["views", true],
                "unique_views" => ["unique_views", true],
                "conversion" => ["conversion", true],
                "unique_conversion" => ["unique_conversion", true],
                "last_entry_date" => ["last_entry_date", true],
            ];
        }

        /** Get Columns
         * @return array
         */
        public function get_columns()
        {
            return [
                'cb' => '<input type="checkbox" />',
                'title' => __('ID/Title', 'pwh-dcfh'),
                'entries' => __('Entries', 'pwh-dcfh'),
                'views' => __('Views', 'pwh-dcfh'),
                'unique_views' => __('Unique Views', 'pwh-dcfh'),
                'conversion' => __('Conversion', 'pwh-dcfh'),
                'unique_conversion' => __('Unique Conversion', 'pwh-dcfh'),
                'last_entry_date' => __('Last Entry', 'pwh-dcfh'),
                'entries_threshold' => __('Threshold', 'pwh-dcfh'),
                'actions_html' => __('Actions', 'pwh-dcfh'),
            ];
        }

        /**
         * Set Default Columns
         *
         * @param array|object $item
         * @param string $column_name
         *
         * @return mixed|string
         */
        public function column_default($item, $column_name)
        {
            switch ($column_name) {
                case 'title':
                case 'entries':
                case 'views':
                case 'unique_views':
                case 'last_entry_date':
                    return $item[$column_name];
                case 'conversion':
                case 'unique_conversion':
                    return sprintf('%s%%', $item[$column_name]);
                case 'actions_html':
                    return $this->action_links($item);
                case 'entries_threshold':
                    return '-1' === $item[$column_name] ? __('Unlimited', 'pwh-dcfh') : $item[$column_name];
                default:
                    return null;
            }
        }

        /**
         * Checkbox Column
         *
         * @param $item
         *
         * @return string
         */
        public function column_cb($item)
        {
            return sprintf('<input type="checkbox" name="bulk-action[]" value="%s">', $item['ID']);
        }

        /**
         * No Item Found
         * @return void
         */
        public function no_items()
        {
            esc_attr_e('Oops! No Record Found In Database.', 'pwh-dcfh');
        }

        /**
         * Register Bulk Actions
         * @return array
         */
        public function get_bulk_actions()
        {
            if (!current_user_can('delete_pwh_dcfhs')) {
                return [];
            }

            return [
                'bulk-delete-entries' => __('Delete Entries', 'pwh-dcfh'),
                'bulk-reset-views' => __('Reset Views', 'pwh-dcfh'),
                'bulk-delete-record' => __('Delete Record', 'pwh-dcfh')
            ];
        }

        /**
         * Process Bulk Actions
         * @return void
         */
        public function process_bulk_action()
        {
            if (!empty($_GET['_wpnonce'])) {
                $_wpnonce = sanitize_text_field($_GET['_wpnonce']);
                if (!wp_verify_nonce($_wpnonce, 'bulk-action-'.$this->_page)) {
                    wp_die(esc_html__('Security checks failed. Please try again later.', 'pwh-dcfh'));
                }
                $action = $this->current_action();
                if (!$action) {
                    return;
                }
                $bulk_actions = isset($_GET['bulk-action']) ? array_map('sanitize_text_field', $_GET['bulk-action']) : [];
                if (!empty($bulk_actions)) {
                    // Delete Entries
                    if ('bulk-delete-entries' === $action) {
                        global $wpdb;
                        $rows = [];
                        foreach ($bulk_actions as $bulk_action) {
                            $rows[$bulk_action] = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_value LIKE %s ORDER BY meta_id DESC", $bulk_action)); //db call ok; no-cache ok
                        }
                        if (!empty($rows)) {
                            foreach ($rows as $row) {
                                foreach ($row as $_post) {
                                    wp_delete_post($_post->post_id, true);
                                }
                            }
                        }
                    }
                    // Reset Views
                    if ('bulk-reset-views' === $action) {
                        foreach ($bulk_actions as $bulk_action) {
                            pwh_dcfh_db()::reset_contact_form_views($bulk_action);
                        }
                    }
                    // Delete Options
                    if ('bulk-delete-record' === $action) {
                        foreach ($bulk_actions as $bulk_action) {
                            pwh_dcfh_db()::delete_contact_form_options($bulk_action);
                        }
                    }
                }
            }
        }

        /**
         * Get Contact Form Data
         * @return array
         */
        private static function get_contact_forms()
        {
            $data = [];
            $contact_forms = pwh_dcfh_db()::get_contact_forms_list();
            if (empty($contact_forms)) {
                return $data;
            }
            foreach ($contact_forms as $contact_form) {
                $contact_form_id = $contact_form['unique_id'];
                $contact_form_title = $contact_form['title'];
                $contact_form_views = $contact_form['views'];
                $contact_form_unique_views = $contact_form['unique_views'];
                $page_id = $contact_form['page_id'];
                $contact_form_entries_threshold = $contact_form['entries_threshold'];
                $contact_form_entries = $contact_form['total_entries'];
                $last_post_id = pwh_dcfh_db()::get_contact_form_last_post_id($contact_form_id);
                $contact_form_conversion = ($contact_form_views !== 0) ? round(($contact_form_entries / $contact_form_views) * 100, 2) : 0;
                $contact_form_unique_conversion = ($contact_form_unique_views !== 0) ? round(($contact_form_entries / $contact_form_unique_views) * 100, 2) : 0;
                $contact_form_last_entry_date = !empty($last_post_id) ? pwh_dcfh_wp_helpers()::datetime(get_post_field('post_date', $last_post_id)) : '-';
                $data[$contact_form_id] = [
                    'ID' => $contact_form_id,
                    'title' => $contact_form_title,
                    'views' => $contact_form_views,
                    'unique_views' => $contact_form_unique_views,
                    'entries_threshold' => $contact_form_entries_threshold,
                    'entries' => $contact_form_entries,
                    'conversion' => $contact_form_conversion,
                    'unique_conversion' => $contact_form_unique_conversion,
                    'last_entry_date' => $contact_form_last_entry_date,
                    'last_post_id' => $last_post_id,
                    'page_id' => $page_id,
                ];
            }

            return $data;

        }

        /**
         * Table Action Links
         *
         * @param $item
         *
         * @return string
         */
        private function action_links($item)
        {
            $contact_form_id = $item['ID'];
            //$last_post_id = $item['last_post_id'];
            $page_id = $item['page_id'];
            $view_entries = add_query_arg(['post_type' => $this->_post_type, 'contact_form_id' => $contact_form_id], admin_url('edit.php'));
            $export_csv = add_query_arg(['post_type' => $this->_post_type, 'page' => 'export_as_csv', 'contact_form_id' => $contact_form_id], admin_url('edit.php'));
            $view_form = '';
            if ($page_id && get_post($page_id)) {
                $view_form = get_the_permalink($page_id);
            }
            $output = '<div class="action-links">';
            $output .= sprintf(' <a href="%s">%s</a>', esc_url($view_entries), __('View Entries', 'pwh-dcfh'));
            if (current_user_can('export_csv_pwh_dcfh')) {
                $output .= sprintf(' <a href="%s">%s</a>', esc_url($export_csv), __('Export Entries', 'pwh-dcfh'));
            }
            if ($view_form) {
                $output .= sprintf(' <a href="%s">%s</a>', esc_url($view_form), __('View Form', 'pwh-dcfh'));
            }
            $output .= '</div>';

            return $output;

        }

        /**
         * Sort Table Data
         *
         * @param $a
         * @param $b
         *
         * @return float|int
         */
        private function sort_data($a, $b)
        {
            // Set defaults
            $orderby = 'title';
            $order = 'asc';
            // phpcs:disable
            if (!empty($_GET['orderby'])) {
                $orderby = sanitize_text_field($_GET['orderby']);
            }
            if (!empty($_GET['order'])) {
                $order = sanitize_text_field($_GET['order']);
            }
            // phpcs:enable
            $result = strcmp($a[$orderby], $b[$orderby]);
            if ($order === 'asc') {
                return $result;
            }

            return -$result;
        }
    }
}