<?php
namespace NCPC\Api\Admin\Additionals_Options;

use WP_Error;
use WP_Post;
use WP_Query;
use WP_REST_Controller;

/**
 * class for api routes reaching custom additionals
 */
class NCPC_Api_Customs_Additionals extends WP_REST_Controller
{
    /**
     * [__construct description]
     */
    public function __construct()
    {
        $this->namespace = 'ncpc/v2';
        $this->rest_base = 'configs/(?P<config_id>\d+)/custom-additionals';
    }
    /**
     * Register the routes
     *
     * @return void
     */
    public function register_routes()
    {
        register_rest_route(
            $this->namespace,
            '/' . $this->rest_base,
            array(
                array(
                    'methods' => \WP_REST_Server::READABLE,
                    'callback' => array($this, 'get_all_custom_additionals'),
                    'permission_callback' => array($this, 'get_config_permissions_check'),
                    'args' => array(
                        'config_id' => array(
                            'type' => 'integer',
                            'required' => true,
                        )
                    ),
                ),
                array(
                    'methods' => \WP_REST_Server::CREATABLE,
                    'callback' => array($this, 'add_custom_additional_to_custom_additionals_setting'),
                    'permission_callback' => array($this, 'get_config_permissions_check'),
                    'args' => array(
                        'config_id' => array(
                            'type' => 'integer',
                            'required' => true,
                        )
                    ),
                )
            )
        );
        register_rest_route(
            $this->namespace,
            '/' . $this->rest_base . "/update",
            array(
                array(
                    'methods' => \WP_REST_Server::EDITABLE,
                    'callback' => array($this, 'update_custom_additionals'),
                    'permission_callback' => array($this, 'get_config_permissions_check'),
                    'args' => array(
                        'config_id' => array(
                            'type' => 'integer',
                            'required' => true,
                        )
                    ),
                ),
            )
        );
        register_rest_route(
            $this->namespace,
            '/' . $this->rest_base . "/(?P<custom_additional_id>\d+)",
            array(
                array(
                    'methods' => \WP_REST_Server::READABLE,
                    'callback' => array($this, 'get_custom_additional_info'),
                    'permission_callback' => array($this, 'get_config_permissions_check'),
                    'args' => array(
                        'config_id' => array(
                            'type' => 'integer',
                            'required' => true,
                        ),
                        'custom_additional_id' => array(
                            'type' => 'integer',
                            'required' => true
                        )
                    ),
                ),
                array(
                    'methods' => \WP_REST_Server::EDITABLE,
                    'callback' => array($this, 'update_custom_additional_in_custom_additionals_setting'),
                    'permission_callback' => array($this, 'get_config_permissions_check'),
                    'args' => array(
                        'config_id' => array(
                            'type' => 'integer',
                            'required' => true,
                        ),
                        'custom_additional_id' => array(
                            'type' => 'integer',
                            'required' => true
                        )
                    ),
                ),
                array(
                    'methods' => \WP_REST_Server::DELETABLE,
                    'callback' => array($this, 'delete_custom_additional_in_custom_additionals_setting'),
                    'permission_callback' => array($this, 'get_config_permissions_check'),
                    'args' => array(
                        'config_id' => array(
                            'type' => 'integer',
                            'required' => true,
                        ),
                        'custom_additional_id' => array(
                            'type' => 'integer',
                            'required' => true
                        )
                    ),
                )
            )
        );

    }

    /**
     * Get all custom additionals of ncpc configuration ID
     * @param \WP_REST_Request $request Full details about the request.
     * @return \WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
     */

    public function get_all_custom_additionals($request)
    {
        $id = $request->get_param('config_id');
        if ($id != 0) {
            $meta_value = get_post_meta($id, 'ncpc-configs-meta', true);

            if (!empty($meta_value)) {

                if (isset($meta_value["data"]["additionalOptions"]["customAdditionalsOptions"])) {
                    $custom_additionals_options = $meta_value["data"]["additionalOptions"]["customAdditionalsOptions"];
                    return rest_ensure_response($custom_additionals_options);
                }
                return rest_ensure_response(["message" => "No data found"]);
            } else {
                return rest_ensure_response(["message" => "No data found"]);
            }
        } else {
            return rest_ensure_response(["message" => "Custom ID invalid"]);
        }

    }

