<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
use Gridpress_Options\Gridpress_Field_Functions;

if (! class_exists('Gridpress_Options_General_Settings')) {

    class Gridpress_Options_General_Settings {

        private $gridpress_general_options_name = 'gridpress_general_settings';

        private $gridpress_general_options_data = [];

        public function __construct() {
            add_action( 'admin_menu', [ $this, 'gridpress_load_settings' ], 30 );
            add_action( 'admin_init', [ $this, 'gridpress_register_settings' ] );
            $this->gridpress_general_options_data = get_option( $this->gridpress_general_options_name );
        }

        public function gridpress_register_settings() {

            register_setting(
                'gridpress_general_options_group',
                'gridpress_general_settings',
                array(
                    'type'              => 'array',
                    'sanitize_callback' => array($this, 'gridpress_sanitize_general_settings'),
                    'default'           => [],
                    'show_in_rest'      => false,
                )
            );
        }

        public function gridpress_sanitize_general_settings($gridpress_input) {

            if (!is_array($gridpress_input)) {
                return [];
            }

            $gridpress_sanitized = [];

            // Post types (array)
            $gridpress_sanitized['grid_post_types'] = [];
            if (isset($gridpress_input['grid_post_types']) && is_array($gridpress_input['grid_post_types'])) {
                foreach ($gridpress_input['grid_post_types'] as $gridpress_type) {
                    $gridpress_sanitized['grid_post_types'][] = sanitize_key(wp_unslash($gridpress_type));
                }
            }

            // Single values
            $gridpress_sanitized['grid_default_style'] = isset($gridpress_input['grid_default_style'])
                ? sanitize_key(wp_unslash($gridpress_input['grid_default_style']))
                : '';

            $gridpress_sanitized['grid_default_pagination_style'] = isset($gridpress_input['grid_default_pagination_style'])
                ? sanitize_key(wp_unslash($gridpress_input['grid_default_pagination_style']))
                : '';

            $gridpress_sanitized['grid_default_orderby'] = isset($gridpress_input['grid_default_orderby'])
                ? sanitize_key(wp_unslash($gridpress_input['grid_default_orderby']))
                : '';

            $gridpress_sanitized['grid_default_order'] = isset($gridpress_input['grid_default_order'])
                ? sanitize_key(wp_unslash($gridpress_input['grid_default_order']))
                : '';

            $gridpress_sanitized['grid_default_numposts'] = isset($gridpress_input['grid_default_numposts'])
                ? absint(wp_unslash($gridpress_input['grid_default_numposts']))
                : 0;

            return $gridpress_sanitized;
        }

        public function gridpress_load_settings() {

            wp_enqueue_media();
            wp_enqueue_script('wp-color-picker');

            $gridpress_options_key_name = $this->gridpress_general_options_name;
            $gridpress_options_data = $this->gridpress_general_options_data;

            $gridpress_field_functions = new Gridpress_Field_Functions;

            add_settings_section(
                'gridpress_admin_settings_section_general', // ID
                '', // Title
                [$this, 'gridpress_settings_section_callback'], // Callback
                'gridpress_general_options_page', // Page
                array(
                    'before_section' => '<div class="gridpress-settingssec-title">',
                    'after_section' => '</div>',
                    'section_title' => ''
                )
            );

            $gridpress_select_options_list = gridpress_cpt_list();
            add_settings_field(
                'grid-post-types', // ID
                esc_html__('Select post types', 'gridpress-grid-layout-design'), // Title
                [$gridpress_field_functions, 'select_field_callback'], // Callback
                'gridpress_general_options_page', // Page
                'gridpress_admin_settings_section_general', // Section
                array(
                    'options_name' => $gridpress_options_key_name,
                    'options_data' => $gridpress_options_data,
                    'label_for' => 'grid-post-types',
                    'field_name' => 'grid_post_types',
                    'multiple' => true,
                    'classes' => ['gridpress-select2-dropdown'],
                    'extra_attributes' => ['placeholder' => esc_html__('Select post types', 'gridpress-grid-layout-design')],
                    'info' => esc_html__('Selected post types can be used in grid post item settings.', 'gridpress-grid-layout-design'),
                    'options' => $gridpress_select_options_list
                )
            );

            $gridpress_styles_list = [];
            for ($gridpress_st = 1; $gridpress_st <= 30; $gridpress_st++) {
                /* translators: %d: grid style number. */
                $gridpress_styles_list['style' . $gridpress_st] = sprintf(esc_html__('Style %d', 'gridpress-grid-layout-design'), $gridpress_st);
            }
            add_settings_field(
                'grid-default-style', // ID
                esc_html__('Default Style for Grid', 'gridpress-grid-layout-design'), // Title
                [$gridpress_field_functions, 'select_field_callback'], // Callback
                'gridpress_general_options_page', // Page
                'gridpress_admin_settings_section_general', // Section
                array(
                    'options_name' => $gridpress_options_key_name,
                    'options_data' => $gridpress_options_data,
                    'label_for' => 'grid-default-style',
                    'field_name' => 'grid_default_style',
                    'classes' => ['gridpress-select2-dropdown'],
                    'options' => $gridpress_styles_list
                )
            );

            add_settings_field(
                'grid-default-pagination-style', // ID
                esc_html__('Default Pagination Style', 'gridpress-grid-layout-design'), // Title
                [$gridpress_field_functions, 'select_field_callback'], // Callback
                'gridpress_general_options_page', // Page
                'gridpress_admin_settings_section_general', // Section
                array(
                    'options_name' => $gridpress_options_key_name,
                    'options_data' => $gridpress_options_data,
                    'label_for' => 'grid-default-pagination-style',
                    'field_name' => 'grid_default_pagination_style',
                    'classes' => ['gridpress-select2-dropdown'],
                    'info' => esc_html__('Set default pagination style for post items', 'gridpress-grid-layout-design'),
                    'options' => [
                        'page_nums' => esc_html__('Page Numbers', 'gridpress-grid-layout-design'),
                        'load_more' => esc_html__('Load More', 'gridpress-grid-layout-design'),
                        'infinite' => esc_html__('Infinite Scroll', 'gridpress-grid-layout-design'),
                    ]
                )
            );

            add_settings_field(
                'grid-default-orderby', // ID
                esc_html__('Default Order by', 'gridpress-grid-layout-design'), // Title
                [$gridpress_field_functions, 'select_field_callback'], // Callback
                'gridpress_general_options_page', // Page
                'gridpress_admin_settings_section_general', // Section
                array(
                    'options_name' => $gridpress_options_key_name,
                    'options_data' => $gridpress_options_data,
                    'label_for' => 'grid-default-orderby',
                    'field_name' => 'grid_default_orderby',
                    'classes' => ['gridpress-select2-dropdown'],
                    'info' => esc_html__('Set default order by field for post items', 'gridpress-grid-layout-design'),
                    'options' => [
                        'id' => esc_html__('ID', 'gridpress-grid-layout-design'),
                        'title' => esc_html__('Title', 'gridpress-grid-layout-design'),
                    ]
                )
            );

            add_settings_field(
                'grid-default-order', // ID
                esc_html__('Default Order', 'gridpress-grid-layout-design'), // Title
                [$gridpress_field_functions, 'select_field_callback'], // Callback
                'gridpress_general_options_page', // Page
                'gridpress_admin_settings_section_general', // Section
                array(
                    'options_name' => $gridpress_options_key_name,
                    'options_data' => $gridpress_options_data,
                    'label_for' => 'grid-default-order',
                    'field_name' => 'grid_default_order',
                    'classes' => ['gridpress-select2-dropdown'],
                    'info' => esc_html__('Set default order type for post items', 'gridpress-grid-layout-design'),
                    'options' => [
                        'DESC' => esc_html__('Descending', 'gridpress-grid-layout-design'),
                        'ASC' => esc_html__('Ascending', 'gridpress-grid-layout-design'),
                    ]
                )
            );

            add_settings_field(
                'grid-default-numposts', // ID
                esc_html__('Number of posts', 'gridpress-grid-layout-design'), // Title
                [$gridpress_field_functions, 'input_field_callback'], // Callback
                'gridpress_general_options_page', // Page
                'gridpress_admin_settings_section_general', // Section
                array(
                    'options_name' => $gridpress_options_key_name,
                    'options_data' => $gridpress_options_data,
                    'label_for' => 'grid-default-numposts',
                    'field_name' => 'grid_default_numposts',
                    'input_type' => 'number',
                    'extra_attributes' => ['min' => 1, 'max' => 100],
                    'info' => esc_html__('Set default number of posts per page', 'gridpress-grid-layout-design'),
                )
            );
        }

        public function gridpress_settings_section_callback($gridpress_args) {
            ?>
            <h3><?php esc_html_e('General Settings', 'gridpress-grid-layout-design') ?></h3>
            <?php
        }

    }
    new Gridpress_Options_General_Settings();
}