<?php


namespace IPS\chatbox\modules\admin\chatbox;

/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
	header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
	exit;
}

/**
 * rooms
 */
class _rooms extends \IPS\Node\Controller
{
	
	/**
	 * @brief	Has been CSRF-protected
	 */
	public static $csrfProtected = TRUE;
	
	/**
	 * Node Class
	 */
	protected $nodeClass = '\IPS\chatbox\Rooms\Room';
	
	
	/**
	 * Execute
	 *
	 * @return	void
	 */
	public function execute()
	{
		\IPS\Dispatcher::i()->checkAcpPermission( 'rooms_manage' );
		
		\IPS\Output::i()->cssFiles = array_merge( \IPS\Output::i()->cssFiles, \IPS\Theme::i()->css( 'skins.css', 'chatbox', 'front' ) );

		parent::execute();
	}
	
	/**
	 * ...
	 *
	 * @return	void
	 */
	protected function manage()
	{
		\IPS\Output::i()->sidebar['actions'] = array();

		\IPS\Output::i()->sidebar['actions']['notifications'] = array(
			'icon'	=> 'bullhorn',
			'link'	=> \IPS\Http\Url::internal( 'app=chatbox&module=chatbox&controller=rooms&do=notifications' ),
			'title'	=> 'menu__chatbox_chatbox_notifications',
		);	
			
		\IPS\Output::i()->output .= \IPS\Theme::i()->getTemplate( 'forms', 'chatbox', 'admin' )->clubConf();
		parent::manage();
	}

