<?php

add_action('rest_api_init', function () {
    register_rest_route('boxful-fulfillment/cvs-choose-callback', '/family-mart', array(
        'methods'             => 'POST',
        'callback'            => 'chooseCVSStore',
        'permission_callback' => function (WP_REST_Request $request) {
            return true;
        },
    ));
});
add_action('rest_api_init', function () {
    register_rest_route('boxful-fulfillment/cvs-choose-callback', '/seven-eleven', array(
        'methods'             => 'POST',
        'callback'            => 'chooseCVSStore',
        'permission_callback' => function (WP_REST_Request $request) {
            return true;
        },
    ));
});
add_action('rest_api_init', function () {
    register_rest_route('boxful-fulfillment/cvs-choose-callback', '/hi-life', array(
        'methods'             => 'POST',
        'callback'            => 'chooseCVSStore',
        'permission_callback' => function (WP_REST_Request $request) {
            return true;
        },
    ));
});
add_action('rest_api_init', function () {
    register_rest_route('boxful-fulfillment/cvs-choose-callback', '/family-mart-data', array(
        'methods'             => 'GET',
        'callback'            => 'chooseCVSStoreData',
        'permission_callback' => function (WP_REST_Request $request) {
            return true;
        },
    ));
});
add_action('rest_api_init', function () {
    register_rest_route('boxful-fulfillment/cvs-choose-callback', '/seven-eleven-data', array(
        'methods'             => 'GET',
        'callback'            => 'chooseCVSStoreData',
        'permission_callback' => function (WP_REST_Request $request) {
            return true;
        },
    ));
});
add_action('rest_api_init', function () {
    register_rest_route('boxful-fulfillment/cvs-choose-callback', '/hi-life-data', array(
        'methods'             => 'GET',
        'callback'            => 'chooseCVSStoreData',
        'permission_callback' => function (WP_REST_Request $request) {
            return true;
        },
    ));
});

function chooseCVSStore()
{
    $store_id = sanitize_text_field($_POST["store_id"]);
    $store_name = sanitize_text_field($_POST["store_name"]);
    $store_address = sanitize_text_field($_POST["store_address"]);
    $callback_data = sanitize_text_field($_POST["callback_data"]);
    $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
    $new_url = $actual_link . '-data?storeid=' . $store_id . '&storename=' . $store_name . '&address=' . $store_address.'&callback_data='.$callback_data;
    wp_redirect($new_url);
    exit();
}


function chooseCVSStoreData()
{
    $request = $_REQUEST;
    if(!empty($request['callback_data'])){
        $checkout_url = base64_decode($request['callback_data']);
    }else{
        $checkout_url = wc_get_page_permalink('checkout');
    }
    setcookie("boxful_cvs_store_id", sanitize_text_field($_GET["storeid"]), time() + 3600, '/');
    setcookie("boxful_cvs_store_name", sanitize_text_field($_GET["storename"]), time() + 3600, '/');
    setcookie("boxful_cvs_store_address", sanitize_text_field($_GET["address"]), time() + 3600, '/');
    wp_redirect($checkout_url);
    exit();
}