<?php

namespace FSPoster\App\SocialNetworks\YoutubeShorts\App;

use FSPoster\App\Providers\Core\RestRequest;
use FSPoster\App\Providers\Helpers\PluginHelper;
use FSPoster\App\Providers\Core\Settings;

class Controller
{
    public static function saveSettings( RestRequest $request ): array
    {
        if ( ! PluginHelper::canAccessToSettings() )
            return [];

        $postTitle            = $request->param( 'post_title', '', RestRequest::TYPE_STRING );
        $postContent          = $request->param( 'post_content', '', RestRequest::TYPE_STRING );
        $privacyLevel         = $request->param( 'privacy_level', '', RestRequest::TYPE_STRING );
        $shareToFirstComment  = $request->param( 'share_to_first_comment', false, RestRequest::TYPE_BOOL );
        $firstCommentText     = $request->param( 'first_comment_text', '', RestRequest::TYPE_STRING );

        $allowedPrivacyLevels = [ 'public', 'unlisted', 'private' ];

        $privacyLevel = in_array( $privacyLevel, $allowedPrivacyLevels ) ? $privacyLevel : $allowedPrivacyLevels[0];

        Settings::set( 'youtube_shorts_post_title', $postTitle );
        Settings::set( 'youtube_shorts_post_content', $postContent );
        Settings::set( 'youtube_shorts_privacy_level', $privacyLevel );
        Settings::set( 'youtube_shorts_share_to_first_comment', (int) $shareToFirstComment );
        Settings::set( 'youtube_shorts_first_comment_text', $firstCommentText );

        do_action( 'fsp_save_settings', $request, Bootstrap::getInstance()->getSlug() );

        return [];
    }

    public static function getSettings( RestRequest $request ): array
    {
        if ( ! PluginHelper::canAccessToSettings() )
            return [];

        return apply_filters( 'fsp_get_settings', [
            'post_title'              => Settings::get( 'youtube_shorts_post_title', '{post_title}' ),
            'post_content'            => Settings::get( 'youtube_shorts_post_content', '{post_excerpt}' ),
            'privacy_level'           => Settings::get( 'youtube_shorts_privacy_level', 'public' ),
            'share_to_first_comment'  => (bool) Settings::get( 'youtube_shorts_share_to_first_comment', false ),
            'first_comment_text'      => Settings::get( 'youtube_shorts_first_comment_text', '' ),
        ], Bootstrap::getInstance()->getSlug() );
    }
}
