<?php
/**
 * Frontend asset loading and initialization.
 *
 * @package BatoCookieConsent
 */

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

final class Bato_Cookie_Consent_Frontend
{
    private Bato_Cookie_Consent_Settings $settings;

    public function __construct(Bato_Cookie_Consent_Settings $settings)
    {
        $this->settings = $settings;
    }

    public function init(): void
    {
        add_action('wp_enqueue_scripts', array($this, 'enqueue_assets'), 100);
    }

    public function enqueue_assets(): void
    {
        $settings = $this->settings->get();

        if (empty($settings['general']['enabled'])) {
            return;
        }

        $asset_file = BATO_COOKIE_CONSENT_PATH . 'assets/frontend.asset.php';
        $asset = file_exists($asset_file)
            ? include $asset_file
            : array('dependencies' => array(), 'version' => BATO_COOKIE_CONSENT_VERSION);

        wp_enqueue_style(
            'bato-cookie-consent',
            BATO_COOKIE_CONSENT_URL . 'assets/frontend.css',
            array(),
            $asset['version']
        );

        $inline_css = $this->inline_css();

        if ('' !== $inline_css) {
            wp_add_inline_style('bato-cookie-consent', $inline_css);
        }

        wp_enqueue_script(
            'bato-cookie-consent',
            BATO_COOKIE_CONSENT_URL . 'assets/frontend.js',
            $asset['dependencies'],
            $asset['version'],
            true
        );

        wp_add_inline_script(
            'bato-cookie-consent',
            'window.BatoCookieConsentConfig = ' . wp_json_encode($this->settings->frontend_config()) . ';',
            'before'
        );
    }

    private function inline_css(): string
    {
        return $this->priority_css();
    }

