<?php

namespace Ezypay\Resource;


use Ezypay\Contract\IDriver;
use Ezypay\Contract\IResourceCreate;
use Ezypay\Contract\IResourcePaymentMethods;
use Ezypay\Result\ResultProducer;
use Ezypay\Validation\ValidationBase;

/**
 * Class PaymentMethod
 *
 * @package Ezypay\Resource
 */
class PaymentMethod extends Resurce implements IResourcePaymentMethods, IResourceCreate
{

    /**
     * @var string defautl object name
     */
    private $resourceType = 'PaymentMethod';

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

    /**
     * Retrieves the list of available payment method details of an existing customer.
     *
     * @link  https://demoapi.ezypay.com/help/docs#!/PaymentMethods/PaymentMethodsApi_GetPaymentAccountList
     * @param array $data
     * @return ResultProducer
     */
    public function findAll(array $data = array())
    {
        $this->settings(array(
            'url' => !empty($data) ? 'paymentmethods?' . http_build_query($data) : 'paymentmethods',
            'method' => 'GET'
        ));

        return $this->result();
    }

    /**
     * Retrieves a particular payment method details of an existing customer.
     *
     * @param $id string The ID of the payment method.
     * @return ResultProducer
     */
    public function findById($id)
    {
        $this->settings(array(
            'url' => 'paymentmethods/' . $id,
            'method' => 'GET'
        ));

        return $this->result();
    }

    /**
     * Retrieves the default payment method details of an existing customer.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/PaymentMethods/PaymentMethodsApi_GetDefaultPaymentMethod
     * @param array $data
     * @return ResultProducer
     */
    public function findDefault(array $data)
    {
        $this->settings(array(
            'url' => 'paymentmethods/default?' . http_build_query($data),
            'method' => 'GET'
        ));

        return $this->result();
    }

    /**
     * Creates a new bank payment method for an existing customer. If the payment method's owner has no default payment method specified, then the new bank account will become the default. However, if the owner already has a default then it will not change unless setAsDefault flag is set to true.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/PaymentMethods/PaymentMethodsApi_UpdateBank
     * @param array $data The bank account data. Refer to request body schema (see link).
     * @return ResultProducer
     */
    public function create(array $data)
    {
        $this->settings(array(
            'url' => 'paymentmethods/banks',
            'method' => 'POST',
            'data' => $data
        ));

        return $this->result();
    }

    /**
     * You can switch default payment method for an existing customer.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/PaymentMethods/PaymentMethodsApi_UpdateDefaultPaymentMethod
     * @param $paymentMethodId string The ID of the payment method.
     * @param $customerId string The ID of the customer. A unique identifier generated by the system.
     * @return ResultProducer
     */
    public function setDefault($paymentMethodId, $customerId)
    {
        $this->settings(array(
            'url' => 'paymentmethods/' . $paymentMethodId . '/default?customerId=' . $customerId,
            'method' => 'PUT'
        ));

        return $this->result();

    }

    /**
     * You can remove an obsolete Payment Method which belongs to a customer.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/PaymentMethods/PaymentMethodsApi_DeletePaymentMethod
     * @param $paymentMethodId string The ID of the payment method.
     * @param $customerId string The ID of the customer. A unique identifier generated by the system.
     * @return ResultProducer
     */
    public function remove($paymentMethodId, $customerId)
    {
        $this->settings(array(
            'url' => 'paymentmethods/' . $paymentMethodId . '?customerId=' . $customerId,
            'method' => 'DELETE'
        ));

        return $this->result();
    }

}
