<?php

namespace Opencart\Extension\Alsendo\DTO\Entity;

class ServiceEntity
{
    private const AVAILABLE_MAP_OPERATORS = [
        'ruch',
        'inpost',
        'poczta',
        'dpd',
        'ups',
        'fedex',
        'gls',
        'orlen',
        'orlen_paczka',
        'gls',
        'dhl_parcel',
    ];

    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;

    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;
    }

    public static function fromArray(array $data): self
    {
        return new self([
            'id' => $data['id'] ?? null,
            'external_id' => $data['external_id'] ?? '',
            'is_selected' => isset($data['is_selected']) ? (bool)$data['is_selected'] : false,
            'name' => $data['name'] ?? '',
            'supplier' => $data['supplier'] ?? null,
            'to_point' => $data['to_point'] ?? null,
            'comment' => $data['comment'] ?? 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 isCourierPickupTypeAllowed(): bool
    {
        return true;
    }

    public function getLogoUrl(): string
    {
        return sprintf('/extension/alsendo/admin/view/img/courier_logo/%s.png', strtolower($this->supplier ?? ''));
    }

    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
        ];
    }
}
