<?php

declare(strict_types=1);

namespace KupShop\BankAutoPaymentBundle\Entity;

class BankTransaction
{
    protected $type;
    protected $price;
    protected $currency;
    protected $id;
    protected $comment;
    protected $accountNumber;
    protected $bankCode;
    protected $variableSymbol;
    protected $ref;
    protected $msg;
    protected $iban;

    public function getType()
    {
        return $this->type;
    }

    public function setType($type): self
    {
        $this->type = $type;

        return $this;
    }

    public function getPrice()
    {
        return $this->price;
    }

    public function setPrice($price): self
    {
        $this->price = $price;

        return $this;
    }

    public function getCurrency()
    {
        return $this->currency;
    }

    public function setCurrency($currency): self
    {
        $this->currency = $currency;

        return $this;
    }

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

    public function setId($id): self
    {
        $this->id = $id;

        return $this;
    }

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

    public function setComment($comment): self
    {
        $this->comment = $comment;

        return $this;
    }

    public function getAccountNumber()
    {
        return $this->accountNumber;
    }

    public function setAccountNumber($accountNumber): self
    {
        $this->accountNumber = $accountNumber;

        return $this;
    }

    public function getBankCode()
    {
        return $this->bankCode;
    }

    public function setBankCode($bankCode): self
    {
        $this->bankCode = $bankCode;

        return $this;
    }

    public function getVariableSymbol()
    {
        return $this->variableSymbol;
    }

    public function setVariableSymbol($variableSymbol): self
    {
        $this->variableSymbol = $variableSymbol;

        return $this;
    }

    public function getRef()
    {
        return $this->ref;
    }

    public function setRef($ref): self
    {
        $this->ref = $ref;

        return $this;
    }

    public function getMsg()
    {
        return $this->msg;
    }

    public function setMsg($msg): self
    {
        $this->msg = $msg;

        return $this;
    }

    public function getAccount(): ?string
    {
        if (!empty($this->accountNumber) && !empty($this->bankCode)) {
            return $this->accountNumber.'/'.$this->bankCode;
        }

        return null;
    }

    public function getOrderIdentifier(): array
    {
        if ($this->getVariableSymbol()) {
            $orderIdentifiers = ['vs' => $this->getVariableSymbol()];
        } else {
            $orderIdentifiers = [
                // Reference plátce
                'ref' => $this->getRef() ?? '',
                // Zpráva pro příjemce
                'msg' => $this->getMsg() ?? '',
                // Poznámka
                'note' => $this->getComment() ?? '',
            ];
        }

        $orderIdentifiers = array_map(function ($x) {
            return ltrim(trim($x ?: ''), '0');
        }, $orderIdentifiers);

        return array_filter($orderIdentifiers);
    }

    public function getIban()
    {
        return $this->iban;
    }

    public function setIban($iban): self
    {
        $this->iban = $iban;

        $parsed = iban_get_parts($iban);

        $this->setAccountNumber($parsed['account']);
        $this->setBankCode($parsed['bank']);

        return $this;
    }
}
