<?php
/**
 * GitHub OAuth views file.
 * 
 * @package rundiz-downloads
 * 
 * phpcs:disable Generic.WhiteSpace.ScopeIndent.Incorrect, Generic.WhiteSpace.ScopeIndent.IncorrectExact
 */


if (!defined('ABSPATH')) {
    exit();
}


/* @var $GitHubOAuthListTable \RundizDownloads\App\Models\GitHubOAuthListTable */
?>
<div class="wrap rundiz-downloads-page-githuboauth">
    <h1 class="wp-heading-inline"><?php esc_html_e('GitHub OAuth', 'rundiz-downloads'); ?></h1>

    <?php if (isset($form_result_class) && isset($form_result_msg)) { ?>
    <div class="<?php echo esc_attr($form_result_class); ?> notice is-dismissible">
        <p>
            <strong><?php echo wp_kses_post($form_result_msg); ?></strong>
        </p>
        <button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php esc_html_e('Dismiss this notice.', 'rundiz-downloads'); ?></span></button>
    </div>
    <?php } ?>
    <div class="rundiz-downloads-form-result-placeholder"></div>

    <div class="rundiz-downloads-row">
        <div class="col">
            <?php
            if (isset($githubOAuthLink)) {
                if (!isset($accessToken) || (isset($accessToken) && empty($accessToken))) {
                    echo '<p>';
                    esc_html_e('You are not connected with GitHub. To make auto update, auto correct repository URL works you have to connect to GitHub.', 'rundiz-downloads');
                    echo '</p>' . PHP_EOL;
                }
                echo '<p><a class="button" href="' . esc_url($githubOAuthLink) . '"><i class="fas fa-sign-in-alt"></i> ' . esc_html__('Connect with GitHub', 'rundiz-downloads') . '</a></p>' . PHP_EOL;
            }

            if (isset($responseBody->data->viewer->login)) {
                echo '<div>' . PHP_EOL;
                /* translators: %s: Link to GitHub user profile page and display GitHub logged in name. */
                printf(esc_html__('You had connected to GitHub as %s.', 'rundiz-downloads'), '<a href="https://github.com/' . esc_attr($responseBody->data->viewer->login) . '" target="github_userprofile">' . esc_html($responseBody->data->viewer->login) . '</a>');
                echo '</div>' . PHP_EOL;
            }
            ?>
        </div>
        <div class="col">
            <?php
            if (isset($responseBody->data->viewer->login)) {
                echo '<div class="text-right-sm"><a class="button" href="' . esc_url(admin_url('admin.php?page=' . \RundizDownloads\App\Controllers\Admin\Downloads\GithubOAuth::MENU_SLUG . '&subpage=disconnect')) . '"><i class="fas fa-sign-out-alt"></i> ' . esc_html__('Disconnect from GitHub', 'rundiz-downloads') . '</a></div>' . PHP_EOL;
            }
            ?>
        </div>
    </div><!--.rundiz-downloads-row-->
    <?php
    if (isset($githubSecret)) {
    ?>
    <div class="rundiz-downloads-row">
        <div class="col">
            <?php esc_html_e('Your secret key to use with GitHub auto update', 'rundiz-downloads'); ?>:
            <input id="rundiz_downloads_githubwebhook_secret" type="password" name="rundiz_downloads_githubwebhook_secret" value="<?php echo esc_attr($githubSecret); ?>">
            <button id="rundiz_downloads_showhide_secret" class="button" type="button"><?php esc_html_e('Show/Hide', 'rundiz-downloads'); ?></button>
            <button id="rundiz_downloads_regenerate_secret" class="button" type="button"><i class="fas fa-random"></i> <?php esc_html_e('Re-generate secret', 'rundiz-downloads'); ?></button>
            <button id="rundiz_downloads_forcesync_github_secret" class="button" type="button" title="<?php esc_attr_e('This will be force synchronize secret with your GitHub repositories.', 'rundiz-downloads'); ?>"><i class="fas fa-sync-alt"></i> <?php esc_html_e('Force sync secret on GitHub.', 'rundiz-downloads'); ?></button>
        </div>
    </div><!--.rundiz-downloads-row-->
    <?php
    }
    ?>

    <?php
    if (isset($GitHubOAuthListTable) && is_object($GitHubOAuthListTable)) {
        $GitHubOAuthListTable->display();
    }

    if (isset($responseCached) && true === $responseCached && current_user_can('manage_options')) {
        echo '<p>' . esc_html__('The list above was cached. To retreive the latest list, please clear the cache.', 'rundiz-downloads') . '</p>' . PHP_EOL;
    }
    ?>
</div><!--.wrap-->