<?php
/**
* @package RSform!Pro
* @copyright (C) 2015 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

// no direct access
defined('_JEXEC') or die;

use Joomla\CMS\Http\HttpFactory;
use Joomla\CMS\Language\Text;

class RSFPDotmailer
{
	protected $username;
	protected $password;
	protected $errors = array();
	protected $url;
	
	public function __construct($username = null, $password = null, $url = null)
	{
		if ($username === null)
		{
			$username = RSFormProHelper::getConfig('dotmailer.username');
		}

		if ($password === null)
		{
			$password = RSFormProHelper::getConfig('dotmailer.password');
		}

		if ($url === null)
		{
			$url = RSFormProHelper::getConfig('dotmailer.url');
		}

		$this->username = $username;
		$this->password = $password;
		$this->url 		= $url;
	}
	
	public function getLists()
	{
		$url = $this->url . '/v2/address-books';
		return $this->connect($url, 'GET');
	}
	
	public function getFields()
	{
		$url = $this->url . '/v2/data-fields';
		return $this->connect($url, 'GET');
	}
	
	public function addContact($id, $contact)
	{
		$url = $this->url . '/v2/address-books/'.$id.'/contacts';
		return $this->connect($url, $contact, 'POST');
	}

	public function updateConsent($id, $data)
	{
		$url = $this->url . '/v2/contacts/'.$id.'/with-consent';
		return $this->connect($url, $data, 'PUT');
	}
	
	public function removeContact($id, $email)
	{
		$url = $this->url . '/v2/contacts/'.$email;
		$user = $this->connect($url);
		
		if ($user->id)
		{
			$url = $this->url . '/v2/address-books/' . $id . '/' . $user->id;
			return $this->connect($url, null, 'DELETE');
		}

		return false;
	}
	
	public function unsubscribeContact($id, $contact)
	{
		$url = $this->url . '/v2/address-books/'.$id.'/contacts/unsubscribe';
		return $this->connect($url, $contact, 'POST');
	}

	public function getConsent($id)
	{
		$url = $this->url . '/v2/contacts/'.$id.'/with-consent';
		return $this->connect($url);
	}
	
	protected function connect($url, $data = null, $method = 'GET')
	{
		// No need to perform requests without credentials
		if (!$this->username || !$this->password)
		{
			return false;
		}

		$http = HttpFactory::getHttp();

		$headers = array(
			'Authorization' => 'Basic ' . base64_encode($this->username . ':' . $this->password)
		);

		if ($data)
		{
			$data = json_encode($data);
			$headers['Content-Type'] = 'application/json';
		}
		else
		{
			$data = null;
		}

		switch ($method)
		{
			case 'POST':
				$response = $http->post($url, $data, $headers);
				break;

			case 'PUT':
				$response = $http->put($url, $data, $headers);
				break;

			case 'GET':
				$response = $http->get($url, $headers);
				break;

			case 'DELETE':
				$response = $http->delete($url, $headers);

				if ($response->code == 204)
				{
					return true;
				}
				break;
		}

		$response = $response->body;

		if (!$response)
		{
			throw new Exception(Text::_('RSFP_DOTMAILER_INVALID_RESPONSE'));
		}

		$result = json_decode($response);

		if ($result === null)
		{
			throw new Exception(Text::_('RSFP_DOTMAILER_PARSING_ERROR'));
		}

		if (is_object($result) && isset($result->message))
		{
			throw new Exception($result->message);
		}

		return $result;
	}
}