<?php

/**
 * @package cwpt
 * @since 1.0.0
 */

defined('ABSPATH') or die;

class CWPT_Settings
{
    /**
     * Get CWPT Settings
     * 
     * @param string $settings
     * @return mixed
     */
    public static function get($settings)
    {
        $product_types_ids = get_posts(array(
            'fields'         => 'ids',
            'posts_per_page' => -1,
            'post_type'      => 'custom-product-type',
        ));

        if (!empty($product_types_ids)) {
            foreach ($product_types_ids as $type_id) {

                $product_type_single = sanitize_text_field(get_post_meta($type_id, 'cwpt_product_type_name_singular', true));
                $product_type_id = preg_replace('/\\s+/', '_', trim(preg_replace('/[^\\da-z ]/i', '', strtolower($product_type_single))));
                $product_type_class = preg_replace('/\\s+/', '-', trim(preg_replace('/[^\\da-z ]/i', '', strtolower($product_type_single))));
                $product_type_plural = sanitize_text_field(get_post_meta($type_id, 'cwpt_product_type_name_plural', true));
                $product_type_tax_status = get_post_meta($type_id, 'cwpt_custom_taxonomy', true);
                $product_type_badge = get_post_meta($type_id, 'cwpt_enable_badge', true);
                $product_type_custom_tab = get_post_meta($type_id, 'cwpt_enable_admin_fields', true);
                $product_type_custom_tab_fields = get_post_meta($type_id, 'cwpt_admin_tab_fields', true);
                $cwpt_layout = get_post_meta($type_id, 'cwpt_product_layout', true);

                    $product_type_addons = get_post_meta($type_id, 'cwpt_enable_front_fields', true);
                    $product_type_addons_fields = get_post_meta($type_id, 'cwpt_front_tab_fields', true);

                if ($product_type_badge == 'on') {
                    $product_badge_text = get_post_meta($type_id, 'cwpt_badge_text', true);
                    $product_badge_background = get_post_meta($type_id, 'cwpt_badge_background_color', true);
                    $product_badge_text_color = get_post_meta($type_id, 'cwpt_badge_text_color', true);
                    $cwpt_badges[$product_type_id] = [
                        'text'       => $product_badge_text,
                        'background' => $product_badge_background,
                        'color'      => $product_badge_text_color,
                    ];
                }

                if ($cwpt_layout) {

                    $cwpt_product_layouts[$product_type_id] = $cwpt_layout;
                }

                //Admin tab front data
                if ($product_type_custom_tab == 'on') {

                    $product_tab_label_text = get_post_meta($type_id, 'cwpt_admin_fields_tab_label', true);
                    $product_tab_position = get_post_meta($type_id, 'cwpt_front_position', true);

                    $cwpt_tab[$product_type_id] = [
                        'position' => $product_tab_position,
                        'text'   => $product_tab_label_text,
                        'fields' => $product_type_custom_tab_fields,
                    ];
                }

                    //Product Addons
                    if ($product_type_addons == 'on') {

                        $product_addons_title_text = get_post_meta($type_id, 'cwpt_front_fields_title', true);

                        $cwpt_addons[$product_type_id] = [
                            'enabled' => $product_type_addons == 'on' ? true : false,
                            'text'   => $product_addons_title_text,
                            'fields' => $product_type_addons_fields,
                        ];
                    }

                $types_names_single[] = $product_type_single;
                $types_names_plural[] = $product_type_plural;
                $types_tax_status[] = $product_type_tax_status;
                $types_ids[] = $product_type_id;
                $types_classes[] = $product_type_class;
            }
            switch ($settings) {
                case 'single':
                    return $types_names_single ?? [];
                case 'IDS':
                    return $product_types_ids ?? [];
                case 'name_ids':
                    return $types_ids ?? [];
                case 'class':
                    return $types_classes ?? [];
                case 'plural':
                    return $types_names_plural ?? [];
                case 'status':
                    return $types_tax_status ?? [];
                case 'badges':
                    return $cwpt_badges ?? null;
                case 'custom_tab':
                    return $cwpt_tab ?? null;
                case 'product_layout':
                    return $cwpt_product_layouts ?? null;
                case 'front_addons':
                    return $cwpt_addons ?? null;
            }
        }
        return null;
    }
}