	/**
	 * Notifications settings
	 */
	protected function notifications()
	{
		\IPS\Output::i()->output .= \IPS\Member::loggedIn()->language()->addToStack('chatbox_ntf_help');
		
		$opts = json_decode( \IPS\Settings::i()->chatbox_notifications, TRUE );

		$lang = \IPS\Member::loggedIn()->language();
		
		# Build Form
		$form = new \IPS\Helpers\Form;		
		
		$form->addTab( 'chatbox_notifications_in_rooms' );
		$form->add( new \IPS\Helpers\Form\Node( 'rooms', isset( $opts['rooms'] ) ? $opts['rooms'] : NULL, FALSE, array(
			'class' 			=> 'IPS\chatbox\Rooms\Room', 
			'multiple' 			=> TRUE
		) ) );

		foreach( \IPS\Application::allExtensions( 'core', 'ContentRouter' ) as $key => $router )
		{
			$addedTab = false;
			if ( \in_array( $key, array( 'core_Clubs', 'core_Status', 'forums_Topics', 'gallery_gallery', 'cms_Records', 'calendar_calendar', 'blog_Blog', 'downloads_Files', 'videobox_Videobox', 'musicbox_Musicbox', 'tutorials_tutorials' ) ) )
			{
				foreach( $router->classes as $class )
				{
					$cnt++;
					
					if ( !$addedTab )
					{
						$form->addTab( '__app_' . $class::$application );
						if ( $class::$application == 'core' )
						{
							$form->add( new \IPS\Helpers\Form\YesNo( 'register_notify', $opts['register_notify'], FALSE ) );
						}
						$addedTab = true;
					}
					
					if( isset( $class::$containerNodeClass ) )
					{
						$containerClass = $class::$containerNodeClass;
						$containerName = str_replace( '\\', '_', $containerClass );
						$form->add( new \IPS\Helpers\Form\Node( $containerName, isset( $opts[ $containerName ] ) ? ( $opts[ $containerName ] == '0' ? 0 : $opts[ $containerName ] ) : NULL, FALSE, array(
							'class' 			=> $containerClass, 
							'zeroVal' 			=> 'all', 
							'multiple' 			=> TRUE
						), NULL, NULL, NULL, 'container_' . $cnt ) );
						$lang->words[ $containerName ] = $lang->checkKeyExists( $containerClass::$nodeTitle ) ? $lang->addToStack( $containerClass::$nodeTitle ) : $containerClass::$nodeTitle;
					}
					
					$itemName = str_replace( '\\', '_', $class );
					$form->add( new \IPS\Helpers\Form\YesNo( $itemName, $opts[ $itemName ], FALSE ) );
					$lang->words[$itemName] = $lang->checkKeyExists( $class::$title . '_pl' ) ? $lang->addToStack( $class::$title . '_pl' ) : $class::$title . '_pl';
					
					if ( isset( $class::$commentClass ) )
					{
						$commentClass = $class::$commentClass;
						$commentName = str_replace( '\\', '_', $commentClass );
						$form->add( new \IPS\Helpers\Form\YesNo( $commentName, $opts[ $commentName ], FALSE, array(), NULL, NULL, NULL, 'comment_' . $cnt ) );
						$lang->words[ $commentName ] = $lang->checkKeyExists( $commentClass::$title . '_pl' ) ? $lang->addToStack( $commentClass::$title . '_pl' ) : $commentClass::$title . '_pl';
					}
					
					if ( isset( $class::$reviewClass ) )
					{
						$reviewClass = $class::$reviewClass;
						$reviewName = str_replace( '\\', '_', $reviewClass );
						$form->add( new \IPS\Helpers\Form\YesNo( $reviewName, $opts[ $reviewName ], FALSE, array(), NULL, NULL, NULL, 'review_' . $cnt ) );
						$lang->words[ $reviewName ] = $lang->checkKeyExists( $reviewClass::$title . '_pl' ) ? $lang->addToStack( $reviewClass::$title . '_pl' ) : $reviewClass::$title . '_pl';
					}						
				}
			}
		}
	
		/* Save */
		if ( $values = $form->values( TRUE ) )
		{
			$dataOpts = array();
			
			foreach( \array_keys( $values ) as $key )
			{
				$dataOpts[ $key ] = $values[ $key ];
				unset( $values[ $key ] );
			}

			if ( $dataOpts )
			{
				$values['chatbox_notifications'] = json_encode( $dataOpts );
			}	

			$form->saveAsSettings( $values );
			
			\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=chatbox&module=chatbox&controller=rooms&do=notifications' ), 'saved' );
		}
		
		/* Output */
		\IPS\Output::i()->breadcrumb[] = array( \IPS\Http\Url::internal( "app=chatbox&module=chatbox&controller=rooms" ), 'menu__chatbox_chatbox_rooms' );
		\IPS\Output::i()->breadcrumb[] = array( \IPS\Http\Url::internal( "app=chatbox&module=chatbox&controller=rooms&do=notifications" ), 'menu__chatbox_chatbox_notifications' );		
		\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('menu__chatbox_chatbox_notifications');
		
		\IPS\Output::i()->output .= $form;		
	}	

	
	/**
	 * Clubs settings
	 */
	protected function clubs()
	{
		$opts = json_decode( \IPS\Settings::i()->room_clubs_configs, TRUE );
		
		$groups = \IPS\Member\Group::groups();	
		
		# Build Form
		$form = new \IPS\Helpers\Form;	
		
		$form->addTab( 'room_tab_general' );
		$form->add( new \IPS\Helpers\Form\YesNo( 'room_clubs', \IPS\Settings::i()->room_clubs, FALSE ) );
		$form->add( new \IPS\Helpers\Form\Upload( 'room_clubs_notifile', \IPS\Settings::i()->room_clubs_notifile ? \IPS\File::get( 'chatbox_Nofifile',\IPS\Settings::i()->room_clubs_notifile ) : NULL, FALSE, array( 
			'allowedFileTypes'	=> array( 'mp3' ),
			'maxFileSize'		=> 1,
			'storageExtension' 	=> 'chatbox_Nofifile',
		), NULL, NULL, NULL, 'room_clubs_notifile' ) );

		# Appearance
		$form->addTab( 'room_tab_style' );
		$form->add( new \IPS\Helpers\Form\Number( 'chatbox_height', $opts['height'] ? \intval( $opts['height'] ) : 420, TRUE, array( 'min' => 200 ) ) );
		$form->add( new \IPS\Helpers\Form\Select( 'chatbox_style', $opts ? $opts['style'] : 'standard', FALSE, array( 'options' => \IPS\Application::load('chatbox')->styles() ) ) );	
		foreach( \IPS\Application::load('chatbox')->skins() as $bg )
		{
			$skinClass = $bg != 'white' ? $bg . '_box' : 'ipsType_sectionTitle';
			$skinSelect[ $bg ] = "<div class='ipsBox {$skinClass}' style='height: 20px; width: 80px;'></div>";
		}		
		$form->add( new \IPS\Helpers\Form\Radio( 'chatbox_skin', $opts['skin'] ? $opts['skin'] : 'white', FALSE, array( 'options' => $skinSelect, 'parse' => 'raw' ) ) );		
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_allow_select_skins', $opts['allow_select_skins'], FALSE, array(), NULL, NULL, NULL, 'chatbox_allow_select_skins' ) );
		
		# Chat
		$form->addTab( 'room_tab_chat' );
		$form->add( new \IPS\Helpers\Form\Select( 'chatbox_sort', $opts ? $opts['sort'] : 'asc', FALSE, array( 'options' => array(
			'asc'	=> 'chatbox_asc_sort',
			'desc'	=> 'chatbox_desc_sort'
		) ) ) );		
		$form->add( new \IPS\Helpers\Form\Number( 'chatbox_limit', $opts['limit'] ? \intval( $opts['limit'] ) : 20, TRUE, array( 'min' => 5 ) ) );
		$form->add( new \IPS\Helpers\Form\Number( 'chatbox_interval', $opts['interval'] ? \intval( $opts['interval'] ) : 10000, TRUE, array( 'min' => 2000 ) ) );
		$form->add( new \IPS\Helpers\Form\Number( 'chatbox_flood', $opts['flood'] ? \intval( $opts['flood'] ) : 5, FALSE, array( 'unlimited' => 0, 'unlimitedLang' => 'none' ) ) );
		$form->add( new \IPS\Helpers\Form\Number( 'chatbox_length', $opts['length'] ? \intval( $opts['length'] ) : 300, FALSE, array( 'unlimited' => 0, 'unlimitedLang' => 'unlimited' ) ) );
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_autoload', $opts['autoload'], FALSE ) );
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_showUserList', $opts['showUserList'], FALSE ) );
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_hideAnon', $opts['hideAnon'], FALSE ) );
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_auto_guest', $opts['auto_guest'], FALSE ) );
		
		# Input
		$form->addTab('chatbox_tab_input_media');
		//$form->addHeader('chatbox_tab_input');
		$form->add( new \IPS\Helpers\Form\Select( 'chatbox_use_editor', (int) $opts['use_editor'], FALSE, array(
			'options'	=> array( 0	=> 'chatbox_use_clean_text', 1 => 'chatbox_use_ckeditor' ),
			'toggles'	=> array(
				'0'	=> array( 'chatbox_allow_img', 'chatbox_allow_video', 'chatbox_allow_audio', 'chatbox_giphy', 'chatbox_autogrow' ),
				'1'	=> array( 'chatbox_enter_editor', 'chatbox_editor_upload' )
			)
		) ) );			
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_editor_upload', $opts['editor_upload'], FALSE, array(), NULL, NULL, NULL, 'chatbox_editor_upload' ) );
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_enter_editor', $opts['enter_editor'], FALSE, array(), NULL, NULL, NULL, 'chatbox_enter_editor' ) );
		
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_autogrow', $opts['autogrow'], FALSE, array(), NULL, NULL, NULL, 'chatbox_autogrow' ) );	
		
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_allow_video', $opts['allow_video'], FALSE, array(), NULL, NULL, NULL, 'chatbox_allow_video' ) );
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_allow_audio', $opts['allow_audio'], FALSE, array(), NULL, NULL, NULL, 'chatbox_allow_audio' ) );

		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_allow_img', $opts['allow_img'], FALSE, array( 'togglesOn' => array( 'room_clubs_can_upload' ) ), NULL, NULL, NULL, 'chatbox_allow_img' ) );
		$form->add( new \IPS\Helpers\Form\Select( 'room_clubs_can_upload', array_filter( explode( ',',  \IPS\Settings::i()->room_clubs_can_upload ) ), FALSE, array( 'options' => $groups, 'parse' => 'normal', 'multiple' => TRUE ), NULL, NULL, NULL, 'room_clubs_can_upload' ) );
		
		if ( \method_exists( '\IPS\core\modules\front\system\plugins', 'bimGiphy' ) )
		{
			$form->add( new \IPS\Helpers\Form\Select( 'chatbox_giphy', $opts['giphy'] ? $opts['giphy'] : 'no', FALSE, array( 'options' => array( 'no' => 'chatbox_giphy_no', 'button' => 'chatbox_giphy_button', 'cmd' => 'chatbox_giphy_cmd', 'both' => 'chatbox_giphy_both' ) ), NULL, NULL, NULL, 'chatbox_giphy' ) );	
		}
		else
		{
			$form->add( new \IPS\Helpers\Form\Custom( 'chatbox_giphy', 'no', FALSE, array( 'getHtml' => function(){
				return "<div class='ipsPadding:half'>" . \IPS\Member::loggedIn()->language()->addToStack('chatbox_giphy_req') . "</div>";
			} ) ) );
		}

		if ( \IPS\Application::appIsEnabled('musicbox') )
		{
			$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_musicbox', $opts['musicbox'], FALSE, array(), NULL, NULL, NULL, 'chatbox_musicbox' ) );
		}
		else
		{
			$form->add( new \IPS\Helpers\Form\Custom( 'chatbox_musicbox', FALSE, FALSE, array( 'getHtml' => function(){
				return "<div class='ipsPadding:half'>" . \IPS\Member::loggedIn()->language()->addToStack('chatbox_musicbox_req') . "</div>";
			} ) ) );
		}
		
		if ( \method_exists( '\IPS\core\modules\front\system\plugins', 'sendVoiceToChatbox' ) )
		{
			$form->add( new \IPS\Helpers\Form\Custom( 'chatbox_voice', FALSE, FALSE, array( 'getHtml' => function(){
				try
				{	
					$id = \IPS\Db::i()->select( 'plugin_id', 'core_plugins', array( 'plugin_name=? OR plugin_location=?', 'Voice Messages for Chatbox+', 'voicemsgforchatbox' ) )->first();
				}
				catch( \UnderflowException $e ) { }
				return "<div class='ipsPadding:half'>" . \IPS\Member::loggedIn()->language()->addToStack('chatbox_voice_on', FALSE, array( 'sprintf' => array( $id ) ) ) . "</div>";
			} ) ) );	
		}
		else
		{
			$form->add( new \IPS\Helpers\Form\Custom( 'chatbox_voice', FALSE, FALSE, array( 'getHtml' => function(){
				return "<div class='ipsPadding:half'>" . \IPS\Member::loggedIn()->language()->addToStack('chatbox_voice_req') . "</div>";
			} ) ) );
		}		
		
		# System messages
		$form->addTab('chatbox_tab_auto_msg');
		$form->addHeader('chatbox_header_sysMSG');
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_announcement_on', $opts['announcement_on'] ) );
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_online_on', $opts['online_on'] ) );

		# Advanced options
		$form->addTab( 'chatbox_header_adv' );
		$form->add( new \IPS\Helpers\Form\Number( 'room_clubs_auto_del', \IPS\Settings::i()->room_clubs_auto_del, FALSE, array( 'min' => 0 ), NULL, NULL, \IPS\Member::loggedIn()->language()->addToStack('chatbox_suff_days') ) );
								
		# Permissions
		$form->addTab( 'room_tab_permissions' );	
		$form->add( new \IPS\Helpers\Form\Select( 'room_clubs_can_see_online', array_filter( explode( ',', \IPS\Settings::i()->room_clubs_can_see_online ) ), FALSE, array( 'options' => $groups, 'parse' => 'normal', 'multiple' => TRUE ) ) );
		$form->add( new \IPS\Helpers\Form\Select( 'room_clubs_can_archive', array_filter( explode( ',', \IPS\Settings::i()->room_clubs_can_archive ) ), FALSE, array( 'options' => $groups, 'parse' => 'normal', 'multiple' => TRUE ) ) );
		$form->add( new \IPS\Helpers\Form\Select( 'room_clubs_can_bypass_flood', array_filter( explode( ',', \IPS\Settings::i()->room_clubs_can_bypass_flood ) ), FALSE, array( 'options' => $groups, 'parse' => 'normal', 'multiple' => TRUE ) ) );
		
		/* Save */
		if ( $values = $form->values( TRUE ) )
		{
			$dataOpts = array();
			
			foreach( \array_keys( $values ) as $key )
			{
				if ( mb_strpos( $key, 'chatbox_' ) !== FALSE )
				{
					$dataOpts[ str_replace( 'chatbox_', '', $key ) ] = $values[ $key ];
					unset( $values[ $key ] );
				}
			}

			if ( $dataOpts )
			{
				$values['room_clubs_configs'] = json_encode( $dataOpts );
			}	

			$form->saveAsSettings( $values );
			
			\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=chatbox&module=chatbox&controller=rooms&do=clubs' ), 'saved' );
		}
		
		/* Output */
		\IPS\Output::i()->breadcrumb[] = array( \IPS\Http\Url::internal( "app=chatbox&module=chatbox&controller=rooms" ), 'menu__chatbox_chatbox_rooms' );
		\IPS\Output::i()->breadcrumb[] = array( \IPS\Http\Url::internal( "app=chatbox&module=chatbox&controller=rooms&do=clubs" ), 'room_clubs' );
		\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('room_clubs');
		\IPS\Output::i()->output = $form;		
	}

}