<?php
/**
 * Frontend account access helper shortcode.
 *
 * @package Invizo\SubscriptionsLite\Frontend
 */

namespace Invizo\SubscriptionsLite\Frontend;

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

class Auth {

    public static function init() {
        add_shortcode( 'invizo_auth', array( __CLASS__, 'render_shortcode' ) );
    }

    /**
     * @return string
     */
    public static function get_account_page_url() {
        $id = (int) get_option( 'invizo_standalone_account_page_id', 0 );
        if ( ! $id ) {
            $id = (int) get_option( 'invizo_standalone_subscriptions_page_id', 0 );
        }
        if ( $id ) {
            $url = get_permalink( $id );
            if ( $url ) {
                return $url;
            }
        }
        return home_url( '/' );
    }

    /**
     * @return string
     */
    public static function get_login_page_url() {
        $id = (int) get_option( 'invizo_standalone_login_page_id', 0 );
        if ( $id ) {
            $url = get_permalink( $id );
            if ( $url ) {
                return $url;
            }
        }
        return wp_login_url( self::get_account_page_url() );
    }

    /**
     * @return string
     */
    public static function get_register_page_url() {
        $id = (int) get_option( 'invizo_standalone_register_page_id', 0 );
        if ( $id ) {
            $url = get_permalink( $id );
            if ( $url ) {
                return $url;
            }
        }

        if ( get_option( 'users_can_register' ) ) {
            return self::get_registration_action_url();
        }

        return self::get_login_page_url();
    }

    /**
     * @return string
     */
    protected static function get_registration_action_url() {
        if ( function_exists( 'wp_registration_url' ) ) {
            return wp_registration_url();
        }

        return wp_login_url();
    }

    /**
     * @return string
     */
    public static function get_store_page_url() {
        $id = (int) get_option( 'invizo_standalone_store_page_id', 0 );
        if ( $id ) {
            $url = get_permalink( $id );
            if ( $url ) {
                return $url;
            }
        }
        return home_url( '/' );
    }

    /**
     * After successful login/register.
     *
     * @return string
     */
    public static function get_post_login_redirect_url() {
        $url = apply_filters( 'invizo_standalone_post_login_redirect', self::get_account_page_url() );
        return $url ? esc_url_raw( $url ) : home_url( '/' );
    }

