<?php

namespace CookieSession\Storage\Serialization;

use Symfony\Component\HttpFoundation\Cookie;

class Collection
{
    protected $encrypted_metadata;
    protected $metadata = [];
    protected $signature;
    protected $size;
    protected $length;
    protected $records = [];


    /**
     * @param           $name
     * @param \DateTime $expire
     * @param string    $domain
     * @param bool|true $secure
     * @param bool|true $httpOnly
     *
     * @return Cookie
     */
    public function toCookie($name, \DateTime $expire, $domain = '', $secure = true, $httpOnly = true)
    {
        return new Cookie($name, $this->encrypted_metadata, $expire, '/', $domain, $secure, $httpOnly);
    }


    /**
     * @param \DateTime $expire
     * @param string    $domain
     * @param bool|true $secure
     * @param bool|true $httpOnly
     *
     * @return array
     */
    public function toCookies(\DateTime $expire, $domain = '', $secure = true, $httpOnly = true)
    {
        $result = [];

        foreach($this->records as $record)
        {
            /** @var $record Record */
            $result[] = $record->toCookie($expire, $domain, $secure, $httpOnly);
        }

        return $result;
    }

    /**
     * @param      $index
     * @param null $default
     *
     * @return Record
     */
    public function at($index, $default = null)
    {
        return isset($this->records[$index]) ? $this->records[$index] : $default;
    }

    /**
     * @param array $metadata
     *
     * @return array
     */
    public function setMetadata(array $metadata)
    {
        $this->metadata = $metadata;

        return $metadata;
    }

    /**
     * @return array
     */
    public function getMetadata()
    {
        return $this->metadata;
    }


    /**
     * @param $metadata
     *
     * @return $this
     */
    public function setEncryptedMetadata($metadata)
    {
        $this->encrypted_metadata = $metadata;

        return $this;
    }

    /**
     * @param $signature
     *
     * @return $this
     */
    public function setSignature($signature)
    {
        $this->signature = $signature;

        return $this;
    }


    /**
     * @param $length
     *
     * @return $this
     */
    public function setLength($length)
    {
        $this->length = $length;

        return $this;
    }


    /**
     * @param array $chunks
     *
     * @return $this
     * @throws \Exception
     */
    public function setChunks(array $chunks)
    {
        foreach($chunks as $chunk)
        {
            $this->add($chunk);
        }

        $this->size = sizeof($this->records);

        return $this;
    }


    /**
     * @return string
     */
    public function getEncryptedMetadata()
    {
        return $this->encrypted_metadata;
    }


    /**
     * @return string
     */
    public function getSignature()
    {
        return $this->signature;
    }


    /**
     * @return int
     */
    public function getSize()
    {
        return $this->size;
    }


    /**
     * @return int
     */
    public function getLength()
    {
        return $this->length;
    }


    /**
     * @return array
     */
    public function getRecords()
    {
        return $this->records;
    }

    /**
     * @param array $chunk
     *
     * @return Record
     * @throws \Exception
     */
    public function add(array $chunk)
    {
        if(!isset($chunk['name']) && !isset($chunk['value']))
        {
            throw new \Exception("No name and value set on the chunk.");
        }

        $record = new Record($chunk['name'], $chunk['value']);

        $this->records[] = $record;

        return $record;
    }
}