    /**
     * Get a custom additional with ID of ncpc configuration ID
     * @param \WP_REST_Request $request Full details about the request.
     * @return \WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
     */

    public function get_custom_additional_info($request)
    {
        $id = $request->get_param('config_id');
        $custom_additional_id = $request->get_param('custom_additional_id');
        if ($id != 0) {
            $meta_value = get_post_meta($id, 'ncpc-configs-meta', true);

            if (!empty($meta_value)) {

                if (isset($meta_value["data"]["additionalOptions"]["customAdditionalsOptions"])) {
                    $custom_additionals = $meta_value["data"]["additionalOptions"]["customAdditionalsOptions"];
                    if (isset($custom_additionals[$custom_additional_id])) {
                        return rest_ensure_response($custom_additionals[$custom_additional_id]);
                    }
                    return rest_ensure_response(array("message" => "No custom found"));
                } else {
                    return rest_ensure_response(array("message" => "No custom found"));
                }


            } else {
                return rest_ensure_response(["message" => "No data found"]);
            }
        } else {
            return rest_ensure_response(["message" => "Custom ID invalid"]);
        }

    }

    /**
     * Add custom in custom configuration
     * @param \WP_REST_Request $request Full details about the request.
     * @return \WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
     */
    public function add_custom_additional_to_custom_additionals_setting($request)
    {

        $id = $request->get_param('config_id');
        $custom_additional = json_decode($request->get_body(), true);

        if ($id != 0) {
            $post = get_post($id);
            if ($post) {
                $meta_value = get_post_meta($id, 'ncpc-configs-meta', true);
                if (empty($meta_value)) {
                    $meta_value["data"]["additionalOptions"]["customAdditionalsOptions"][0] = $custom_additional;
                } else {
                    if (isset($meta_value["data"]["additionalOptions"]["customAdditionalsOptions"])) {
                        $custom_additionals = $meta_value["data"]["additionalOptions"]["customAdditionalsOptions"];

                        array_push($custom_additionals, $custom_additional);

                        $meta_value["data"]["additionalOptions"]["customAdditionalsOptions"] = $custom_additionals;
                    } else {
                        $meta_value["data"]["additionalOptions"]["customAdditionalsOptions"][0] = $custom_additional;
                    }
                }

                $response = update_post_meta($id, 'ncpc-configs-meta', $meta_value);

                if ($response) {
                    return rest_ensure_response(["success" => true]);
                } else {
                    return rest_ensure_response(["message" => "add custom failed"]);
                }
            } else {
                return rest_ensure_response(["message" => "Custom ID invalid"]);
            }
        } else {
            return rest_ensure_response(["message" => "Custom ID invalid"]);
        }
    }
    public function update_custom_additionals($request)
    {

        $id = $request->get_param('config_id');
        $custom_additionals = json_decode($request->get_body(), true);

        if ($id != 0) {
            $post = get_post($id);
            if ($post) {
                $meta_value = get_post_meta($id, 'ncpc-configs-meta', true);
                if ($meta_value["data"]["additionalOptions"]["customAdditionalsOptions"] == $custom_additionals) {
                    return rest_ensure_response(["success" => "same", "message" => __("NO change observe on additionnals options", "neon-channel-product-customizer-free")]);
                } else {
                    $meta_value["data"]["additionalOptions"]["customAdditionalsOptions"] = $custom_additionals;

                    $response = update_post_meta($id, 'ncpc-configs-meta', $meta_value);

                    if ($response) {
                        return rest_ensure_response(["success" => true, "message" => __("Additonnals Option successfully sorted")]);
                    } else {
                        return rest_ensure_response(["message" => __("Additonnals Option sort failed", "neon-channel-product-customizer-free")]);
                    }
                }
            } else {
                return rest_ensure_response(["message" => "Custom ID invalid"]);
            }
        } else {
            return rest_ensure_response(["message" => "Custom ID invalid"]);
        }
    }

