<?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;
}

/**
 * conversations
 */
class _conversations extends \IPS\Dispatcher\Controller
{
	/**
	 * @brief	Has been CSRF-protected
	 */
	public static $csrfProtected = TRUE;
			
	/**
	 * Execute
	 *
	 * @return	void
	 */
	public function execute()
	{
		\IPS\Dispatcher::i()->checkAcpPermission( 'conversations_manage' );
		parent::execute();
	}

	/**
	 * ...
	 *
	 * @return	void
	 */
	protected function manage()
	{
		$form = new \IPS\Helpers\Form;
		
		$opts = json_decode( \IPS\Settings::i()->chatbox_con_configs, TRUE );
		$groups = \IPS\Member\Group::groups();
		unset( $groups[ \IPS\Settings::i()->guest_group ] );

		# General
		$form->addTab( 'room_tab_general' );
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_bar_con', \IPS\Settings::i()->chatbox_bar_con, FALSE, array() ) );
		$form->add( new \IPS\Helpers\Form\Upload( 'chatbox_con_notifile', \IPS\Settings::i()->chatbox_con_notifile ? \IPS\File::get( 'chatbox_Nofifile',\IPS\Settings::i()->chatbox_con_notifile ) : NULL, FALSE, array( 
			'allowedFileTypes'	=> array( 'mp3' ),
			'maxFileSize'		=> 1,
			'storageExtension' 	=> 'chatbox_Nofifile',
		), NULL, NULL, NULL, 'chatbox_con_notifile' ) );
		
		# Appearance
		$form->addTab( 'room_tab_style' );		
		$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', isset( $opts['flood'] ) ? \intval( $opts['flood'] ) : 5, FALSE, array( 'unlimited' => 0, 'unlimitedLang' => 'none' ) ) );
		$form->add( new \IPS\Helpers\Form\Number( 'chatbox_length', isset( $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_no_counter_popup', $opts['no_counter_popup'], FALSE ) );

		# Input
		$form->addTab('chatbox_tab_input_media');
		$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( 'chatbox_con_can_upload' ) ), NULL, NULL, NULL, 'chatbox_allow_img' ) );
		$form->add( new \IPS\Helpers\Form\Select( 'chatbox_con_can_upload', array_filter( explode( ',',  \IPS\Settings::i()->chatbox_con_can_upload ) ), FALSE, array( 'options' => $groups, 'parse' => 'normal', 'multiple' => TRUE ), NULL, NULL, NULL, 'chatbox_con_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 ( \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>";
			} ) ) );
		}	
		
		# Ban
		$form->addTab('room_tab_ban');
		$form->add( new \IPS\Helpers\Form\Select( 'chatbox_con_ban_groups', array_filter( explode( ',', \IPS\Settings::i()->chatbox_con_ban_groups ) ), FALSE, array( 'options' => $groups, 'parse' => 'normal', 'multiple' => TRUE ) ) );
		$form->add( new \IPS\Helpers\Form\Member( 'chatbox_con_ban_users', \IPS\Settings::i()->chatbox_con_ban_users ? array_map( array( 'IPS\Member', 'load' ), explode( "\n", \IPS\Settings::i()->chatbox_con_ban_users ) ) : NULL, FALSE, array( 'multiple' => 500 ) ) );
		
		# Advanced options
		$form->addTab( 'chatbox_header_adv' );
		$form->add( new \IPS\Helpers\Form\Number( 'chatbox_con_auto_del', \IPS\Settings::i()->chatbox_con_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( 'chatbox_con_can', array_filter( explode( ',', \IPS\Settings::i()->chatbox_con_can ) ), FALSE, array( 'options' => $groups, 'parse' => 'normal', 'multiple' => TRUE ) ) );
		$form->add( new \IPS\Helpers\Form\Select( 'chatbox_con_get', array_filter( explode( ',', \IPS\Settings::i()->chatbox_con_get ) ), FALSE, array( 'options' => $groups, 'parse' => 'normal', 'multiple' => TRUE ) ) );		
		$form->add( new \IPS\Helpers\Form\Select( 'chatbox_con_can_delete_con', array_filter( explode( ',', \IPS\Settings::i()->chatbox_con_can_delete_con ) ), FALSE, array( 'options' => $groups, 'parse' => 'normal', 'multiple' => TRUE ) ) );
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_bar_perm_del', $opts['bar_perm_del'], FALSE ) );
		$form->add( new \IPS\Helpers\Form\YesNo( 'chatbox_bar_perm_edit', $opts['bar_perm_edit'], FALSE ) );
		$form->add( new \IPS\Helpers\Form\Select( 'chatbox_con_can_bypass_flood', array_filter( explode( ',', \IPS\Settings::i()->chatbox_con_can_bypass_flood ) ), FALSE, array( 'options' => $groups, 'parse' => 'normal', 'multiple' => TRUE ) ) );

		# Group Chat
		$form->addTab('room_tab_gchat');
		$form->add( new \IPS\Helpers\Form\Select( 'chatbox_con_can_groups', array_filter( explode( ',', \IPS\Settings::i()->chatbox_con_can_groups ) ), FALSE, array( 'options' => $groups, 'parse' => 'normal', 'multiple' => TRUE ) ) );
		$form->add( new \IPS\Helpers\Form\Number( 'chatbox_con_g_max_users', \IPS\Settings::i()->chatbox_con_g_max_users, FALSE, array( 'min' => 3, 'max' => 100 ) ) );

		
		/* Save */
		if ( $values = $form->values( TRUE ) )
		{
			$dataOpts = array();
			
			foreach( \array_keys( $values ) as $key )
			{
				if ( ! \in_array( $key, array( 'chatbox_con_can_delete_con', 'chatbox_con_can', 'chatbox_con_can_groups', 'chatbox_con_get', 'chatbox_bar_con', 'chatbox_con_can_bypass_flood', 'chatbox_con_ban_groups', 'chatbox_con_ban_users', 'chatbox_con_can_upload', 'chatbox_con_notifile', 'chatbox_con_g_max_users', 'chatbox_con_auto_del' ) ) )
				{
					$dataOpts[ str_replace( 'chatbox_', '', $key ) ] = $values[ $key ];
					unset( $values[ $key ] );
				}
			}

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

			$form->saveAsSettings( $values );
			
			\IPS\Output::i()->redirect( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : \IPS\Http\Url::internal( '' ) );
		}
		
		/* Output */
		\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('menu__chatbox_chatbox_conversations');
		\IPS\Output::i()->output = $form;
	}
	
	// Create new methods with the same name as the 'do' parameter which should execute it
}