<?php

namespace App\Libraries\Adapter\Auth;

use App\Entities\UserEntity;
use App\Models\UserModel;
use App\Models\UserSNSModel;
use App\Libraries\Adapter\Adapter;

// 참고:https://github.com/SyntaxPhoenix/iloclient
abstract class Auth extends Adapter
{
	private $_site = null;
	private $_userModel = null;
	private $_userSNSModel = null;
	private $_formDatas = array();
	protected function __construct(string $site, $debug = false)
	{
		parent::__construct($debug);
		$this->_site = $site;
	}
	final public function getSiteName(): string
	{
		if (is_null($this->_site)) {
			throw new \Exception("Auth Adpater Site명이 정의 되지 않았습니다.");
		}
		return ucfirst($this->_site);
	}
	abstract public function getAuthButton();
	abstract protected function execute_process(): UserEntity;

	final protected function getUserModel(): UserModel
	{
		if (is_null($this->_userModel)) {
			$this->_userModel = new UserModel();
		}
		return $this->_userModel;
	}

	final protected function getUserSNSModel(): UserSNSModel
	{
		if (is_null($this->_userSNSModel)) {
			$this->_userSNSModel = new UserSNSModel();
		}
		return $this->_userSNSModel;
	}

	final public function setFormDatas(array $formDatas)
	{
		$this->_formDatas = $formDatas;
	}
	final public function getFormDatas(): array
	{
		return $this->_formDatas;
	}

	protected function setSession_process(UserEntity $entity): UserEntity
	{
		$this->_session->set(SESSION_NAMES['ISLOGIN'], true);
		$auths = [];
		foreach (array_values(AUTH_FIELDS) as $field) {
			switch ($field) {
				case 'id':
					$auths[$field] = $entity->getPrimaryKey();
					break;
				case 'title':
					$auths[$field] = $entity->getTitle();
					break;
				case 'role':
					$auths[$field] = $entity->$field;
					break;
			}
		}
		$this->_session->set(SESSION_NAMES['AUTH'], $auths);
		return $entity;
	}

	final public function execute(): UserEntity
	{
		return $this->execute_process();
	}
}
