<?php


namespace Ezypay\Resource;


use Ezypay\Contract\IDriver;
use Ezypay\Contract\IResourceCreate;
use Ezypay\Contract\IResourceInvoice;
use Ezypay\Contract\IResourceUpdate;
use Ezypay\Result\ResultProducer;
use Ezypay\Validation\ValidationBase;

/**
 * Class Invoice
 *
 * @package Ezypay\Resource
 */
class Invoice extends Resurce implements IResourceInvoice, IResourceCreate, IResourceUpdate
{
    /**
     * @var string defautl object name
     */
    private $resourceType = 'Invoice';

    /**
     * Invoice constructor.
     * @param IDriver $connector
     * @param ValidationBase $validation
     */
    public function __construct(IDriver $connector, ValidationBase $validation)
    {
        parent::__construct($connector, $validation, $this->resourceType);
    }

    /**
     *
     * Returns a list of Invoices.Merchant ID, customer ID, subscription ID are optional fields but they are good filters for setting the scope of query.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/Invoices/InvoicesApi_GetInvoiceList
     * @param array $data
     * @return ResultProducer
     */
    public function findAll(array $data = array())
    {
        $this->settings(array(
            'url' => !empty($data) ? 'invoices?' . http_build_query($data) : 'invoices',
            'method' => 'GET'
        ));

        return $this->result();
    }

    /**
     *
     * Get an invoice to retrieve detailed information about its payments and statuses.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/Invoices/InvoicesApi_GetInvoice
     * @param $id string The ID of the invoice.
     * @return ResultProducer
     */
    public function findById($id)
    {
        $this->settings(array(
            'url' => 'invoices/' . $id,
            'method' => 'GET'
        ));

        return $this->result();
    }

    /**
     *
     * Get the next invoice to retrieve detailed information about its payments and statuses.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/Invoices/InvoicesApi_GetNextInvoice
     * @param $customerId string The ID of the customer. A unique identifier generated by the system.
     * @param $subscriptionId string The ID of the subscription.
     * @return ResultProducer
     */
    public function getNext($customerId, $subscriptionId)
    {
        $this->settings(array(
            'url' => 'invoices/next?customerId=' . $customerId . '&subscriptionId=' . $subscriptionId,
            'method' => 'GET'
        ));

        return $this->result();
    }

    /**
     *
     * Creates an ad-hoc invoice that is not part of a customers subscription.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/Invoices/InvoicesApi_CreateInvoice
     * @param array $data The invoice array. Refer to request body schema (see link)
     * @return ResultProducer
     */
    public function create(array $data)
    {
        $this->settings(array(
            'url' => 'invoices',
            'method' => 'POST',
            'data' => $data
        ));

        return $this->result();
    }

    /**
     *
     * Updates an existing invoice information. If the invoice has already been paid, any update request will be rejected.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/Invoices/InvoicesApi_UpdateInvoice
     * @param $id string The ID of the invoice.
     * @param array $data The invoice object. Refer to request body schema (see link)
     * @return ResultProducer
     */
    public function update($id, array $data)
    {
        $this->settings(array(
            'url' => 'invoices/' . $id,
            'method' => 'PUT',
            'data' => $data
        ));

        return $this->result();
    }

    /**
     *
     * Invoice that has already been paid can be refund in full using this End Point. Note that if an Invoice that is not of success status, can not be refund.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/Invoices/InvoicesApi_RefundInvoice
     * @param $id string The ID of the invoice
     * @return ResultProducer
     */
    public function refund($id)
    {
        $this->settings(array(
            'url' => 'invoices/' . $id . '/refund',
            'method' => 'PUT'
        ));

        return $this->result();
    }

    /**
     *
     * You can mark the particular Invoice as paid which you have collected offline.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/Invoices/InvoicesApi_MarkAsPaidInvoice
     * @param $id string The ID of the invoice.
     * @return ResultProducer
     */
    public function markAsPaid($id)
    {
        $this->settings(array(
            'url' => 'invoices/' . $id . '/markaspaid',
            'method' => 'PUT'
        ));

        return $this->result();
    }

    /**
     *
     * Write-Off an existing Invoice from billing.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/Invoices/InvoicesApi_DeleteInvoice
     * @param $id string The ID of the invoice
     * @return ResultProducer
     */
    public function delete($id)
    {
        $this->settings(array(
            'url' => 'invoices/' . $id,
            'method' => 'DELETE'
        ));

        return $this->result();
    }

}
