<?php

class PeepSoConfigSectionModeration extends PeepSoConfigSectionAbstract {
    const SITE_ALERTS_SECTION = 'site_alerts_';

    public function register_config_groups() {
        $this->set_context( 'left' );
        $this->reporting();
        $this->set_context( 'right' );
        $this->sensitive();


        $this->wordfilter();
    }


    private function reporting() {

        // # Enable Reporting
        $this->args( 'children', array( 'site_reporting_types', 'reporting_notify' ) );
        $this->set_field(
            'site_reporting_enable',
            __( 'Enabled', 'peepso-core' ),
            'yesno_switch'
        );

        // # Predefined  Text
        $this->args( 'raw', true );
        $this->args( 'multiple', true );
        $this->args( 'descript', __( 'One per line', 'peepso-core' ) );
        $this->set_field(
            'site_reporting_types',
            __( 'Predefined report reasons', 'peepso-core' ),
            'textarea'
        );

        // # Email alerts
        $this->args( 'descript', __( 'Administrators and Community Administrators will receive emails about new reports', 'peepso-core' ) );
        $this->set_field(
            'reporting_notify_email',
            __( 'Email alerts', 'peepso-core' ),
            'yesno_switch'
        );

        // # Email alerts recipients
        $this->args( 'descript', __( 'One per line', 'peepso-core' ) );
        $this->set_field(
            'reporting_notify_email_list',
            __( 'Additional recipients', 'peepso-core' ),
            'textarea'
        );

        // # Automatic actions - posts
        $this->set_field(
            'site_reporting_actions_posts_header',
            __( 'Automatically unpublish posts', 'peepso-core' ),
            'separator'
        );

        // # Automatically unpublish reported posts after X reports
        $options = [];
        for ( $i = 0; $i <= 50; $i ++ ) {
            $options[ $i ] = $i;
        }
        $options[0] = __('Disabled','peepso-core');
        $this->args( 'options', $options );
        $this->args( 'default', 0 );
        $this->args( 'validation', array( 'numeric' ) );

        $this->set_field(
            'site_reporting_num_unpublish_post',
            __( "Reports threshold", 'peepso-core' ),
            'select'
        );

        // # Automatic actions - users
        $this->set_field(
            'site_reporting_actions_users_header',
            __( 'Automatically ban users', 'peepso-core' ),
            'separator'
        );

        // # Automatically ban a user whose profile is reported X times (#7624). 0 = disabled.
        $ban_options = [];
        for ( $i = 0; $i <= 50; $i ++ ) {
            $ban_options[ $i ] = $i;
        }
        $ban_options[0]=__('Disabled','peepso-core');
        $this->args( 'options', $ban_options );
        $this->args( 'default', 0 );
        $this->args( 'validation', array( 'numeric' ) );

        $this->set_field(
            'site_reporting_num_ban_user',
            __( 'Reports threshold', 'peepso-core' ),
            'select'
        );

        $this->args( 'default', 0 );
        $this->args( 'validation', array( 'numeric' ) );
        $this->args( 'descript', __( '0 to ban permanently', 'peepso-core' ) );
        $this->set_field(
            'site_reporting_ban_days',
            __( 'Ban duration (days)', 'peepso-core' ),
            'text'
        );


        // Build Group
        $this->set_group(
            'reporting',
            __( 'Reporting', 'peepso-core' )
        );
    }

    private function sensitive()
    {
        $this->args('descript', __('If enabled, users will be able to mark "sensitive" posts (NSFW/nudity, spoilers etc). The entire post content will be hidden until clicked.', 'peepso-core'));
        $this->set_field(
            'nsfw',
            __('Enabled', 'peepso-core'),
            'yesno_switch'
        );

        // Build Group
        $this->set_group(
            'nsfw',
            __('Sensitive posts', 'peepso-core')
        );
    }


    private function wordfilter() {
        if(!function_exists('mb_substr') || !function_exists('mb_strlen')) {
            $this->set_field(
                'wordfilter_mb_warning',
                __('PHP functions mb_substr and mb_strlen are recommended for accurate text processing, especially for languages with accents (French, Spanish, Polish, Vietnamese etc) or using non-latin script (Russian, Chinese, Japanese etc).', 'peepso-core'),
                'message'
            );
        }
		# Enable WordFilter
		$this->args('default', 1);
		$this->set_field(
			'wordfilter_enable',
			__('Enabled', 'peepso-core'),
			'yesno_switch'
		);

		// what to filter
		$this->args('default', 1);
		$this->set_field(
			'wordfilter_type_' . PeepSoActivityStream::CPT_POST,
			__('Posts', 'peepso-core'),
			'yesno_switch'
		);

		$this->args('default', 1);
		$this->set_field(
			'wordfilter_type_' . PeepSoActivityStream::CPT_COMMENT,
			__('Comments', 'peepso-core'),
			'yesno_switch'
		);

		if ( class_exists('PeepSoMessagesPlugin') ) {
			$this->args('default', 1);
			$this->set_field(
				'wordfilter_type_' . PeepSoMessagesPlugin::CPT_MESSAGE,
				__('Messages', 'peepso-core'),
				'yesno_switch'
			);
		}

		// How to render — mask style + character combined (value = type + char code).
		$options = array(
			'1b' => '••••',
			'2b' => 'F••k',
			'1a' => '****',
			'2a' => 'F**k',
			'1h' => '####',
			'2h' => 'F##k',
		);
		$this->args('options', $options);
		$this->args('default', '2b');
		$this->set_field(
			'wordfilter_render',
			__('How to render', 'peepso-core'),
			'select'
		);

		$general_config = apply_filters('peepso_wordfilter_general_config', array());

		if(count($general_config) > 0 ) {

			foreach ($general_config as $option) {
				if(isset($option['descript'])) {
					$this->args('descript', $option['descript']);
				}
				if(isset($option['int'])) {
					$this->args('int', $option['int']);
				}
				if(isset($option['default'])) {
					$this->args('default', $option['default']);
				}

				$this->set_field($option['name'], $option['label'], $option['type']);
			}
		}


		// Filtered phrases (moved to the bottom of the section).
		$this->set_field(
			'wordfilter_phrases_header',
			__('Filtered phrases', 'peepso-core'),
			'separator'
		);
		$this->set_field(
			'wordfilter_phrases_desc',
			__('Separate words or phrases with comma or new line.', 'peepso-core'),
			'message'
		);

		// Aggressive — substring match (matches anywhere, even inside longer words). Optional; empty is fine.
		$this->args('descript', __('Matched anywhere, even inside longer words. Best for words that are never part of an innocent word (e.g. profanity).', 'peepso-core'));
		$this->set_field(
			'wordfilter_keywords',
			__('Keywords to remove (aggressive)', 'peepso-core'),
			'textarea'
		);

		// Casual — whole-word match only (experimental).
		$this->args('descript', __('Matched as whole words only, so "ass" will not affect "glass".', 'peepso-core'));
		$this->set_field(
			'wordfilter_keywords_whole',
			__('Keywords to remove (casual)', 'peepso-core'),
			'textarea'
		);

		// Build Group
		$this->set_group(
			'wordfilter',
			__('Word Filter', 'peepso-core')
		);
    }

}

// EOF
