<?php
interface PlatformNotificationInterface
{
    public function getRecipientUserId(): string;
    public function getNotificationEventName(): string;
    public function process(): void;
}

class MessageExtractor{
	var $Message;
	public function __construct(string $xml){
		try{
			$sxe = simplexml_load_string(trim($xml));
			$sxe->registerXPathNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
			$resXML = current($sxe->xpath('//soap:Body'));
			$this->Message = current($resXML->children());	
		}catch(Exception $e){
			die($e->getMessage());
		}
	}
	public function getEbayId(){
		return (string)$this->Message->Item->ItemID;
	}
	public function getName(){
		return $this->Message->getName();
	}
	public function getEventName(){
		return (string)$this->Message->NotificationEventName;
	}
	public function getRecipientUserID(){
		return (string) $this->Message->RecipientUserID;
	}
	
	public function get_auth_token(string $xml_endpoint){
		return hash_hmac('sha256', 
			$xml_endpoint, 
			PLATFORMNOTIFICATION_MASTER_SECRET
		);
	}
}

class ItemClosed implements PlatformNotificationInterface
{
    private string $recipientUserId;
    private string $notificationEventName;
    private object $message;

    public function __construct(string $recipientUserId, string $notificationEventName, object $message)
    {
        $this->recipientUserId = $recipientUserId;
        $this->notificationEventName = $notificationEventName;
        $this->message = $message;
    }

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

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

    public function process(): void
    {
        printf("Processing %s for %s\n", $this->notificationEventName, $this->recipientUserId);
        $auction = \YES4Trade\Model\ebay_auctions::get_by_ebay_id(
        	(string)$this->message->Item->ItemID,
        	['id']
        );
        if($auction === null){
        	printf("\tUnknown Auction %s / Seller %s\n",
        		(string)$this->message->Item->ItemID,
        		$this->getRecipientUserId(),
        	);
        }else{
        	printf("\tAuction %s / Seller %s (yes auction id %d) \n",
        		(string)$this->message->Item->ItemID,
        		$this->recipientUserId,
        		$auction->id
        	);
        }
    }
}

class ItemExtended implements PlatformNotificationInterface
{
    private string $recipientUserId;
    private string $notificationEventName;

    public function __construct(string $recipientUserId, string $notificationEventName)
    {
        $this->recipientUserId = $recipientUserId;
        $this->notificationEventName = $notificationEventName;
    }

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

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

    public function process(): void
    {
        printf("Processing %s for %s\n", $this->notificationEventName, $this->recipientUserId);
    }
}

