<?php

namespace App\Libraries\Adapter\API;

use App\Libraries\Adapter\Adapter;

// 참고:https://github.com/SyntaxPhoenix/iloclient
abstract class API extends Adapter
{
	private $_url = null;
	private $_method = "GET";
	private $_datas = array();
	private $_headers = array('Content-Type: application/json; charset=utf-8');
	protected $_channel = null;
	protected $_serverInfo = array();
	protected $_accountInfo = array();
	protected $_debug = false;
	protected function __construct($debug = false)
	{
		parent::__construct($debug);
	}
	abstract protected function getChannel();
	abstract protected function execute_process(): object;

	final public function setURL(string $url)
	{
		$this->_url = $url;
	}
	final public function getURL(): string
	{
		return $this->_url;
	}
	final public function setMethod(string $method)
	{
		$this->_method = $method;
	}
	final public function getMethod(): string
	{
		return $this->_method;
	}
	final public function setDatas(array $datas)
	{
		$this->_datas = $datas;
	}
	final public function getDatas(): array
	{
		return $this->_datas;
	}
	final public function setHeader($key, $value, $delimiter = ":")
	{
		array_push($this->_headers, "{$key}{$delimiter} {$value}");
	}
	final public function getHeaders(): array
	{
		return $this->_headers;
	}

	final public function setServerInfo($ip, $port)
	{
		$this->_serverInfo['ip'] = $ip;
		$this->_serverInfo['port'] = $port;
	}
	final protected function getServerInfo($scheme = "https://", $delimeter = ":"): string
	{
		return $scheme . $this->_serverInfo['ip'] . $delimeter . $this->_serverInfo['port'];
	}
	final public function setAccountInfo($id, $password)
	{
		$this->_serverInfo['id'] = $id;
		$this->_serverInfo['password'] = $password;
	}
	final protected function getAccountInfo($authType = 'basic'): array
	{
		//type: basic , digest
		return array($this->_serverInfo['id'], $this->_serverInfo['password'], $authType);
	}

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