<?php
namespace Opencart\Catalog\Controller\Extension\Alsendo\Api;

class ShippingInfo extends \Opencart\System\Engine\Controller
{
    public function checkHasMap(): void
    {
        $json = [];

        if (!isset($this->request->post['shipping_method'])) {
            $json['error'] = 'Missing shipping method';
            $this->response->addHeader('Content-Type: application/json');
            $this->response->setOutput(json_encode($json));
            return;
        }

        $shippingMethod = $this->request->post['shipping_method'];
        $json = $this->getMapData($shippingMethod);

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }

    public function getShippingCountryCode(): void
    {
        $isoCode = $this->session->data['shipping_address']['iso_code_2'] ?? null;
        if ($isoCode !== null && $isoCode !== '') {
            $isoCode = strtoupper((string) $isoCode);
        } else {
            $isoCode = null;
        }

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode(['iso_code_2' => $isoCode]));
    }

    public function storeSelectedPickupPoint(): void
    {
        $pickupPoint = $this->request->post['pickup_point'] ?? null;

        if (!empty($pickupPoint)) {
            if (is_array($pickupPoint)) {
                $pickupPointJson = json_encode($pickupPoint);
            } else {
                $pickupPointJson = html_entity_decode((string) $pickupPoint, ENT_QUOTES, 'UTF-8');
            }

            $this->load->model('extension/alsendo/module/alsendo_order_selected_pickup_points');
            $this->model_extension_alsendo_module_alsendo_order_selected_pickup_points->create(
                $pickupPointJson,
                null
            );
        }

        if (empty($pickupPoint)) {
            unset($this->session->data['alsendo_pickup_point']);
        } else {
            $this->session->data['alsendo_pickup_point'] = $pickupPoint;
        }

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode(['success' => true]));
    }

    private function getMapData(string $methodKey): array
    {
        $this->load->model('extension/alsendo/shipping/services');
        $methods = $this->model_extension_alsendo_shipping_services->getSelectedServicesByCurrentRegion();

        if (isset($methods[$methodKey]) && (!empty($methods[$methodKey]['has_map'] || !empty($methods[$methodKey]['to_point'])))) {
            $operator = $methods[$methodKey]['map_operator'] ?? null;

            if ($operator === null) {
                $operator = $methods[$methodKey]['supplier'] ?? null;
            }

            return [
                'success' => true,
                'has_map' => ((bool) $methods[$methodKey]['has_map']) || ((bool) $methods[$methodKey]['to_point']),
                'operator' => $operator ? strtolower($operator) : null,
            ];
        }

        return [
            'success' => true,
            'has_map' => false,
            'operator' => null,
        ];
    }
}
