<?php

namespace AuthStack\Driver\LDAP;

use Buckhill\LDAP\Entry;
use Buckhill\LDAP\EntryCollection;
use Illuminate\Support\Collection;
use function Buckhill\config as cfg;

class User extends \AuthStack\Driver\User\AbstractUser implements \AuthStack\Driver\User\UserInterface
{
    /**
     * @var EntryCollection
     */
    protected $collection;

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

    /**
     * @var bool
     */
    protected $is_admin = false;

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



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

        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 bool
     */
    public function checkIfAdmin()
    {
        // Attribute to fetch from user collection
        $attribute = $this->config('administrative.attribute');

        // Value to check against
        $value = $this->config('administrative.value');

        // If empty (null, false, 0, '') then return false instantly
        if(empty($attribute) || empty($value))
        {
            return false;
        }

        $user_attribute = $this->getCollection()->get($attribute, null);

        if(is_array($user_attribute))
        {
            foreach($user_attribute as $val)
            {
                $result = 0 === strcmp(sha1($value), sha1($val));

                if($result)
                {
                    return true;
                }
            }
        }
        elseif(is_string($user_attribute))
        {
            // Compare hashes of two values
            return 0 === strcmp(sha1($value), sha1($user_attribute));
        }
        else
        {
            return false;
        }
    }

    /**
     * @return bool
     */
    public function isAdmin()
    {
        return $this->is_admin;
    }

    /**
     * @return $this
     */
    public function setAdmin()
    {
        $this->is_admin = true;

        return $this;
    }

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

    /**
     * @return mixed
     */
    public function getEmail()
    {
        return $this->getCollection()->get('email_primary', 'N/A');
    }

    /**
     * @return string
     */
    public function getFullName()
    {
        $c = $this->getCollection();

        return sprintf("%s %s", $c->get('first_name', 'N/A'), $c->get('last_name', 'N/A'));
    }

    /**
     * @return string
     */
    public function getUsername()
    {
        return $this->getCollection()->get('user_username');
    }

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

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

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