<?php
get_header();

while (have_posts()) {
    the_post();
    ob_start();
    do_action('woocommerce_before_single_product_summary');
    $wooGallery = ob_get_contents();
    ob_end_clean();

    $currentProduct = site()->getPost($post, [
        "id",
        "permalink",
        "title",
        "excerpt",
        "acf.rating",
        "acf.total_ratings",
        "acf.activation_fee",
        "acf.about_product_lines",
        "meta._product_attributes",
        "meta._regular_price",
        "acf.fee_text",
        "acf.fee_type",
        "coupon"
    ]);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['variation_id'])) {
    $variation_id = intval($_POST['variation_id']);
    $variation = wc_get_product($variation_id);

    if ($variation && $variation->is_type('variation')) {
        $cart_item_key = WC()->cart->add_to_cart($product->get_id(), 1, $variation_id);

        if ($cart_item_key) {
            echo '<script>window.location.href = "' . wc_get_cart_url() . '";</script>';
            exit;
        }
    }
}
$allProducts = site()->getPosts(
    ['post_type' => 'product', 'nopaging' => true],
    ["id", "title", "permalink", "meta._product_attributes"]
);

$currentProductIsHome = false;

$productsWithConnectionType = [];

foreach ($allProducts as $maProduct) {
    if (strpos($maProduct["title"], "Home") !== false) {
        $homesystemProducts[] = $maProduct;
    }

    if (
        is_array($maProduct["_product_attributes"]) &&
        array_key_exists("style", $maProduct["_product_attributes"]) &&
        is_array($currentProduct["_product_attributes"]) &&
        array_key_exists("connection-type", $currentProduct["_product_attributes"]) &&
        $currentProduct["_product_attributes"]["connection-type"]["value"] == $maProduct["_product_attributes"]["connection-type"]["value"]
    ) {
        $productsWithWearingStyle[] = $maProduct;
    }

    if (
        is_array($maProduct["_product_attributes"]) &&
        array_key_exists("connection-type", $maProduct["_product_attributes"]) &&
        is_array($currentProduct["_product_attributes"]) &&
        array_key_exists("style", $currentProduct["_product_attributes"]) &&
        $currentProduct["_product_attributes"]["style"]["value"] == $maProduct["_product_attributes"]["style"]["value"]
    )

        if (strpos($currentProduct["title"], "Home") !== false) {
            $currentProductIsHome = true;
        }

    if (
        is_array($maProduct["_product_attributes"]) &&
        array_key_exists("connection-type", $maProduct["_product_attributes"]) &&
        is_array($currentProduct["_product_attributes"]) &&
        array_key_exists("connection-type", $currentProduct["_product_attributes"]) &&
        $currentProduct["_product_attributes"]["connection-type"]["value"] == $maProduct["_product_attributes"]["connection-type"]["value"]
    ) {
    }
}

$OntheGoProducts = [];
foreach ($allProducts as $llProduct) {
    if (strpos($llProduct["title"], "Mini Mobile System") !== false || strpos($llProduct["title"], "Mobile System") !== false) {
        $ontheGoProducts[] = $llProduct;
    }
}

$flexOutput = "";
if (is_array(get_field('flex_content'))) {
    foreach (get_field('flex_content') as $flex) {
        $flexOutput .= site()->render($flex['acf_fc_layout'], $flex);
    }
}

$variations = $product->get_available_variations();
foreach ($variations as $variation) {
    $variation_id = $variation['variation_id'];
    $attributes = $variation['attributes']['attribute_rate-plan'];
    $price_html = $variation['price_html'];
}

$cart = WC()->cart;
echo site()->render("single_product", [
    "variation" => $variations,
    "attributes" => $attributes,
    "gallery" => $wooGallery,
    "product" => $currentProduct,
    "add-to-cart-url" => do_shortcode("[add_to_cart_url id='" . $currentProduct['id'] . "']"),
    "flex-content" => $flexOutput,
    "showHomeOptions" => strtolower(substr($currentProduct["title"], 0, 4)) === "home",
    "showOTGOptions" => strtolower($currentProduct["title"]) === "mobile system" || strtolower($currentProduct["title"]) === "mini mobile system",
    "OTGProducts" => array_reverse($ontheGoProducts),
    "homesystemProducts" => array_reverse($homesystemProducts),
    "currentProductIsHome" => $currentProductIsHome,
]);

get_footer('shop');
