<?php
namespace WACVP\Inc\Email;

use WACVP\Inc\Data;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'GetResponse_WACV' ) ) {

	class GetResponse_WACV {
		public $settings;
		protected $api_key;
		protected static $instance = null;

		function __construct() {
			$this->settings = Data::get_params();
			$this->api_key  = $this->settings[ 'getresponse_api' ];
		}

		public static function get_instance() {
			if ( null == self::$instance ) {
				self::$instance = new self;
			}

			return self::$instance;
		}

		public function get_lists() {
			if ( ! $this->api_key ) {
				return array();
			}

			try {
				$r = wp_remote_get( 'https://api.getresponse.com/v3/campaigns', [
					'headers' => [
						'X-Auth-Token' => 'api-key ' . $this->api_key,
						'Accept'       => 'application/json',
						'Content-Type' => 'application/json',
					]
				] );

				$body = wp_remote_retrieve_body( $r );

				return json_decode( $body );

			} catch ( \Exception $e ) {

			}

			return [];
		}

		public function add_recipient( $email = '', $list_id = '', $firstname = '', $lastname = '', $ip_address = '' ) {
			if ( ! $this->api_key || ! $email || empty( $list_id ) ) {
				return;
			}

//		$list_id = array_map( 'absint', (array) $list_id );
//		$list_object = [];

			$body_object = [
				'email'     => $email,
				'campaign'  => [
					'campaignId' => $list_id
				]
			];

			if ( ! empty( $firstname ) || ! empty( $lastname ) ) {
				$body_object['name'] = $firstname . ' ' . $lastname;
			}

			if ( ! empty( $ip_address ) ) {
				$body_object['ipAddress'] = $ip_address;
			}

			$body = wp_json_encode( $body_object );

			$r = wp_remote_post( 'https://api.getresponse.com/v3/contacts', [
				'body'    => $body,
				'headers' => [
					'X-Auth-Token' => 'api-key ' . $this->api_key,
					'Accept'       => 'application/json',
					'Content-Type' => 'application/json',
				],
			] );
		}

	}
}