<?php
/**
 * Branded maintenance screen template.
 *
 * @package Polanger_Maintenance_Center
 */

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

$pmc_context = isset( $pmc_context ) && is_array( $pmc_context ) ? $pmc_context : array();

$defaults = array(
  'page_title'          => wp_get_document_title() ? wp_get_document_title() : wp_strip_all_tags( get_bloginfo( 'name' ), true ),
  'site_title'          => wp_strip_all_tags( get_bloginfo( 'name' ), true ),
  'headline'            => __( 'We will be back shortly.', 'polanger-admin-menu-manager' ),
  'headline_alignment'  => 'center',
  'message'             => __( 'The website is temporarily unavailable while planned maintenance or deployment work is completed.', 'polanger-admin-menu-manager' ),
  'message_alignment'   => 'center',
  'status_note'         => '',
  'countdown_label'     => '',
  'button_label'        => __( 'Return to homepage', 'polanger-admin-menu-manager' ),
  'button_url'          => home_url( '/' ),
  'logo_url'            => '',
  'logo_alignment'      => 'center',
  'footer_content'      => '',
  'footer_alignment'    => 'center',
  'status_code'         => 503,
  'background_style'    => '',
  'robots_content'      => '',
  'countdown'           => array( 'enabled' => false ),
  'custom_css'          => '',
  'custom_js'           => '',
);

$pmc_context = wp_parse_args( $pmc_context, $defaults );
$countdown   = isset( $pmc_context['countdown'] ) && is_array( $pmc_context['countdown'] ) ? $pmc_context['countdown'] : array( 'enabled' => false );
$has_button  = ! empty( trim( (string) $pmc_context['button_label'] ) );
$countdown_label = trim( (string) $pmc_context['countdown_label'] );
$has_status_note = ! empty( trim( (string) $pmc_context['status_note'] ) );

$custom_js = str_replace( '</script>', '<\/script>', (string) $pmc_context['custom_js'] );
?><!doctype html>
<html <?php language_attributes(); ?>>
  <head>
    <meta charset="<?php bloginfo( 'charset' ); ?>" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <?php if ( ! empty( $pmc_context['robots_content'] ) ) : ?>
      <meta name="robots" content="<?php echo esc_attr( $pmc_context['robots_content'] ); ?>" />
    <?php endif; ?>
    <title><?php echo esc_html( $pmc_context['page_title'] ); ?></title>
    <?php wp_print_styles( array( 'polanger-maintenance-center-screen' ) ); ?>
    <?php if ( ! empty( $pmc_context['custom_css'] ) ) : ?>
      <style id="pmc-custom-css">
        <?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
        <?php echo $pmc_context['custom_css']; ?>
      </style>
    <?php endif; ?>
  </head>
  <body class="pmc-maintenance-body" style="<?php echo esc_attr( $pmc_context['background_style'] ); ?>">
    <main class="pmc-shell" role="main" aria-labelledby="pmc-title">
      <div class="pmc-shell__orb pmc-shell__orb--one" aria-hidden="true"></div>
      <div class="pmc-shell__orb pmc-shell__orb--two" aria-hidden="true"></div>
      <div class="pmc-shell__grid"></div>

      <section class="pmc-card">
        <div class="pmc-card__brand pmc-align-<?php echo esc_attr( $pmc_context['logo_alignment'] ); ?>">
          <?php if ( ! empty( $pmc_context['logo_url'] ) ) : ?>
            <img class="pmc-card__logo" src="<?php echo esc_url( $pmc_context['logo_url'] ); ?>" alt="<?php echo esc_attr( $pmc_context['site_title'] ); ?>" />
          <?php else : ?>
            <span class="pmc-card__logo-fallback" aria-hidden="true"><?php echo esc_html( strtoupper( function_exists( 'mb_substr' ) ? mb_substr( $pmc_context['site_title'], 0, 1 ) : substr( $pmc_context['site_title'], 0, 1 ) ) ); ?></span>
          <?php endif; ?>
          <strong class="pmc-card__brand-name"><?php echo esc_html( $pmc_context['site_title'] ); ?></strong>
        </div>

        <div class="pmc-card__copy">
          <h1 id="pmc-title" class="pmc-card__title pmc-align-<?php echo esc_attr( $pmc_context['headline_alignment'] ); ?>">
            <?php echo esc_html( $pmc_context['headline'] ); ?>
          </h1>

          <div class="pmc-card__message pmc-align-<?php echo esc_attr( $pmc_context['message_alignment'] ); ?>">
            <?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
            <?php echo $pmc_context['message']; ?>
          </div>
        </div>

        <?php if ( $has_status_note ) : ?>
          <div class="pmc-card__meta">
            <?php if ( $has_status_note ) : ?>
              <div class="pmc-card__meta-item">
                <span><?php esc_html_e( 'Status note', 'polanger-admin-menu-manager' ); ?></span>
                <strong><?php echo esc_html( $pmc_context['status_note'] ); ?></strong>
              </div>
            <?php endif; ?>
          </div>
        <?php endif; ?>

        <?php if ( ! empty( $countdown['enabled'] ) && ! empty( $countdown['units'] ) ) : ?>
          <section class="pmc-countdown" data-pmc-countdown="1" data-target="<?php echo esc_attr( $countdown['target_iso'] ); ?>">
            <div class="pmc-countdown__head">
              <div>
                <?php if ( '' !== $countdown_label ) : ?>
                  <span class="pmc-countdown__eyebrow"><?php echo esc_html( $countdown_label ); ?></span>
                <?php endif; ?>
                <strong><?php echo esc_html( $countdown['target_label'] ); ?></strong>
              </div>
            </div>

            <div class="pmc-countdown__grid">
              <?php foreach ( $countdown['units'] as $unit ) : ?>
                <div class="pmc-countdown__unit" data-pmc-unit="<?php echo esc_attr( $unit['key'] ); ?>">
                  <strong data-pmc-value>00</strong>
                  <span><?php echo esc_html( $unit['label'] ); ?></span>
                </div>
              <?php endforeach; ?>
            </div>
          </section>
        <?php endif; ?>

        <?php if ( $has_button ) : ?>
          <div class="pmc-card__actions">
            <a class="pmc-card__button" href="<?php echo esc_url( $pmc_context['button_url'] ); ?>">
              <?php echo esc_html( $pmc_context['button_label'] ); ?>
            </a>
          </div>
        <?php endif; ?>

        <?php if ( ! empty( $pmc_context['footer_content'] ) ) : ?>
          <footer class="pmc-card__footer pmc-align-<?php echo esc_attr( $pmc_context['footer_alignment'] ); ?>">
            <?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
            <?php echo $pmc_context['footer_content']; ?>
          </footer>
        <?php endif; ?>
      </section>
    </main>

    <?php wp_print_scripts( array( 'polanger-maintenance-center-screen' ) ); ?>
    <?php if ( ! empty( $custom_js ) ) : ?>
      <script id="pmc-custom-js">
        <?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
        <?php echo $custom_js; ?>
      </script>
    <?php endif; ?>
  </body>
</html>
