<?php
/**
 * @package WavypointPopUps
 */
namespace WavypointPopUps\Base;

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

class Frontend extends BaseController {

    public function register() {
        add_action('wp_footer', [$this, 'display_popup']);
    }

    public function display_popup() {
        $popups = get_posts([
            'post_type'      => 'wavypopup',
            'post_status'    => 'publish',
            'posts_per_page' => -1,
            'meta_query'     => [
                [
                    'key'   => '_popup_enabled',
                    'value' => '1'
                ]
            ]
        ]);

        foreach ($popups as $popup) {
            $type = get_post_meta($popup->ID, '_wavypopup_type', true);
            $banner_url = get_post_meta($popup->ID, '_wavypopup_banner_url', true);
            $banner_target_url = get_post_meta($popup->ID, '_wavypopup_target_link', true);
            $location = get_post_meta($popup->ID, '_popup_display_location', true);
            $animation = get_post_meta($popup->ID, '_popup_animation', true);
            $animation_class = $animation ? ' animation-' . esc_attr($animation) : '';
            $enable_confetti = get_post_meta($popup->ID, '_popup_enable_confetti', true);
            $max_width = get_post_meta($popup->ID, '_popup_max_width', true);
            $inline_style = $max_width ? 'style="max-width:' . esc_attr($max_width) . ';"' : '';
            $style = get_post_meta($popup->ID, '_popup_style', true) ;
            $video_url = get_post_meta($popup->ID, '_background_video_url', true);
            $loop      = get_post_meta($popup->ID, '_background_video_loop', true);
            if ( $loop =='yes' ){ 
                $loop = 'loop';
            }
            else{
                $loop ='';
            }
            $opacity   = get_post_meta($popup->ID, '_background_video_opacity', true);
            $opacity_style = $opacity ? 'style="opacity:' . esc_attr($opacity) . ';"' : '';

            if ($this->should_display($location)) {
                
                if( $type == 'banner' ){
                    include $this->plugin_path . 'templates/banner.php';
                }
                else{
                    $template_file = $this->plugin_path . 'templates/' . $style . '.php';
                    include $template_file;
                }
                
            }
        }
    }

    private function should_display($location) {
        if (is_admin()) return false;

        switch ($location) {
            case 'entire_site':
                return true;

            case 'home':
                return is_front_page();

            case 'all_pages':
                return is_page();

            case 'all_posts':
                return is_single() && get_post_type() === 'post';

            case 'specific_page':
                $target_id = get_post_meta(get_the_ID(), '_popup_target_page', true);
                return is_page($target_id);

            case 'specific_post':
                $target_id = get_post_meta(get_the_ID(), '_popup_target_post', true);
                return is_single($target_id);

            default:
                return false;
        }
    }
}