    /**
     * @param array $atts Shortcode attributes. view: both | login | register
     * @return string
     */
    public static function render_shortcode( $atts ) {
        $atts = shortcode_atts(
            array(
                'view' => 'both',
            ),
            $atts,
            'invizo_auth'
        );

        $view = strtolower( (string) $atts['view'] );
        if ( ! in_array( $view, array( 'both', 'login', 'register' ), true ) ) {
            $view = 'both';
        }

        if ( is_user_logged_in() ) {
            $account = self::get_account_page_url();
            $logout  = wp_logout_url( get_permalink() ?: home_url( '/' ) );
            ob_start();
            ?>
            <div class="invizo-auth__shell">
                <div class="invizo-auth invizo-auth--logged-in">
                    <header class="invizo-auth__page-head">
                        <p class="invizo-auth__site-name"><?php echo esc_html( get_bloginfo( 'name' ) ); ?></p>
                        <h1 class="invizo-auth__page-title"><?php esc_html_e( 'You are signed in', 'invizo-subscriptions-lite' ); ?></h1>
                    </header>
                    <p class="invizo-auth__intro"><?php esc_html_e( 'Use the links below to continue.', 'invizo-subscriptions-lite' ); ?></p>
                    <p class="invizo-auth__actions">
                        <a class="invizo-auth__btn invizo-auth__btn--primary" href="<?php echo esc_url( $account ); ?>"><?php esc_html_e( 'My account', 'invizo-subscriptions-lite' ); ?></a>
                        <a class="invizo-auth__btn invizo-auth__btn--ghost" href="<?php echo esc_url( $logout ); ?>"><?php esc_html_e( 'Log out', 'invizo-subscriptions-lite' ); ?></a>
                    </p>
                </div>
            </div>
            <?php
            return ob_get_clean();
        }

        $redirect               = self::get_post_login_redirect_url();
        $current_url            = get_permalink() ?: home_url( '/' );
        $login_url              = self::get_login_page_url();
        $register_page_url      = self::get_register_page_url();
        $registration_action    = self::get_registration_action_url();
        $lost_password_url      = wp_lostpassword_url( self::get_account_page_url() );
        $show_login             = ( $view === 'both' || $view === 'login' );
        $registration_available = (bool) get_option( 'users_can_register' );
        $show_register          = ( $view === 'both' || $view === 'register' ) && $registration_available;

        if ( $view === 'login' ) {
            $auth_page_title = __( 'Sign in', 'invizo-subscriptions-lite' );
            $auth_page_lead  = __( 'Use your site account to access your subscriptions and billing.', 'invizo-subscriptions-lite' );
        } elseif ( $view === 'register' ) {
            $auth_page_title = __( 'Create an account', 'invizo-subscriptions-lite' );
            $auth_page_lead  = __( 'Use the standard WordPress registration screen with the same email address you used for checkout.', 'invizo-subscriptions-lite' );
        } else {
            $auth_page_title = __( 'Your account', 'invizo-subscriptions-lite' );
            $auth_page_lead  = __( 'Sign in or create a site account to manage your subscriptions.', 'invizo-subscriptions-lite' );
        }

        ob_start();
        ?>
        <div class="invizo-auth__shell">
            <div class="invizo-auth">
                <header class="invizo-auth__page-head">
                    <p class="invizo-auth__site-name"><?php echo esc_html( get_bloginfo( 'name' ) ); ?></p>
                    <h1 class="invizo-auth__page-title"><?php echo esc_html( $auth_page_title ); ?></h1>
                    <?php if ( $auth_page_lead !== '' ) : ?>
                        <p class="invizo-auth__page-lead"><?php echo esc_html( $auth_page_lead ); ?></p>
                    <?php endif; ?>
                </header>
                <div class="invizo-auth__grid invizo-auth__grid--<?php echo esc_attr( $view ); ?>">
                    <?php if ( $show_login ) : ?>
                    <section class="invizo-auth__panel">
                        <form class="invizo-auth__form" method="post" action="<?php echo esc_url( wp_login_url() ); ?>" aria-label="<?php echo esc_attr__( 'Log in', 'invizo-subscriptions-lite' ); ?>">
                            <input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect ); ?>" />
                            <p class="invizo-auth__field">
                                <label for="invizo_user_login"><?php esc_html_e( 'Username or email', 'invizo-subscriptions-lite' ); ?></label>
                                <input type="text" name="log" id="invizo_user_login" required autocomplete="username" class="invizo-auth__input" />
                            </p>
                            <p class="invizo-auth__field">
                                <label for="invizo_user_pass"><?php esc_html_e( 'Password', 'invizo-subscriptions-lite' ); ?></label>
                                <input type="password" name="pwd" id="invizo_user_pass" required autocomplete="current-password" class="invizo-auth__input" />
                            </p>
                            <p class="invizo-auth__field invizo-auth__field--inline">
                                <label><input type="checkbox" name="rememberme" value="forever" /> <?php esc_html_e( 'Remember me', 'invizo-subscriptions-lite' ); ?></label>
                            </p>
                            <p class="invizo-auth__submit">
                                <button type="submit" class="invizo-auth__btn invizo-auth__btn--primary"><?php esc_html_e( 'Log in', 'invizo-subscriptions-lite' ); ?></button>
                            </p>
                        </form>
                        <p class="invizo-auth__switch">
                            <a href="<?php echo esc_url( $lost_password_url ); ?>"><?php esc_html_e( 'Forgot your password?', 'invizo-subscriptions-lite' ); ?></a>
                        </p>
                        <?php if ( $view === 'login' && $registration_available && $register_page_url !== $current_url ) : ?>
                            <p class="invizo-auth__switch">
                                <a href="<?php echo esc_url( $register_page_url ); ?>"><?php esc_html_e( 'Need an account? Start here', 'invizo-subscriptions-lite' ); ?></a>
                            </p>
                        <?php endif; ?>
                    </section>
                    <?php endif; ?>

                    <?php if ( $show_register ) : ?>
                    <section class="invizo-auth__panel">
                        <p class="invizo-auth__intro"><?php esc_html_e( 'Account registration is handled by WordPress on its secure registration screen.', 'invizo-subscriptions-lite' ); ?></p>
                        <p class="invizo-auth__hint"><?php esc_html_e( 'Use the same email address you paid with so Invizo can match your subscriptions to this account.', 'invizo-subscriptions-lite' ); ?></p>
                        <p class="invizo-auth__submit">
                            <a class="invizo-auth__btn invizo-auth__btn--primary" href="<?php echo esc_url( $registration_action ); ?>"><?php esc_html_e( 'Open registration', 'invizo-subscriptions-lite' ); ?></a>
                        </p>
                        <p class="invizo-auth__switch">
                            <a href="<?php echo esc_url( $login_url ); ?>"><?php esc_html_e( 'Already have an account? Log in', 'invizo-subscriptions-lite' ); ?></a>
                        </p>
                    </section>
                    <?php elseif ( $view === 'both' || $view === 'register' ) : ?>
                    <section class="invizo-auth__panel invizo-auth__panel--muted">
                        <p class="invizo-auth__muted"><?php esc_html_e( 'New account registration is disabled on this site. Contact the site administrator if you need access.', 'invizo-subscriptions-lite' ); ?></p>
                        <?php if ( $login_url !== $current_url ) : ?>
                            <p class="invizo-auth__switch">
                                <a href="<?php echo esc_url( $login_url ); ?>"><?php esc_html_e( 'Log in instead', 'invizo-subscriptions-lite' ); ?></a>
                            </p>
                        <?php endif; ?>
                    </section>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php
        return ob_get_clean();
    }
}
