<?php

namespace Slack\Command;

use Illuminate\Support\Facades\Log;

use Slack\Base as SlackBase;
use Slack\Message;

abstract class Base extends SlackBase
{
	public function __construct(array $request)
	{
		Log::info(sprintf('SCb:Slack SLASHCOMMAND Initialised [%s]',get_class($this)),['m'=>__METHOD__]);
		parent::__construct($request);
	}

	/**
	 * Enable getting values for keys in the response
	 *
	 * @note: This method is limited to certain values to ensure integrity reasons
	 * @note: Classes should return:
	 *    + channel_id,
	 *    + team_id,
	 *    + ts,
	 *    + user_id
	 * @param string $key
	 * @return mixed|object
	 */
	public function __get(string $key)
	{
		switch ($key) {
			case 'command':
				$command = preg_replace('/^([a-z]+)(\s?.*)/','$1',$this->_data->text);

				return $command ?: 'help';

			case 'slashcommand':
				return object_get($this->_data,'command');

			case 'channel_id':
			case 'response_url':
			case 'enterprise_id':
			case 'team_id':
			case 'user_id':
				return object_get($this->_data,$key);

			case 'text':
				return preg_replace("/^{$this->command}\s*/",'',object_get($this->_data,$key));

			case 'trigger':
				return object_get($this->_data,'trigger_id');
		}
	}

	protected function bot_in_channel(): ?Message
	{
		$o = new Message;

		if (! $this->channel() || ! $this->channel()->active) {
			$o->addAttachment(
				Message\Attachment::item()
					->title(':robot_face: Bot not in this channel')
					->text(sprintf('Please add %s to this channel and try this again.',$this->team()->bot->name ?: 'the BOT'))
					->color('#ff0000')
			);
		}

		return $o->isEmpty() ? NULL : $o;
	}
}