    /**
     * Update a custom in the custom configuration
     * @param \WP_REST_Request $request Full details about the request.
     * @return \WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
     */
    public function update_custom_additional_in_custom_additionals_setting($request)
    {
        $id = $request->get_param('config_id');
        $custom_additional_id = $request->get_param('custom_additional_id');

        $custom_additional = json_decode($request->get_body(), true);
        if ($id != 0) {
            $post = get_post($id);
            if ($post) {
                $meta_value = get_post_meta($id, 'ncpc-configs-meta', true);
                if (!empty($meta_value)) {
                    $custom_additionals = $meta_value["data"]["additionalOptions"]["customAdditionalsOptions"];

                    if (isset($custom_additionals[$custom_additional_id])) {

                        $custom_additionals[$custom_additional_id] = $custom_additional;
                        $meta_value["data"]["additionalOptions"]["customAdditionalsOptions"] = $custom_additionals;
                        $response = update_post_meta($id, 'ncpc-configs-meta', $meta_value);

                        if ($response) {
                            return rest_ensure_response(["success" => true]);
                        } else {
                            return rest_ensure_response(["message" => "update custom additional failed"]);
                        }
                    } else {
                        return rest_ensure_response(["message" => "update custom additional failed"]);
                    }


                } else {
                    return rest_ensure_response(["message" => "No custom additional setting found"]);
                }

            } else {
                return rest_ensure_response(["message" => "Custom ID invalid"]);
            }
        } else {
            return rest_ensure_response(["message" => "Custom ID invalid"]);
        }
    }

    /**
     * Delete a custom in the custom configuration
     * @param \WP_REST_Request $request Full details about the request.
     * @return \WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
     */
    public function delete_custom_additional_in_custom_additionals_setting($request)
    {
        $id = $request->get_param('config_id');
        $custom_additional_id = $request->get_param('custom_additional_id');
        if ($id != 0) {
            $post = get_post($id);
            if ($post) {
                $meta_value = get_post_meta($id, 'ncpc-configs-meta', true);
                if (!empty($meta_value["data"]["additionalOptions"]["customAdditionalsOptions"])) {
                    $custom_additionals = $meta_value["data"]["additionalOptions"]["customAdditionalsOptions"];

                    array_splice($custom_additionals, $custom_additional_id, 1);

                    $meta_value["data"]["additionalOptions"]["customAdditionalsOptions"] = $custom_additionals;

                    $response = update_post_meta($id, 'ncpc-configs-meta', $meta_value);

                    if ($response) {
                        return rest_ensure_response(["success" => true]);
                    } else {
                        return rest_ensure_response(["message" => "delete custom additional failed"]);
                    }
                } else {
                    return rest_ensure_response(["message" => "No custom setting found"]);
                }

            } else {
                return rest_ensure_response(["message" => "Custom ID invalid"]);
            }
        } else {
            return rest_ensure_response(["message" => "Custom ID invalid"]);
        }
    }

    /**
     * Checks if a given request has access to read the items.
     *
     * @param  \WP_REST_Request $request Full details about the request.
     *
     * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
     */
    public function get_config_permissions_check($request)
    {
        if (!is_user_logged_in()) {
            return new WP_Error(
                'rest_forbidden',
                esc_html__("You must be logged in to access this resource.", "neon-channel-product-customizer-free"),
                array('status' => rest_authorization_required_code())
            );
        }

        $method = $request->get_method();

        if ($method === 'GET') {
            if (!current_user_can('manage_product_terms')) {
                return new WP_Error(
                    'rest_forbidden',
                    esc_html__("You do not have permission to read.", "neon-channel-product-customizer-free"),
                    array('status' => 403)
                );
            }
        } elseif ($method === 'POST') {
            if (!current_user_can('manage_product_terms')) {
                return new WP_Error(
                    'rest_forbidden',
                    esc_html__('You do not have permission to create.', "neon-channel-product-customizer-free"),
                    array('status' => 403)
                );
            }
        } elseif ($method === 'PUT' || $method === 'PATCH') {
            if (!current_user_can('manage_product_terms')) {
                return new WP_Error(
                    'rest_forbidden',
                    esc_html__("You do not have permission to edit.", "neon-channel-product-customizer-free"),
                    array('status' => 403)
                );
            }
        } elseif ($method === 'DELETE') {
            if (!current_user_can('manage_product_terms')) {
                return new WP_Error(
                    'rest_forbidden',
                    esc_html__("You don't have permission to delete.", "neon-channel-product-customizer-free"),
                    array('status' => 403)
                );
            }
        }

        return true;
    }
}