<?php

namespace Opencart\Extension\Alsendo\Models;

/** deprecated todo use DTO/Entity */
class Service
{
    private const AVAILABLE_MAP_OPERATORS = [
        'ruch',
        'inpost',
        'poczta',
        'dpd',
        'ups',
        'fedex',
        'gls',
        'orlen',
        'orlen_paczka',
        'zasilkovna',
        'balikovna',
        'wedo',
        'ppl',
        'urgent_cargus',
        'sameday',
        'fan_courier',
        'dhl'
    ];

    private const SUPPLIER_LOGO_MAP = [
        'wedo' => 'one_delivery',
    ];

    private const SUPPLIER_TO_OPERATOR_MAP = [
        'dhl_parcel' => 'dhl'
    ];

    private string $externalId;
    private bool $isSelected;
    private string $name;
    private ?string $supplier;
    private ?int $toPoint;
    private ?string $comment;
    private ?int $id;
    private ?int $pointToPoint;
    private ?int $doorToPoint;
    private ?int $pointToDoor;
    private ?int $doorToDoor;
    private ?string $logoUrl;

    public function __construct(array $data = [])
    {
        $this->id = $data['id'] ?? null;
        $this->externalId = $data['external_id'] ?? '';
        $this->isSelected = (bool)($data['is_selected'] ?? false);
        $this->name = $data['name'] ?? '';
        $this->supplier = isset($data['supplier']) ? (string)$data['supplier'] : null;
        $this->toPoint = isset($data['to_point']) ? (int)$data['to_point'] : null;
        $this->comment = isset($data['comment']) ? (string)$data['comment'] : null;
        $this->pointToDoor = isset($data['point_to_door']) ? (int)$data['point_to_door'] : null;
        $this->pointToPoint = isset($data['point_to_point']) ? (int)$data['point_to_point'] : null;
        $this->doorToPoint = isset($data['door_to_point']) ? (int)$data['door_to_point'] : null;
        $this->doorToDoor = isset($data['door_to_door']) ? (int)$data['door_to_door'] : null;
        $this->logoUrl = isset($data['logo_url']) ? (string)$data['logo_url'] : null;
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function setId(?int $id): void
    {
        $this->id = $id;
    }

    public function getExternalId(): string
    {
        return $this->externalId;
    }

    public function isSelected(): bool
    {
        return $this->isSelected;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getCustomName(): ?string
    {
        return $this->comment;
    }

    public function getSupplier(): ?string
    {
        return $this->supplier;
    }

    public function getToPoint(): ?int
    {
        return $this->toPoint;
    }

    public function getComment(): ?string
    {
        return $this->comment;
    }

    public function getDisplayText(): string
    {
        return $this->name;
    }

    public function isDeliveryToPoint(): bool
    {
        return (bool)$this->toPoint;
    }

    public function isSelfPickupTypeAllowed(): bool
    {
        return (bool)$this->toPoint;
    }

    public function isShipViaPickupPointAllowed(): bool
    {
        return (bool)($this->pointToPoint ?? 0) || (bool)($this->pointToDoor ?? 0);
    }

    public function isPickupRequestAllowed(): bool
    {
        return (bool) ($this->doorToPoint ?? 0) || (bool) ($this->doorToDoor ?? 0);
    }

    public function isCourierPickupTypeAllowed(): bool
    {
        return true;
    }

    public function getLogoUrl(): string
    {
        if (!empty($this->logoUrl)) {
            return $this->logoUrl;
        }

        $fileName = strtolower($this->supplier ?? '');

        if (isset(self::SUPPLIER_LOGO_MAP[$fileName])) {
            $fileName = self::SUPPLIER_LOGO_MAP[$fileName];
        }

        return sprintf('/extension/alsendo/admin/view/img/courier_logo/%s.png', $fileName);
    }

    public function getMapOperatorName(): ?string
    {
        if (empty($this->supplier)) {
            return null;
        }

        $supplier = strtolower($this->supplier);

        if (in_array($supplier, array_keys(self::SUPPLIER_TO_OPERATOR_MAP))) {
            $supplier = self::SUPPLIER_TO_OPERATOR_MAP[$supplier];
        }

        if (in_array($supplier, self::AVAILABLE_MAP_OPERATORS)) {
            return $supplier;
        }

        return null;
    }

    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'external_id' => $this->externalId,
            'is_selected' => $this->isSelected,
            'name' => $this->name,
            'supplier' => $this->supplier,
            'to_point' => $this->toPoint,
            'comment' => $this->comment,
            'point_to_point' => $this->pointToPoint,
            'door_to_point' => $this->doorToPoint,
            'point_to_door' => $this->pointToDoor,
            'door_to_door' => $this->doorToDoor
        ];
    }
}
