<?php
/**
 * Created by PhpStorm.
 * User: srecko
 * Date: 3.5.2017.
 * Time: 13:00
 */

namespace AuthStack\Driver\SOAP\BPS\Lib;


class XMLBuilder
{
    /**
     * @var \DOMDocument
     */
    protected $objXML = null;
    /**
     * @var string
     */
    protected $apiUser;
    /**
     * @var string
     */
    protected $apiPassword;
    /**
     * @var string
     */
    protected $apiEnvironment;

    /**
     * XMLBuilder constructor.
     * @param string $apiUser
     * @param string $apiPassword
     * @param string $apiEnvironment
     */
    public function __construct($apiUser, $apiPassword, $apiEnvironment)
    {
        $this->apiUser        = $apiUser;
        $this->apiPassword    = $apiPassword;
        $this->apiEnvironment = $apiEnvironment;

        $this->objXML               = new \DOMDocument(1, 'utf-8');
        $this->objXML->formatOutput = true;
        $rootNode                   = $this->objXML->createElement('request');
        $rootNode->setAttribute('user', $apiUser);
        $rootNode->setAttribute('password', $apiPassword);
        $rootNode->setAttribute('dataset', $apiEnvironment);
        $this->objXML->appendChild($rootNode);

    }

    /**
     * Returns XML document as string
     * @return string
     */
    protected function getXML()
    {
        return $this->objXML->saveXML($this->objXML->documentElement);
    }

    /**
     * Cleans child nodes from root node
     */
    protected function cleanRootNode()
    {
        foreach ($this->objXML->documentElement->childNodes as $node) {
            $this->objXML->documentElement->removeChild($node);
        }
    }

    /**
     * @param string $email
     * @param string $password
     * @return string
     */
    public function authenticate($email, $password)
    {
        $this->cleanRootNode();

        $this->objXML->documentElement->appendChild($this->objXML->createElement('email', $email));
    
/*
        // Encodes password string properly ( e.g. "&" becomes "&amp;" )
        $this->objXML->documentElement->appendChild($this->objXML->createElement('password'))->appendChild($this->objXML->createTextNode($password));
*/
        // Puts password in cdata section
        $this->objXML->documentElement->appendChild($this->objXML->createElement('password'))->appendChild($this->objXML->createCDATASection($password));

        return $this->getXML();
    }

    /**
     * @param string|int $bpsId
     * @return string
     */
    public function logout($bpsId)
    {
        $this->cleanRootNode();

        $this->objXML->documentElement->appendChild($this->objXML->createElement('bpsid', $bpsId));

        return $this->getXML();
    }

    /**
     * @param string|int $bpsId
     * @return string
     */
    public function getUserDetails($bpsId)
    {
        $this->cleanRootNode();

        $account = $this->objXML->createElement('account');
        $account->setAttribute('bpsId', $bpsId);
        $accounts = $this->objXML->createElement('accounts');
        $accounts->appendChild($account);
        $this->objXML->documentElement->appendChild($accounts);

        return $this->getXML();
    }

    /**
     * @param string $token
     * @return string
     */
    public function getUserFromToken($token)
    {
        $this->cleanRootNode();

        $this->objXML->documentElement->appendChild($this->objXML->createElement('token', $token));

        return $this->getXML();
    }

    /**
     * @param string|int $bpsId
     * @return string
     */
    public function getInterests($bpsId)
    {
        $this->cleanRootNode();

        $accounts = $this->objXML->createElement('accounts');
        $accounts->appendChild($this->objXML->createElement('id', $bpsId));
        $this->objXML->documentElement->appendChild($accounts);

        return $this->getXML();
    }
}