<?php

namespace Convertforce\Frontend;

class DisplayCondition
{
    private static $post = null;
    public static function should_show_popup($display_conditions, $post)
    {
        self::$post = $post;
        if (empty($display_conditions)) {
            return true;
        }

        $first_condition = $display_conditions[0]['condition'] ?? 'exclude';
        $should_show = $first_condition === 'include' ? false : true;

        foreach ($display_conditions as $condition) {
            $rule_type = $condition['condition'] ?? 'include';
            $is_match = self::evaluate_condition($condition);

            if ($rule_type === 'include' && $is_match) {
                $should_show = true;
            } elseif ($rule_type === 'exclude' && $is_match) {
                $should_show = false;
            }
        }

        return $should_show;
    }

    private static function evaluate_condition($condition)
    {
        $general = $condition['general'] ?? 'entire-site';

        switch ($general) {
            case 'entire-site':
                return true;

            case 'singular':
                return self::evaluate_singular_condition($condition);

            case 'archive':
                return self::evaluate_archive_condition($condition);

            default:
                return false;
        }
    }

            private static function evaluate_singular_condition($condition)
    {
        $singular_type = $condition['singular'] ?? 'front-page';
        $singular_items = $condition['singular_items'] ?? [];

        switch ($singular_type) {
            case 'front-page':
                return is_front_page();

            case 'page':
                if (!is_page()) {
                    return false;
                }
                if (empty($singular_items)) {
                    return true;
                }
                return self::match_post_items($singular_items);

            case 'page-child':
                if (!is_page()) {
                    return false;
                }
                if (empty($singular_items)) {
                    return wp_get_post_parent_id(get_the_ID()) > 0;
                }
                return self::match_child_of_items($singular_items);

            case 'page-template':
                if (!is_page()) {
                    return false;
                }
                if (empty($singular_items)) {
                    return true;
                }
                return self::match_page_template($singular_items);

            case '404':
                return is_404();

            case 'post':
                if (!is_single()) {
                    return false;
                }
                if (empty($singular_items)) {
                    return true;
                }
                return self::match_post_items($singular_items);

            case 'post-cat':
                if (!is_single()) {
                    return false;
                }
                if (empty($singular_items)) {
                    return true;
                }
                return self::match_post_categories($singular_items);

            case 'post-tag':
                if (!is_single()) {
                    return false;
                }
                if (empty($singular_items)) {
                    return true;
                }
                return self::match_post_tags($singular_items);

            default:
                return false;
        }
    }


    private static function evaluate_archive_condition($condition)
    {
        $archive_type = $condition['archive'] ?? 'all';
        $archive_items = $condition['archive_items'] ?? [];

        switch ($archive_type) {
            case 'all':
                return is_archive();

            case 'category':
                if (!is_category()) {
                    return false;
                }
                if (empty($archive_items)) {
                    return true;
                }
                return self::match_archive_term($archive_items, 'category');

            case 'tag':
                if (!is_tag()) {
                    return false;
                }
                if (empty($archive_items)) {
                    return true;
                }
                return self::match_archive_term($archive_items, 'post_tag');

            case 'author':
                if (!is_author()) {
                    return false;
                }
                if (empty($archive_items)) {
                    return true;
                }
                return self::match_author_archive($archive_items);

            case 'date':
                return is_date();

            case 'search':
                return is_search();

            default:
                return false;
        }
    }

    private static function match_post_items($items)
    {
        foreach ($items as $item) {
            if (is_numeric($item) && (int) $item === self::$post->ID) {
                return true;
            }
            if (self::$post->post_name === $item) {
                return true;
            }
        }

        return false;
    }

    private static function match_child_of_items($items)
    {
        $ancestors = get_post_ancestors(self::$post->ID);

        if (empty($ancestors)) {
            return false;
        }

        foreach ($items as $item) {
            foreach ($ancestors as $ancestor_id) {
                if (is_numeric($item) && (int) $item === $ancestor_id) {
                    return true;
                }
                $ancestor = get_post($ancestor_id);
                if ($ancestor && $ancestor->post_name === $item) {
                    return true;
                }
            }
        }

        return false;
    }

    private static function match_page_template($items)
    {
        $current_template = get_page_template_slug();

        foreach ($items as $template) {
            if ($current_template === $template) {
                return true;
            }
        }

        return false;
    }

    private static function match_post_categories($items)
    {
        $categories = get_the_category(self::$post->ID);

        if (empty($categories)) {
            return false;
        }

        foreach ($items as $item) {
            foreach ($categories as $category) {
                if (is_numeric($item) && (int) $item === $category->term_id) {
                    return true;
                }
                if ($category->slug === $item) {
                    return true;
                }
            }
        }

        return false;
    }

    private static function match_post_tags($items)
    {
        $tags = get_the_tags(self::$post->ID);

        if (empty($tags)) {
            return false;
        }

        foreach ($items as $item) {
            foreach ($tags as $tag) {
                if (is_numeric($item) && (int) $item === $tag->term_id) {
                    return true;
                }
                if ($tag->slug === $item) {
                    return true;
                }
            }
        }

        return false;
    }

    private static function match_archive_term($items, $taxonomy)
    {
        $queried_object = get_queried_object();

        if (!$queried_object || !isset($queried_object->term_id)) {
            return false;
        }

        foreach ($items as $item) {
            if (is_numeric($item) && (int) $item === $queried_object->term_id) {
                return true;
            }
            if ($queried_object->slug === $item) {
                return true;
            }
        }

        return false;
    }

    private static function match_author_archive($items)
    {
        $queried_object = get_queried_object();

        if (!$queried_object || !isset($queried_object->ID)) {
            return false;
        }

        foreach ($items as $item) {
            if (is_numeric($item) && (int) $item === $queried_object->ID) {
                return true;
            }
            if ($queried_object->user_nicename === $item) {
                return true;
            }
        }

        return false;
    }
}
