<?php

namespace FSPoster\App\SocialNetworks\Twitter\App;

use FSPoster\App\Providers\Core\Container;
use FSPoster\App\Providers\Core\Route;
use FSPoster\App\Providers\SocialNetwork\SocialNetworkAddon;
use FSPoster\App\SocialNetworks\Twitter\App\Factories\CommentServiceFactory;
use FSPoster\App\SocialNetworks\Twitter\App\Factories\TwitterCommentRepositoryFactory;
use FSPoster\App\SocialNetworks\Twitter\App\Factories\WorkerCredentialsFactory;
use FSPoster\App\SocialNetworks\Twitter\App\Repositories\TwitterCommentRepository;
use FSPoster\App\SocialNetworks\Twitter\Api\AppMethod\Api as AppMethodApi;
use FSPoster\App\SocialNetworks\Twitter\Api\AppMethod\AuthData as AppMethodAuthData;
use FSPoster\App\SocialNetworks\Twitter\Api\CookieMethod\Api as CookieMethodApi;
use FSPoster\App\SocialNetworks\Twitter\Api\CookieMethod\AuthData as CookieMethodAuthData;
use FSPoster\App\SocialNetworks\Twitter\Api\CookieMethod\WorkerCredentialsDTO;

class Bootstrap extends SocialNetworkAddon
{
    protected $slug = 'twitter';
    protected $name = 'X (Twitter)';
    protected $icon = 'fab fa-twitter';
    protected $sort = 30;

    public function init()
    {
        Route::post('social-networks/twitter/settings', [ Controller::class, 'saveSettings' ]);
        Route::get('social-networks/twitter/settings', [ Controller::class, 'getSettings' ]);

        add_filter('fsp_share_post', [ Listener::class, 'sharePost' ], 10, 2);
        add_filter('fsp_channel_custom_post_data', [ Listener::class, 'getCustomPostData' ], 10, 3);
        add_filter('fsp_get_insights', [ Listener::class, 'getInsights' ], 10, 3);
        add_filter('fsp_get_channel_session_data', [ Listener::class, 'getChannelSessionData' ], 10, 4);
        add_filter('fsp_get_shared_post_link', [ Listener::class, 'getPostLink' ], 10, 2);
        add_filter('fsp_get_channel_link', [ Listener::class, 'getChannelLink' ], 10, 3);
        add_filter('fsp_get_calendar_data', [ Listener::class, 'getCalendarData' ], 10, 2);
        add_filter('fsp_refresh_channel', [ Listener::class, 'refreshChannel' ], 10, 4);
        add_action('fsp_disable_channel', [ Listener::class, 'disableSocialChannel' ], 10, 3);

        $this->initMethod('cookie', function () {
            Route::post('social-networks/twitter/channel-via-cookie', [ Controller::class, 'addChannelViaCookie' ]);
        });

        $this->initMethod('app', function () {
            add_filter('fsp_add_app', [ Listener::class, 'addApp' ], 10, 3);
            add_filter('fsp_auth_get_url', [ Listener::class, 'getAuthURL' ], 10, 4);
            add_filter('fsp_auth_get_channels', [ Listener::class, 'getAuthChannels' ], 10, 4);
            add_filter('fsp_standard_app_get_channels', [ Listener::class, 'getStandardAppChannels' ], 10, 4);
        });

        $this->registerDependencies();

        add_action('fsp_cron', static function () {
            Container::get(CommentService::class)->init();
        }, 10, 0);
    }

    private function registerDependencies(): void
    {
        $repositoryFactory = new TwitterCommentRepositoryFactory($this->slug);
        $repository = $repositoryFactory->make();
        Container::add(TwitterCommentRepository::class, $repository);

        Container::addTransient(AppMethodApi::class);
        Container::addTransient(CookieMethodApi::class);
        Container::addTransient(AppMethodAuthData::class);
        Container::addTransient(CookieMethodAuthData::class);

        $workerCredentialsFactory = new WorkerCredentialsFactory();
        Container::addTransient(WorkerCredentialsDTO::class, [ $workerCredentialsFactory, 'make' ]);

        $commentServiceFactory = new CommentServiceFactory($repository);
        Container::addTransient(CommentService::class, [ $commentServiceFactory, 'make' ]);
    }
}
