<?php

defined( 'ABSPATH' ) or exit;

function pwlpara_show_setup_wizard() {
    return get_option( 'pwlpar_show_setup_wizard', 'yes' ) == 'yes';
}

function pwlpara_setup_wizard_is_complete() {
    update_option( 'pwlpar_show_setup_wizard', 'no', false );
}

function pwlpara_admin_page() {
    // If the Pimwick Plugins menu is hidden, we need to redirect to the WooCommerce menu instead.
    if ( get_option( 'hide_pimwick_menu', 'no' ) == 'no' ) {
        $page = 'pw-loyalty-points-and-rewards-for-woocommerce';
    } else {
        $page = 'wc-pw-loyalty-points-and-rewards-for-woocommerce';
    }

    return apply_filters( 'pwlpara_admin_page', $page );
}

function pwlpara_admin_url( $section = '', $args = false ) {

    $admin_url = admin_url( 'admin.php' );
    $admin_url = add_query_arg( 'page', pwlpara_admin_page(), $admin_url );

    if ( !empty( $section ) ) {
        $admin_url = add_query_arg( 'section', $section, $admin_url );
    }

    if ( !empty( $args ) && is_array( $args ) ) {
        foreach ( $args as $key => $value ) {
            $admin_url = add_query_arg( $key, $value, $admin_url );
        }
    }

    return apply_filters( 'pwlpara_admin_url', $admin_url, $section, $args );
}

function pwlpara_edit_email_url( $email_id ) {
    $admin_url = admin_url( 'admin.php' );
    $admin_url = add_query_arg( 'page', 'wc-settings', $admin_url );
    $admin_url = add_query_arg( 'tab', 'email', $admin_url );
    $admin_url = add_query_arg( 'section', 'wc_email_' . $email_id, $admin_url );

    return $admin_url;
}

function pwlpara_advanced_settings_url() {
    $admin_url = admin_url( 'admin.php' );
    $admin_url = add_query_arg( 'page', 'wc-settings', $admin_url );
    $admin_url = add_query_arg( 'tab', 'advanced', $admin_url );
    $admin_url = add_query_arg( 'section', 'pwlpar', $admin_url );

    return $admin_url;
}

function pwlpara_wc_min_version( $version ) {
    return version_compare( WC()->version, $version, ">=" );
}

function pwlpara_section_opening_tag( $section ) {
    //
    // Nonce verification not needed because parameter is only used for string comparison.
    //
    // phpcs:disable WordPress.Security.NonceVerification
    $request_section = isset( $_REQUEST['section'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['section'] ) ) : false;
    $request_id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : false;
    // phpcs:enable

    ?>
    <div id="pwlpara-section-<?php echo esc_attr( $section ); ?>" class="pwlpara-section" style="<?php esc_attr( pwlpara_section_helper( $section, 'display: block;' ) ); ?>" data-id="<?php echo ( $section == $request_section && $request_id !== false ) ? esc_attr( $request_id ) : ''; ?>">
    <?php
        //
        // Nonce verification not needed because we only look for the existence of the parameter.
        //
        // phpcs:ignore WordPress.Security.NonceVerification
        if ( isset( $_REQUEST['saved'] ) && $section == $request_section ) {
            ?>
            <div class="notice notice-success is-dismissible" style="max-width: 700px; margin-left: 0; margin-bottom: 24px;">
            <p><?php esc_html_e( 'Saved', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></p>
            </div>
            <?php
        }
}

function pwlpara_section_selected( $section ) {
    $selected = false;

    //
    // Nonce verification not needed because parameter is only used for string comparison.
    //
    // phpcs:ignore WordPress.Security.NonceVerification
    if ( isset( $_REQUEST['section'] ) ) {
        //
        // Nonce verification not needed because parameter is only used for string comparison.
        //
        // phpcs:ignore WordPress.Security.NonceVerification
        $selected = ( $_REQUEST['section'] == $section );
    } else if ( $section == 'dashboard' ) {
        $selected = true;
    }

    return $selected;
}

// Optionally set the selected CSS for the appropriate section.
function pwlpara_section_helper( $section, $output = 'pwlpara-section-header-item-selected' ) {
    if ( pwlpara_section_selected( $section ) ) {
        echo esc_attr( $output );
    }
}

function pwlpara_get_customer_count() {
    $user_query = new WP_User_Query( array(
        'fields'    => 'ID',
        'role'      => 'customer'
    ) );
    $customer_count = count( $user_query->get_results() );

    return $customer_count;
}

function pwlpara_get_welcome_offers() {
    $offers = array();

    $offers[] = new PWLPAR_Offer( array(
        /* translators: %s: price */
        'title'             => sprintf( __( 'Save %s', 'pw-loyalty-points-and-rewards-for-woocommerce' ), wp_strip_all_tags( wc_price( 1, array( 'decimals' => 0 ) ) ) ),
        'description'       => __( 'On your total purchase', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        'coupon_type'       => 'fixed_cart',
        'coupon_amount'     => 1,
        'points'            => pwlpar_reward_points() * 100,
        'menu_order'        => 0,
    ) );

    $offers[] = new PWLPAR_Offer( array(
        /* translators: %s: price */
        'title'             => sprintf( __( 'Save %s', 'pw-loyalty-points-and-rewards-for-woocommerce' ), wp_strip_all_tags( wc_price( 5, array( 'decimals' => 0 ) ) ) ),
        'description'       => __( 'On your total purchase', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        'coupon_type'       => 'fixed_cart',
        'coupon_amount'     => 5,
        'points'            => pwlpar_reward_points() * 500,
        'menu_order'        => 1,
    ) );

    $offers[] = new PWLPAR_Offer( array(
        /* translators: %s: price */
        'title'             => sprintf( __( 'Save %s', 'pw-loyalty-points-and-rewards-for-woocommerce' ), wp_strip_all_tags( wc_price( 15, array( 'decimals' => 0 ) ) ) ),
        'description'       => __( 'On your total purchase', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        'coupon_type'       => 'fixed_cart',
        'coupon_amount'     => 15,
        'points'            => pwlpar_reward_points() * 1500,
        'menu_order'        => 2,
    ) );

    return apply_filters( 'pwlpara_get_welcome_offers', $offers );
}

function pwlpara_pro_feature_notice( $source, $top = '0px', $left = '225px', $style = '', $logo = true ) {
    $pro_url = 'https://www.pimwick.com/pw-loyalty-points-and-rewards-for-woocommerce/';

    ?>
    <div class="pwlpara-pro-notice" style='<?php echo esc_attr( "top: $top; left: $left; $style" ); ?>'>
        <?php
            printf(
                /* translators: %s: link to Pro version */
                esc_html__( 'Unlock these great features and more in %s', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                '<br><a href="' . esc_attr( $pro_url ) . '" target="_blank">PW Loyalty Points and Rewards for WooCommerce Pro</a>'
            );

            if ( $logo ) {
                ?>
                <div style="margin-top: 32px;">
                    <a href="<?php echo esc_attr( $pro_url ); ?>" class="pwlpara-pro-banner-link" target="_blank"><img src="<?php echo esc_attr( pwlpar_relative_url( '/assets/images/banner-772x250.jpg' ) ); ?>" alt="PW Loyalty Points and Rewards for WooCommerce Pro" class="pwlpara-pro-banner"></a>
                </div>
                <?php
            }
        ?>
    </div>
    <?php
}