<?php

namespace AuthStack\Driver\SOAP\BPS;

use AuthStack\Driver\User\AbstractUser;
use AuthStack\Driver\User\UserInterface;
use Illuminate\Support\Collection;
use function Buckhill\config;

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

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

    public static function fromArray( $bpsId, $locked, array $array, $cfg )
    {
        $me = new static();

        $me->cfg_key     = $cfg;

        $me->attributes = new Collection($array);
        $me->attributes->offsetSet( 'bpsId',  $bpsId );
        $me->attributes->offsetSet( 'locked', $locked );

        return $me;
    }

    public function checkIfAdmin()
    {
        // TODO::Get information how to check if user is admin from retrieved attributes
    }

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



    /**
     * @return string
     */
    public function getFullName()
    {
        $name = $this->attributes->get( $this->config('settings.name' ), null );
        return $name ?: 'N/A';
    }

    /**
     * @return string
     */
    public function getEmail()
    {
        $name = $this->attributes->get( $this->config('settings.email' ), null );
        return $name ?: 'N/A';
    }

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

    /**
     * @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);
    }

}