<?php

namespace stripe\endpoints;

use Exception;
use Stripe\Collection;
use Stripe\Exception\ApiErrorException;
use Stripe\Terminal\Location;
use Stripe\Terminal\Reader;
use traits\stripe_endpoint_t;

class stripe_endpoint_readers
{
    use stripe_endpoint_t;

    /**
     * Get all readers
     * @return Collection
     * @throws ApiErrorException
     * @throws Exception
     */
    public function list(): Collection
    {
        return self::getClient()->terminal->readers->all();
    }

    /**
     * Get all readers for a location
     * @throws ApiErrorException  If the location is not set
     * @throws Exception If the location is not set
     */
    public function listLocation(string $location_id): Collection
    {
        return self::getClient()->terminal->readers->all([
            'location' => $location_id
        ]);
    }

    /**
     * Register a reader
     * @param string $label
     * @param string $location
     * @return Reader
     * @throws ApiErrorException
     * @throws Exception
     */
    public function register(string $label, string $location): Reader
    {
        return self::getClient()->terminal->readers->create([
            'label' => $label,
            'location' => $location
        ]);
    }

    /**
     * Get all locations
     * @return Collection
     * @throws ApiErrorException
     * @throws Exception
     */
    public function listLocations(): Collection
    {
        return self::getClient()->terminal->locations->all();
    }

    /**
     * Get a location
     * @param string $location_id
     * @return Location
     * @throws ApiErrorException
     * @throws Exception
     */
    public function retrieveLocation(string $location_id): Location
    {
        return self::getClient()->terminal->locations->retrieve($location_id);
    }

    /**
     * Get a reader
     * @param string $reader_id
     * @return Reader
     * @throws ApiErrorException
     * @throws Exception
     */
    public function retrieve(string $reader_id): Reader
    {
        return self::getClient()->terminal->readers->retrieve($reader_id);
    }

    /**
     * Register a location
     * @param string $label
     * @return Location
     * @throws ApiErrorException
     * @throws Exception
     */
    public function registerLocation(string $label): Location
    {
        return self::getClient()->terminal->locations->create([
            'label' => $label
        ]);
    }

    /**
     * Update a location
     * @param string $location_id
     * @param string $label
     * @return Location
     * @throws ApiErrorException
     * @throws Exception
     */
    public function updateLocation(string $location_id, string $label): Location
    {
        return self::getClient()->terminal->locations->update($location_id, [
            'label' => $label
        ]);
    }

    /**
     * Update a reader
     * @param string $reader_id
     * @param string $label
     * @return Reader
     * @throws ApiErrorException
     * @throws Exception
     */
    public function update(string $reader_id, string $label): Reader
    {
        return self::getClient()->terminal->readers->update($reader_id, [
            'label' => $label
        ]);
    }

    /**
     * Delete a location
     * @param string $location_id
     * @return Location
     * @throws ApiErrorException
     * @throws Exception
     */
    public function deleteLocation(string $location_id): Location
    {
        return self::getClient()->terminal->locations->delete($location_id);
    }

    /**
     * Delete a reader
     * @param string $reader_id
     * @return Reader
     * @throws ApiErrorException
     * @throws Exception
     */
    public function delete(string $reader_id): Reader
    {
        return self::getClient()->terminal->readers->delete($reader_id);
    }

    /**
     * Send a payment intent to a reader
     * @param string $reader
     * @param string $id
     * @return Reader
     * @throws Exception
     */
    public function sendPaymentIntent(string $reader, string $id): Reader
    {
        return self::getClient()->terminal->readers->processPaymentIntent(
            $reader,
            [
                'payment_intent' => $id
            ]
        );
    }

    /**
     * Send a cancel action to a reader
     * @throws ApiErrorException
     * @throws Exception
     */
    public function sendCancelPaymentIntent(string $reader): Reader
    {
        return self::getClient()->terminal->readers->cancelAction(
            $reader
        );
    }

}