<?php

namespace fluXtore\Core\Settings;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
        exit;
}

if ( ! class_exists( 'fluXtore_Facebook_Pixel_Settings' ) ) {
        class fluXtore_Facebook_Pixel_Settings {
                const OPTION_PIXEL_ID = 'facebook_pixel_id';
                const OPTION_PAGE_VIEW = 'facebook_pixel_track_page_view';
                const OPTION_INITIATE_CHECKOUT = 'facebook_pixel_track_initiate_checkout';
                const OPTION_PURCHASE = 'facebook_pixel_track_purchase';
                const OPTION_ORDER_BUMP = 'facebook_pixel_track_order_bump';
                const OPTION_UPSELL_ACCEPT = 'facebook_pixel_track_upsell_accept';
                const OPTION_UPSELL_DECLINE = 'facebook_pixel_track_upsell_decline';

                /**
                 * @var self|null $instance
                 */
                private static $instance = null;

                private function __construct() {}

                public static function init() {
                        if ( null !== self::$instance ) {
                                return;
                        }

                        self::$instance = new self();
                        add_action( 'fluxtore_register_options', [ self::$instance, 'add_options' ] );
                }

                public function add_options() {
                        fluXtore_Settings::register_option( 'tracking', self::OPTION_PIXEL_ID, [
                                'type'    => 'text',
                                'label'   => 'Facebook Pixel ID',
                                'section' => 'facebook_pixel',
                        ] );

                        fluXtore_Settings::register_option( 'tracking', self::OPTION_PAGE_VIEW, [
                                'type'    => 'bool',
                                'label'   => 'Track PageView event',
                                'section' => 'facebook_pixel',
                                'default' => $this->get_bool_default( self::OPTION_PAGE_VIEW, true ),
                        ] );

                        fluXtore_Settings::register_option( 'tracking', self::OPTION_INITIATE_CHECKOUT, [
                                'type'    => 'bool',
                                'label'   => 'Track InitiateCheckout event',
                                'section' => 'facebook_pixel',
                                'default' => $this->get_bool_default( self::OPTION_INITIATE_CHECKOUT, true ),
                        ] );

                        fluXtore_Settings::register_option( 'tracking', self::OPTION_PURCHASE, [
                                'type'    => 'bool',
                                'label'   => 'Track Purchase event',
                                'section' => 'facebook_pixel',
                                'default' => $this->get_bool_default( self::OPTION_PURCHASE, true ),
                        ] );

                        fluXtore_Settings::register_option( 'tracking', self::OPTION_ORDER_BUMP, [
                                'type'    => 'bool',
                                'label'   => 'Track Order Bump toggles (AddToCart)',
                                'section' => 'facebook_pixel',
                                'default' => $this->get_bool_default( self::OPTION_ORDER_BUMP, true ),
                        ] );

                        fluXtore_Settings::register_option( 'tracking', self::OPTION_UPSELL_ACCEPT, [
                                'type'    => 'bool',
                                'label'   => 'Track Upsell acceptance (custom event)',
                                'section' => 'facebook_pixel',
                                'default' => $this->get_bool_default( self::OPTION_UPSELL_ACCEPT, true ),
                        ] );

                        fluXtore_Settings::register_option( 'tracking', self::OPTION_UPSELL_DECLINE, [
                                'type'    => 'bool',
                                'label'   => 'Track Upsell decline (custom event)',
                                'section' => 'facebook_pixel',
                                'default' => $this->get_bool_default( self::OPTION_UPSELL_DECLINE, true ),
                        ] );
                }

                private function get_bool_default( $option, $default = false ) {
                        $raw = fluXtore_Settings::get( $option, $default ? '1' : '0' );

                        if ( is_bool( $raw ) ) {
                                return $raw;
                        }

                        if ( is_numeric( $raw ) ) {
                                return (bool) intval( $raw );
                        }

                        if ( is_string( $raw ) ) {
                                $raw = trim( $raw );
                                if ( '' === $raw ) {
                                        return (bool) $default;
                                }

                                $filtered = filter_var( $raw, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
                                if ( null !== $filtered ) {
                                        return $filtered;
                                }
                        }

                        return (bool) $default;
                }
        }
}

fluXtore_Facebook_Pixel_Settings::init();
