<?php

namespace Podeli\Payment\Models;

class CommitOrder extends Entity
{
    public function __construct()
    {
        $this->data = [
            'amount'            => 0.0,
            'items'             => [],
            'prepaidAmount'     => 0.0,
        ];
    }

    public function setId(string $id)
    {
        $id = $this->prepareOrderId($id);
        $this->data['id'] = $id;
        return $this;
    }

    public function getId()
    {
    	return $this->data['id'];
    }


    public function setPrepaidAmount(float $amount)
    {
        $this->data['prepaidAmount'] = number_format($amount, 2, '.', '');
        return $this;
    }

    public function setAmount(float $amount)
    {
        $this->data['amount'] = number_format($amount, 2, '.', '');
        return $this;
    }

    public function setItems(array $items)
    {
        foreach($items as $item) {
            $this->data['items'][] = $item->toArray();
        }
        return $this;
    }

    public function addItem(Item $item)
    {
        $this->data['items'][] = $item->toArray();
        return $this;
    }

    public function prepareOrderId(string $orderId): string
	{
		$orderId = str_replace(['/','#','?','|',' '], ['-'], $orderId);
		return $orderId;
	}
}
