<?php

/*
 * This file is part of the AuthStack project.
 *
 * (c) Buckhill Ltd <development@buckhill.co.uk>
 *
 * This source file is subject to the Buckhill license that is bundled
 * with this source code in the file LICENSE.
 */

namespace AuthStack\Driver\LDAP;

use Buckhill\LDAP\Entry;
use function Buckhill\config;
use Buckhill\LDAP\EntryCollection;
use AuthStack\Driver\User\AbstractUser;
use AuthStack\Driver\User\UserInterface;

class User extends AbstractUser implements UserInterface
{
    /** @var  EntryCollection */
    protected $attributes;

    /** @var  string */
    protected $cfg_key;


    /**
     * @param EntryCollection $collection
     * @param                 $cfg
     *
     * @return static
     */
    public static function fromEntryCollection(EntryCollection $collection, $cfg)
    {
        $me = new static();

        $me->cfg_key    = $cfg;
        $me->attributes = $collection;

        $memberof = $collection->get($me->config('administrative.attribute'));

        // Check whether the user is an administrator
        if($memberof instanceof Entry)
        {
            $value = $memberof->getValue();

            if(is_array($value))
            {
                $tempcollection = collect($value);

                $result = $tempcollection->contains($me->config('administrative.value'));
            }
            else
            {
                $result = 0 === strcmp($me->config('administrative.value'), $value);
            }

            if($result)
            {
                $me->setAdmin();
            }
        }

        return $me;
    }

    /**
     * @return EntryCollection
     */
    public function getAttributeCollection()
    {
        return $this->attributes;
    }

    /**
     * @param EntryCollection $collection
     * @return $this
     */
    public function setAttributeCollection(EntryCollection $collection)
    {
        $this->attributes = $collection;

        return $this;
    }

    /**
     * @param string $key
     * @param null   $default
     *
     * @return mixed
     */
    public function getAttribute($key = '', $default = null)
    {
        return $this->attributes->get($key, $default);
    }


    /**
     * @return string
     */
    public function getFullName()
    {
        $name = $this->attributes->get($this->config('settings.name'), null);
        $lname = $this->attributes->get($this->config('settings.lastname'), null);

        $name = ($name instanceof Entry) ? $name->getValue() : 'N/A';
        $lname = ($lname instanceof Entry) ? $lname->getValue() : 'N/A';

        return sprintf("%s %s", $name, $lname);
    }


    /**
     * @return array|float|int|string
     */
    public function getEmail()
    {
        $email = $this->attributes->get($this->config('settings.email'));

        return $email instanceof Entry ? $email->getValue() : 'N/A';
    }


    /**
     * @param      $key
     * @param null $default
     *
     * @return string|int|float|array|null
     */
    protected function config($key, $default = null)
    {
        return config(sprintf('%s.%s', $this->cfg_key, $key), $default);
    }
}
