<?php

namespace AuthStack\AuthProfile;

use AuthStack\Session\Session;
use Illuminate\Support\Collection;
use AuthStack\AuthProfile\Model\AuthProfile;
use AuthStack\AuthProfile\Errors\NoActiveProfileError;

class ProfileManager
{
    protected $collection;
    protected $active_profile;


    /**
     * @param array|null $data
     * @param            $active_profile
     *
     * @return mixed
     */
    public static function fromArray(array $data = null, $active_profile)
    {
        $me = new static;

        return $me
            ->setCollection(collect(is_null($data) || empty($data) ? [] : $data))
            ->setActiveProfile($active_profile)
            ;
    }

    public function __construct()
    {
        $this->collection = collect();
    }


    /**
     * @param Collection $collection
     *
     * @return $this
     */
    public function setCollection(Collection $collection)
    {
        $this->collection = $collection;

        return $this;
    }

    /**
     * @param AuthProfile $profile
     *
     * @return $this
     */
    public function add(AuthProfile $profile)
    {
        $has = $this->getCollection()->where('user_id', $profile->getUserId());

        if(!$has->count())
        {
            $this->getCollection()->push($profile->toArray());
        }

        return $this;
    }


    /**
     * @return Collection
     */
    public function getCollection()
    {
        return $this->collection;
    }


    /**
     * @return bool
     */
    public function isAuthenticated()
    {
        // If there's at least 1 profile in collection, user is authenticated
        return (0 !== $this->getCollection()->count());
    }


    /**
     * @return array
     */
    public function toArray()
    {
        if(!$this->getCollection()->count()) return [];

        return [
            'active' => $this->getActiveProfile(),
            'profiles' => $this->getCollection()->all()
        ];
    }


    /**
     * @param Session $session
     *
     * @return Session
     */
    public function saveTo(Session $session)
    {
        if(!$this->getCollection()->count())
        {
            if($session->has('active'))
            {
                $session->del('active');
            }

            if($session->has('profiles'))
            {
                $session->del('profiles');
            }

            return $session;
        }

        $session->put('active', $this->getActiveProfileId());
        $session->put('profiles', $this->getCollection()->all());

        return $session;
    }

    /**
     * @param string $session_id
     *
     * @return $this
     */
    public function setActiveProfile($session_id = '')
    {
        $this->active_profile = $session_id;

        return $this;
    }


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


    /**
     * @return AuthProfile
     * @throws NoActiveProfileError
     */
    public function getActiveProfile()
    {
        $result = $this->getCollection()->where('session_id', $this->active_profile);

        if(!$result->count())
        {
            throw new NoActiveProfileError("The profile collection did not contain an active profile specified in the session.");
        }

        return AuthProfile::fromArray($result->first());
    }
}