<?php

namespace wpbel\classes\bootstrap;

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

use wpbel\classes\repositories\Meta_Field;
use wpbel\classes\repositories\Post;

class WPBEL_Meta_Fields
{
    private static $instance;

    public static function init()
    {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }
    }

    private function __construct()
    {
        add_filter('wpbe_post_column_fields', [$this, 'add_custom_fields']);
        add_filter('wpbe_page_column_fields', [$this, 'add_custom_fields']);
        add_filter('wpbe_custom_post_column_fields', [$this, 'add_custom_fields']);
        add_filter('wpbe_post_column_fields', [$this, 'add_taxonomies']);
        add_filter('wpbe_custom_post_column_fields', [$this, 'add_taxonomies']);
    }

    public function add_custom_fields($fields)
    {
        $meta_fields = (new Meta_Field())->get();
        if (!empty($meta_fields)) {
            foreach ($meta_fields as $meta_field) {
                $content_type = '';
                switch ($meta_field['main_type']) {
                    case "textinput":
                        if ($meta_field['sub_type'] == 'string') {
                            $content_type = 'text';
                        } else {
                            $content_type = 'numeric';
                        }
                        break;
                    case 'textarea':
                        $content_type = 'textarea';
                        break;
                    case 'checkbox':
                        $content_type = 'checkbox';
                        break;
                    case 'array':
                        $content_type = 'select';
                        break;
                    case 'calendar':
                        $content_type = 'date';
                        break;
                    default:
                        $content_type = sanitize_text_field($meta_field['main_type']);
                        break;
                }
                $fields[$meta_field['key']] = [
                    'name' => $meta_field['key'],
                    'label' => $meta_field['title'],
                    'field_type' => 'custom_field',
                    'editable' => true,
                    'content_type' => $content_type,
                    'update_type' => 'meta_field'
                ];

                if (!empty($meta_field['key_value'])) {
                    $fields[$meta_field['key']]['options'] = [];
                    $options = explode('|', $meta_field['key_value']);
                    if (!empty($options)) {
                        foreach ($options as $key => $value) {
                            $fields[$meta_field['key']]['options'][sanitize_text_field($key)] = sanitize_text_field($value);
                        }
                    }
                }
            }
        }
        return $fields;
    }

    public function add_taxonomies($fields)
    {
        $taxonomies = (new Post())->get_taxonomies();
        if (!empty($taxonomies)) {
            foreach ($taxonomies as $key => $taxonomy) {
                $fields[$key] = [
                    'label' => $taxonomy['label'],
                    'editable' => true,
                    'content_type' => 'multi_select',
                ];
                $fields[$key]['field_type'] = 'taxonomy';
            }
        }
        return $fields;
    }
}
