<?php

namespace FSPoster\App\SocialNetworks\YoutubeShorts\Adapters;

use FSPoster\App\Providers\Helpers\WPPostThumbnail;
use FSPoster\App\Providers\Schedules\ScheduleObject;
use FSPoster\App\Providers\Schedules\ScheduleShareException;
use FSPoster\App\SocialNetworks\YoutubeShorts\Api\PostingData;

class PostingDataAdapter
{
    private ScheduleObject $scheduleObj;

    public function __construct( ScheduleObject $scheduleObj )
    {
        $this->scheduleObj = $scheduleObj;
    }

    public function getPostingData(): PostingData
    {
        $postingData = new PostingData();

        $postingData->title        = $this->getPostingDataTitle();
        $postingData->description  = $this->getPostingDataDescription();
        $postingData->uploadMedia  = $this->getPostingDataUploadMedia();
        $postingData->privacyLevel = $this->getPostingDataPrivacyLevel();
        $postingData->firstComment = $this->getPostingDataFirstComment();

        if ( ! $this->scheduleObj->readOnlyMode && empty( $postingData->uploadMedia ) )
            throw new ScheduleShareException( fsp__( 'A video is required to share a post on YouTube Shorts.' ) );

        return apply_filters( 'fsp_schedule_posting_data', $postingData, $this->scheduleObj );
    }

    public function getPostingDataTitle(): string
    {
        $scheduleData = $this->scheduleObj->getSchedule()->customization_data_obj;

        $title = $this->scheduleObj->replaceShortCodes( $scheduleData->post_title ?? '', ! $this->scheduleObj->readOnlyMode );

        $title = strip_tags( $title );
        $title = str_replace( [ '&nbsp;', "\r\n" ], [ ' ', "\n" ], $title );

        if ( mb_strlen( $title ) > 100 )
            $title = mb_substr( $title, 0, 97 ) . '...';

        return $title;
    }

    public function getPostingDataDescription(): string
    {
        $scheduleData = $this->scheduleObj->getSchedule()->customization_data_obj;

        $description = $this->scheduleObj->replaceShortCodes( $scheduleData->post_content ?? '', ! $this->scheduleObj->readOnlyMode );

        $description = strip_tags( $description );
        $description = str_replace( [ '&nbsp;', "\r\n" ], [ ' ', "\n" ], $description );

        if ( mb_strlen( $description ) > 5000 )
            $description = mb_substr( $description, 0, 4997 ) . '...';

        return apply_filters( 'fsp_schedule_post_content', $description, $this->scheduleObj );
    }

    public function getCalendarDataContent(): string
    {
        $scheduleData = $this->scheduleObj->getSchedule()->customization_data_obj;

        $postContent = $scheduleData->post_content ?? '';
        $postTitle = $scheduleData->post_title ?? '';

        $content = $this->scheduleObj->replaceShortCodes(
            implode(' | ', [$postTitle, $postContent]),
            ! $this->scheduleObj->readOnlyMode
        );

        $content = strip_tags( $content );
        $content = str_replace( [ '&nbsp;', "\r\n" ], [ ' ', "\n" ], $content );

        if ( mb_strlen( $content ) > 5000 )
            $content = mb_substr( $content, 0, 4997 ) . '...';

        return apply_filters( 'fsp_schedule_post_content', $content, $this->scheduleObj );
    }

    public function getPostingDataUploadMedia(): array
    {
        $scheduleData      = $this->scheduleObj->getSchedule()->customization_data_obj;
        $mediaListToUpload = [];

        if ( $scheduleData->upload_media )
        {
            if ( $scheduleData->upload_media_type === 'featured_image' )
                $mediaIDs = [ $this->scheduleObj->getPostThumbnailID() ];
            elseif ( $scheduleData->upload_media_type === 'all_images' )
                $mediaIDs = $this->scheduleObj->getPostAllAttachedImagesID();
            else
                $mediaIDs = $scheduleData->media_list_to_upload ?? [];

            foreach ( $mediaIDs as $mediaID )
            {
                if ( ! ( $mediaID > 0 ) )
                    continue;

                $path     = WPPostThumbnail::getOrCreateImagePath( $mediaID, $this->scheduleObj->readOnlyMode );
                $mimeType = get_post_mime_type( $mediaID );

                if ( empty( $path ) )
                    continue;

                if ( !is_string($mimeType) || strpos($mimeType, 'video') === false )
                    continue;

                $mediaListToUpload[] = [
                    'id'        => $mediaID,
                    'type'      => 'video',
                    'mime_type' => $mimeType,
                    'path'      => $path,
                    'url'       => wp_get_attachment_url( $mediaID ),
                ];
            }
        }

        return apply_filters( 'fsp_schedule_media_list_to_upload', $mediaListToUpload, $this->scheduleObj );
    }

    public function getPostingDataFirstComment(): string
    {
        $scheduleData = $this->scheduleObj->getSchedule()->customization_data_obj;

        $firstComment = $this->scheduleObj->replaceShortCodes( $scheduleData->first_comment ?? '', ! $this->scheduleObj->readOnlyMode );

        $firstComment = strip_tags( $firstComment );
        $firstComment = str_replace( [ '&nbsp;', "\r\n" ], [ ' ', "\n" ], $firstComment );

        if ( mb_strlen( $firstComment ) > 10000 )
            $firstComment = mb_substr( $firstComment, 0, 9997 ) . '...';

        return $firstComment;
    }

    public function getPostingDataPrivacyLevel(): string
    {
        $scheduleData = $this->scheduleObj->getSchedule()->customization_data_obj;

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

        $pLevel = $scheduleData->privacy_level ?? $allowedPrivacyLevels[0];

        return in_array( $pLevel, $allowedPrivacyLevels ) ? $pLevel : $allowedPrivacyLevels[0];
    }
}