    private function priority_css(): string
    {
        return <<<'CSS'
body .cc-root .cc-banner .cc-actions .cc-button.cc-button,
body .cc-modal .cc-modal__panel .cc-actions .cc-button.cc-button,
body .cc-embed .cc-embed__button.cc-button {
  border: 0.0625em solid transparent;
  border-radius: var(--cc-radius);
  box-shadow: none;
  cursor: pointer;
  font: 600 0.9375em/1.25 var(--cc-font-family);
  min-height: 2.625em;
  padding: 0 1em;
  text-decoration: none;
}

body .cc-root .cc-banner .cc-actions .cc-button.cc-button::before,
body .cc-root .cc-banner .cc-actions .cc-button.cc-button::after,
body .cc-modal .cc-modal__panel .cc-actions .cc-button.cc-button::before,
body .cc-modal .cc-modal__panel .cc-actions .cc-button.cc-button::after,
body .cc-embed .cc-embed__button.cc-button::before,
body .cc-embed .cc-embed__button.cc-button::after,
body .cc-modal .cc-modal__header .cc-icon-button.cc-icon-button::before,
body .cc-modal .cc-modal__header .cc-icon-button.cc-icon-button::after,
body .cc-modal .cc-category .cc-category__details-toggle.cc-category__details-toggle::before,
body .cc-modal .cc-category .cc-category__details-toggle.cc-category__details-toggle::after {
  content: none;
  display: none;
}

body .cc-root .cc-banner .cc-actions .cc-button--primary.cc-button--primary,
body .cc-modal .cc-modal__panel .cc-actions .cc-button--primary.cc-button--primary,
body .cc-embed .cc-embed__button.cc-button--primary {
  background: var(--cc-color-primary);
  color: var(--cc-color-primary-text);
}

body .cc-root .cc-banner .cc-actions .cc-button--primary.cc-button--primary:hover,
body .cc-modal .cc-modal__panel .cc-actions .cc-button--primary.cc-button--primary:hover,
body .cc-embed .cc-embed__button.cc-button--primary:hover {
  background: var(--cc-color-primary-hover);
}

body .cc-root .cc-banner .cc-actions .cc-button--secondary.cc-button--secondary,
body .cc-modal .cc-modal__panel .cc-actions .cc-button--secondary.cc-button--secondary {
  border-color: var(--cc-color-border);
  background: var(--cc-color-secondary);
  color: var(--cc-color-secondary-text);
}

body .cc-root .cc-banner .cc-actions .cc-button--secondary.cc-button--secondary:hover,
body .cc-modal .cc-modal__panel .cc-actions .cc-button--secondary.cc-button--secondary:hover {
  background: var(--cc-color-secondary-hover);
}

body .cc-root .cc-banner .cc-actions .cc-button--ghost.cc-button--ghost,
body .cc-modal .cc-modal__panel .cc-actions .cc-button--ghost.cc-button--ghost {
  background: transparent;
  color: var(--cc-color-primary);
}

body .cc-root .cc-banner .cc-actions .cc-button--ghost.cc-button--ghost:hover,
body .cc-modal .cc-modal__panel .cc-actions .cc-button--ghost.cc-button--ghost:hover {
  background: color-mix(in srgb, var(--cc-color-primary) 8%, transparent);
  color: var(--cc-color-primary-hover);
}

body .cc-modal .cc-modal__header .cc-icon-button.cc-icon-button {
  display: inline-grid;
  width: 2.25em;
  height: 2.25em;
  place-items: center;
  border: 0.0625em solid transparent;
  border-radius: var(--cc-radius);
  background: var(--cc-color-secondary);
  box-shadow: none;
  color: var(--cc-color-secondary-text);
  font: 600 0.9375em/1.25 var(--cc-font-family);
  padding: 0;
}

body .cc-modal .cc-category .cc-category__details-toggle.cc-category__details-toggle {
  display: inline-flex;
  align-items: center;
  gap: var(--cc-space-xs);
  width: fit-content;
  padding: 0;
  border: 0;
  background: transparent;
  box-shadow: none;
  color: var(--cc-color-primary);
  font: 600 0.875em/1.35 var(--cc-font-family);
  text-align: inherit;
  text-decoration: none;
}

body .cc-modal .cc-category .cc-category__details-toggle.cc-category__details-toggle:hover {
  background: transparent;
  color: var(--cc-color-primary-hover);
  text-decoration: underline;
  text-underline-offset: 0.125em;
}

body .cc-modal .cc-category .cc-switch.cc-switch {
  position: relative;
  display: block;
  width: 2.875em;
  height: 1.625em;
  cursor: pointer;
}

body .cc-modal .cc-category .cc-switch .cc-switch__input[type='checkbox'].cc-switch__input {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  margin: 0;
  appearance: none;
  border: 0;
  background: transparent;
  box-shadow: none;
  cursor: pointer;
  opacity: 0;
}

body .cc-modal .cc-category .cc-switch .cc-switch__input[type='checkbox'].cc-switch__input::before,
body .cc-modal .cc-category .cc-switch .cc-switch__input[type='checkbox'].cc-switch__input::after {
  content: none;
  display: none;
}

body .cc-modal .cc-category .cc-switch .cc-switch__track.cc-switch__track {
  display: block;
  width: 100%;
  height: 100%;
  background: var(--cc-color-border);
  border-radius: 62.4375em;
}

body .cc-modal .cc-category .cc-switch .cc-switch__track.cc-switch__track::after {
  position: absolute;
  top: 0.1875em;
  left: 0.1875em;
  width: 1.25em;
  height: 1.25em;
  content: '';
  background: #fff;
  border-radius: 50%;
  transition: transform 160ms ease;
}

body .cc-modal .cc-category .cc-switch .cc-switch__input[type='checkbox'].cc-switch__input:checked + .cc-switch__track {
  background: var(--cc-color-primary);
}

body .cc-modal .cc-category .cc-switch .cc-switch__input[type='checkbox'].cc-switch__input:checked + .cc-switch__track::after {
  transform: translateX(1.25em);
}

body .cc-modal[dir='rtl'] .cc-category .cc-switch .cc-switch__track.cc-switch__track::after {
  right: 0.1875em;
  left: auto;
}

body .cc-modal[dir='rtl'] .cc-category .cc-switch .cc-switch__input[type='checkbox'].cc-switch__input:checked + .cc-switch__track::after {
  transform: translateX(-1.25em);
}
CSS;
    }
}
