<?php
namespace Gridpress_Options;

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

if (! class_exists('Gridpress_Field_Functions')) {

    class Gridpress_Field_Functions {

        private function common_field_args($gridpress_args) {
            $gridpress_field_args = [];
            if (isset($gridpress_args['label_for'])) {
                $gridpress_field_args['id'] = $gridpress_args['label_for'];
            }
            //
            if (isset($gridpress_args['classes'])) {
                $gridpress_field_args['classes'] = $gridpress_args['classes'];
            }
            if (isset($gridpress_args['default'])) {
                $gridpress_field_args['default'] = $gridpress_args['default'];
            }
            if (isset($gridpress_args['extra_attributes'])) {
                $gridpress_field_args['extra_attributes'] = $gridpress_args['extra_attributes'];
            }

            return $gridpress_field_args;
        }

        private function field_info_str($gridpress_args) {
            if (isset($gridpress_args['info']) && !empty($gridpress_args['info'])) {
                echo '<div class="gridpress-setting-field-info">' . wp_kses_post($gridpress_args['info']) . '</div>';
            }
        }

        public function input_field_callback($gridpress_args) {
            $gridpress_options_data = $gridpress_args['options_data'];
            $gridpress_options_name = $gridpress_args['options_name'];
            $gridpress_field_name = $gridpress_args['field_name'];
            $gridpress_value = isset($gridpress_options_data[$gridpress_field_name]) ? $gridpress_options_data[$gridpress_field_name] : '';

            $gridpress_field_args = $this->common_field_args($gridpress_args);

            $gridpress_field_args['name'] = $gridpress_options_name . '[' . $gridpress_field_name . ']';
            if ($gridpress_value != '') {
                $gridpress_field_args['value'] = $gridpress_value;
            }
            if (isset($gridpress_args['input_type'])) {
                $gridpress_field_args['input_type'] = $gridpress_args['input_type'];
            }
            if (isset($gridpress_args['validation_type'])) {
                $gridpress_field_args['validation_type'] = $gridpress_args['validation_type'];
            }
            \Gridpress_Form_Fields::gridpress_input($gridpress_field_args);

            $this->field_info_str($gridpress_args);
        }

        public function textarea_field_callback($gridpress_args) {
            $gridpress_options_data = $gridpress_args['options_data'];
            $gridpress_options_name = $gridpress_args['options_name'];
            $gridpress_field_name = $gridpress_args['field_name'];
            $gridpress_value = isset($gridpress_options_data[$gridpress_field_name]) ? $gridpress_options_data[$gridpress_field_name] : '';

            $gridpress_field_args = $this->common_field_args($gridpress_args);

            $gridpress_field_args['name'] = $gridpress_options_name . '[' . $gridpress_field_name . ']';
            if ($gridpress_value != '') {
                $gridpress_field_args['value'] = $gridpress_value;
            }
            \Gridpress_Form_Fields::gridpress_textarea($gridpress_field_args);

            $this->field_info_str($gridpress_args);
        }

        public function select_field_callback($gridpress_args) {
            $gridpress_options_data = $gridpress_args['options_data'];
            $gridpress_options_name = $gridpress_args['options_name'];
            $gridpress_field_name = $gridpress_args['field_name'];
            $gridpress_value = isset($gridpress_options_data[$gridpress_field_name]) ? $gridpress_options_data[$gridpress_field_name] : '';

            $gridpress_field_args = $this->common_field_args($gridpress_args);

            $gridpress_field_args['name'] = $gridpress_options_name . '[' . $gridpress_field_name . ']' . (isset($gridpress_args['multiple']) && $gridpress_args['multiple'] ? '[]' : '');
            if ($gridpress_value != '') {
                $gridpress_field_args['value'] = $gridpress_value;
            }
            if (isset($gridpress_args['options'])) {
                $gridpress_field_args['options'] = $gridpress_args['options'];
            }
            if (isset($gridpress_args['multiple']) && $gridpress_args['multiple']) {
                $gridpress_field_args['multiple'] = true;
            }
            \Gridpress_Form_Fields::gridpress_select($gridpress_field_args);

            $this->field_info_str($gridpress_args);
        }

        public function switch_field_callback($gridpress_args) {
            $gridpress_options_data = $gridpress_args['options_data'];
            $gridpress_options_name = $gridpress_args['options_name'];
            $gridpress_field_name = $gridpress_args['field_name'];
            $gridpress_value = isset($gridpress_options_data[$gridpress_field_name]) ? $gridpress_options_data[$gridpress_field_name] : '';

            $gridpress_field_args = $this->common_field_args($gridpress_args);

            $gridpress_field_args['name'] = $gridpress_options_name . '[' . $gridpress_field_name . ']';
            if ($gridpress_value != '') {
                $gridpress_field_args['value'] = $gridpress_value;
            }
            \Gridpress_Form_Fields::gridpress_switch_button($gridpress_field_args);

            $this->field_info_str($gridpress_args);
        }

        public function option_buttons_field_callback($gridpress_args) {
            $gridpress_options_data = $gridpress_args['options_data'];
            $gridpress_options_name = $gridpress_args['options_name'];
            $gridpress_field_name = $gridpress_args['field_name'];
            $gridpress_value = isset($gridpress_options_data[$gridpress_field_name]) ? $gridpress_options_data[$gridpress_field_name] : '';

            $gridpress_field_args = $this->common_field_args($gridpress_args);

            $gridpress_field_args['name'] = $gridpress_options_name . '[' . $gridpress_field_name . ']';
            if ($gridpress_value != '') {
                $gridpress_field_args['value'] = $gridpress_value;
            }
            if (isset($gridpress_args['options'])) {
                $gridpress_field_args['options'] = $gridpress_args['options'];
            }
            if (isset($gridpress_args['multiple']) && $gridpress_args['multiple']) {
                $gridpress_field_args['multiple'] = true;
            }
            \Gridpress_Form_Fields::gridpress_option_buttons($gridpress_field_args);

            $this->field_info_str($gridpress_args);
        }

        public function color_field_callback($gridpress_args) {
            $gridpress_options_data = $gridpress_args['options_data'];
            $gridpress_options_name = $gridpress_args['options_name'];
            $gridpress_field_name = $gridpress_args['field_name'];
            $gridpress_value = isset($gridpress_options_data[$gridpress_field_name]) ? $gridpress_options_data[$gridpress_field_name] : '';

            $gridpress_field_args = $this->common_field_args($gridpress_args);

            $gridpress_field_args['name'] = $gridpress_options_name . '[' . $gridpress_field_name . ']';
            if ($gridpress_value != '') {
                $gridpress_field_args['value'] = $gridpress_value;
            }
            \Gridpress_Form_Fields::gridpress_color_pickr($gridpress_field_args);

            $this->field_info_str($gridpress_args);
        }

        public function image_field_callback($gridpress_args) {
            $gridpress_options_data = $gridpress_args['options_data'];
            $gridpress_options_name = $gridpress_args['options_name'];
            $gridpress_field_name = $gridpress_args['field_name'];
            $gridpress_value = isset($gridpress_options_data[$gridpress_field_name]) ? $gridpress_options_data[$gridpress_field_name] : '';

            $gridpress_field_args = $this->common_field_args($gridpress_args);

            $gridpress_field_args['name'] = $gridpress_options_name . '[' . $gridpress_field_name . ']';
            if ($gridpress_value != '') {
                $gridpress_field_args['value'] = $gridpress_value;
            }
            \Gridpress_Form_Fields::gridpress_image($gridpress_field_args);

            $this->field_info_str($gridpress_args);
        }

    }
}