<?php

/**
 * Copyright (c) 2019. CDEK-IT. All rights reserved.
 * See LICENSE.md for license details.
 *
 * @author Chizhekov Viktor
 */

namespace CdekSDK2\Tests\Actions;

use CdekSDK2\Actions\Offices;
use CdekSDK2\Dto\PickupPoint;
use CdekSDK2\Dto\PickupPointList;
use CdekSDK2\Client;
use CdekSDK2\Dto\PickupPointLocation;
use CdekSDK2\Http\ApiResponse;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Psr18Client;

class OfficesTest extends TestCase
{
    /**
     * @var Offices
     */
    private $offices;

    protected function setUp(): void
    {
        parent::setUp();
        $psr18Client = new Psr18Client(HttpClient::create([
            'verify_peer' => false,
            'verify_host' => false,
        ]));
        $client = new Client($psr18Client);
        $client->setTest(true);

        $this->offices = $client->offices();
        \Doctrine\Common\Annotations\AnnotationReader::addGlobalIgnoredName('phan');

        /** @phan-suppress-next-line PhanDeprecatedFunction */
        \Doctrine\Common\Annotations\AnnotationRegistry::registerLoader('class_exists');
    }

    protected function tearDown(): void
    {
        parent::tearDown();
        $this->offices = null;
    }


    public function testGet()
    {
        $response = $this->offices->get();
        $this->assertInstanceOf(ApiResponse::class, $response);
    }

    public function testGetFiltered()
    {
        $psr18Client = new Psr18Client(HttpClient::create([
            'verify_peer' => false,
            'verify_host' => false,
        ]));
        $client = new Client($psr18Client);
        $client->setTest(true);
        $this->offices = $client->offices();

        $response = $this->offices->getFiltered(['country_code' => 'AE']);
        $this->assertInstanceOf(ApiResponse::class, $response);

        /* @var PickupPointList $pickup_list */
        $pickup_list = $client->formatResponseList($response, PickupPointList::class);
        $this->assertInstanceOf(PickupPointList::class, $pickup_list);

        foreach ($pickup_list->items as $item) {
            $this->assertInstanceOf(PickupPoint::class, $item);
            $this->assertInstanceOf(PickupPointLocation::class, $item->location);
        }
    }

    public function testParseFilter()
    {
        $filter = [
            'type' => 'PVZ',
            'city_code' => 1,
        ];
        $class = new \ReflectionClass(Offices::class);
        $method = $class->getMethod('parseFilter');
        $method->setAccessible(true);

        $response = $method->invokeArgs($this->offices, [$filter]);

        $this->assertIsString($response);
        $this->assertEquals(http_build_query($filter), $response);
    }

    public function testParseFilterEmptyFilter()
    {
        $filter = [];
        $class = new \ReflectionClass(Offices::class);
        $method = $class->getMethod('parseFilter');
        $method->setAccessible(true);

        $response = $method->invokeArgs($this->offices, [$filter]);

        $this->assertIsString($response);
        $this->assertEquals(http_build_query($filter), $response);
    }

    public function testParseFilterBadParam()
    {
        $filter = [
            'type_pvz' => 'PVZ',
            'unknown' => 1,
        ];
        $class = new \ReflectionClass(Offices::class);
        $method = $class->getMethod('parseFilter');
        $method->setAccessible(true);

        $response = $method->invokeArgs($this->offices, [$filter]);

        $this->assertIsString($response);
        $this->assertEquals('', $response);
    }
}
