<?php

namespace App\Libraries\MyAuth;

use App\Entities\UserEntity;
use App\Models\UserModel;
use App\Models\SNSUserModel;

// 참고:https://github.com/SyntaxPhoenix/iloclient
abstract class MyAuth
{
	private $_userModel = null;
	private $_snsUserModel = null;
	protected $_session = null;
	protected function __construct()
	{
		$this->_session = \Config\Services::session();
	}
	abstract public function getAuthButton();
	abstract public function execute(): UserEntity;

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

	final protected function getUserSNSModel(): SNSUSerModel
	{
		if (is_null($this->_snsUserModel)) {
			$this->_snsUserModel = new SNSUserModel();
		}
		return $this->_snsUserModel;
	}

	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->getPK();
					break;
				case 'title':
					$auths[$field] = $entity->getTitle();
					break;
				case 'role':
					$auths[$field] = $entity->$field;
					break;
			}
		}
		$this->_session->set(SESSION_NAMES['AUTH'], $auths);
		return $entity;
	}
}
