<?php

namespace FSPoster\App\SocialNetworks\Instagram\App;

use FSPoster\App\Providers\Core\Request;
use FSPoster\App\Providers\Core\Route;
use FSPoster\App\Providers\SocialNetwork\SocialNetworkAddon;

class Bootstrap extends SocialNetworkAddon
{
    protected $slug = 'instagram';
    protected $name = 'Instagram';
    protected $icon = 'fab fa-instagram';
    protected $sort = 20;

    public function getCallbackUrl() : ?string
    {
        return self_admin_url('admin.php');
    }

    public function checkIsCallbackRequest () : bool
    {
        $code = Request::get('code', '', 'string');

        return ! empty( $code );
    }

    public function init ()
    {
        Route::post( 'social-networks/instagram/settings', [ Controller::class, 'saveSettings' ] );
        Route::get( 'social-networks/instagram/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, 5 );
        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( 'login_pass', function () {
            Route::post( 'social-networks/instagram/channel-via-password', [ Controller::class, 'addChannelViaPassword' ] );
            Route::post( 'social-networks/instagram/confirm-two-factor', [ Controller::class, 'confirmTwoFactor' ] );
        } );

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

        // 'app' = Facebook app method, 'app-2' = Instagram app method
        $this->initMethodAny( [ 'app', 'app-2' ], function () {
            add_filter( 'fsp_add_app', [ Listener::class, 'addApp' ], 10, 3 );
            add_filter( 'fsp_auth_get_url', [ Listener::class, 'getAuthURL' ], 10, 5 );
            add_filter( 'fsp_auth_get_channels', [ Listener::class, 'getAuthChannels' ], 10, 4 );
            add_filter( 'fsp_standard_app_get_channels', [ Listener::class, 'getStandardAppChannels' ], 10, 5 );
        } );
    }
}