<?php

declare(strict_types=1);

namespace PhpWA\Client;

use PhpWA\Auth\AuthenticationCredentials;
use PhpWA\Auth\ClientPayloadFactory;
use PhpWA\Auth\NoiseCertificateVerifier;
use PhpWA\Exception\ConnectionClosedException;
use PhpWA\Exception\ProtocolException;
use PhpWA\Protocol\Binary\BinaryCodec;
use PhpWA\Protocol\Binary\BinaryNode;
use PhpWA\Runtime\Deadline;
use PhpWA\Transport\Noise\NoiseTransportInterface;





final class ProtocolConnection implements ProtocolConnectionInterface
{
    private const STATE_NEW = 'new';
    private const STATE_OPEN = 'open';
    private const STATE_CLOSED = 'closed';
    private const STATE_FAILED = 'failed';

      private $transport;
      private $payloads;
      private $binary;
      private $certificateVerifier;
      private $state = self::STATE_NEW;

    public function __construct(
        NoiseTransportInterface $transport,
        ?ClientPayloadFactory $payloads = null,
        ?BinaryCodec $binary = null,
        ?callable $certificateVerifier = null
    ) {
        $this->transport = $transport;
        $this->payloads = $payloads ?? new ClientPayloadFactory();
        $this->binary = $binary ?? new BinaryCodec();
        if ($certificateVerifier === null) {
            $verifier = new NoiseCertificateVerifier();
            $certificateVerifier = [$verifier, 'verify'];
        }
        $this->certificateVerifier = $certificateVerifier;
    }

    public function open(AuthenticationCredentials $credentials, Deadline $deadline): void
    {
        if ($this->state !== self::STATE_NEW) {
            throw new ProtocolException('Protocol connection instances may be opened only once.');
        }
        try {
            $payload = $this->payloads->forCredentials($credentials)->serializeToString();
            $this->transport->open(
                $credentials->noiseKey(),
                $credentials->routingInfo(),
                $payload,
                $this->certificateVerifier,
                $deadline
            );
            if (!$this->transport->isOpen()) {
                throw new ConnectionClosedException('Noise transport closed during protocol login.');
            }
            $this->state = self::STATE_OPEN;
        } catch (\Throwable $error) {
            $this->fail();
            throw $error;
        }
    }

    public function send(BinaryNode $node, Deadline $deadline): void
    {
        $this->assertOpen();
        try {
            $this->transport->sendPayload($this->binary->encode($node), $deadline);
        } catch (\Throwable $error) {
            $this->fail();
            throw $error;
        }
    }

    public function receive(Deadline $deadline): BinaryNode
    {
        $this->assertOpen();
        try {
            return $this->binary->decode($this->transport->receivePayload($deadline));
        } catch (\Throwable $error) {
            $this->fail();
            throw $error;
        }
    }

    public function close(): void
    {
        if ($this->state === self::STATE_CLOSED) { return; }
        try {
            $this->transport->close();
        } finally {
            $this->state = self::STATE_CLOSED;
        }
    }

    public function isOpen(): bool
    {
        return $this->state === self::STATE_OPEN && $this->transport->isOpen();
    }

    public function state(): string { return $this->state; }

    private function assertOpen(): void
    {
        if (!$this->isOpen()) {
            throw new ConnectionClosedException('Protocol connection is not open.');
        }
    }

    private function fail(): void
    {
        $this->state = self::STATE_FAILED;
        try { $this->transport->close(); } catch (\Throwable $ignored) {}
    }
}
