<?php

namespace PeytzMail\Entity;

use PeytzMail\Entity\TrackingEvent;
use PeytzMail\Exception\InvalidArgumentException;

/**
 * @package PeytzMail
 */
class TrackingEvents extends AbstractEntity
{
    protected $currentPage   = 0;
    protected $totalRecords  = 0;
    protected $totalPages    = 0;
    protected $nextPage      = '';

    /**
     * Create the tracking events entity from the given JSON string.
     *
     * @param  string                   $json  The JSON string.
     * @param  string                   $event The event name.
     * @throws InvalidArgumentException
     */
    public function createFromJson($json, $event)
    {
        $data = json_decode($json);

        if (!isset($data->$event)) {
            throw new InvalidArgumentException('[Peytz Mail] We cannot create the entity with this JSON data.');
        }

        $this->$event = array();

        foreach ($data->$event as $currentEvent) {
            $currentTrackingEvent = new TrackingEvent;
            $currentTrackingEvent->createFromObject($currentEvent);

            array_push($this->$event, $currentTrackingEvent);
        }

        $this->currentPage  = $data->current_page;
        $this->totalRecords = $data->total_records;
        $this->totalPages   = $data->total_pages;
        $this->nextPage     = $data->next_page;
    }
}
