<?php

defined( 'ABSPATH' ) or exit;

function pwlpar_program_name() {
    $program_name = get_option( 'pwlpar_program_name', __( 'Rewards', 'pw-loyalty-points-and-rewards-for-woocommerce' ) );
    return apply_filters( 'pwlpar_program_name', $program_name );
}

function pwlpar_reward_points() {
    $reward_points = absint( get_option( 'pwlpar_reward_points', '10' ) );
    return apply_filters( 'pwlpar_reward_points', $reward_points );
}

function pwlpar_reward_value() {
    $reward_value = (float) get_option( 'pwlpar_reward_value', '1');
    return apply_filters( 'pwlpar_reward_value', $reward_value );
}

function pwlpar_is_active() {
    $is_active = ( 'yes' === get_option( 'pwlpar_is_active', 'no' ) );
    return apply_filters( 'pwlpar_is_active', $is_active );
}

function pwlpar_is_active_or_admin() {
    $is_active = pwlpar_is_active();

    if ( !$is_active && current_user_can( apply_filters( 'pwlpar_is_active_capability', PWLPAR_REQUIRES_PRIVILEGE ) ) ) {
        $is_active = true;
    }

    return apply_filters( 'pwlpar_is_active_or_admin', $is_active );
}

function pwlpar_logo_url() {
    $logo = get_option( 'pwlpar_logo_url', pwlpar_logo_url_default() );
    return apply_filters( 'pwlpar_logo_url', $logo );
}

function pwlpar_logo_url_default() {
    $url = pwlpar_default_logo_url( wc_light_or_dark( pwlpar_primary_color_default(), PWLPAR_DEFAULT_LOGO_INDEX_DARK, PWLPAR_DEFAULT_LOGO_INDEX_LIGHT ) );
    return apply_filters( 'pwlpar_logo_url_default', $url );
}

function pwlpar_offer_logo_url_default() {
    $url = pwlpar_default_logo_url( wc_light_or_dark( pwlpar_background_color(), PWLPAR_DEFAULT_OFFER_LOGO_INDEX_DARK, PWLPAR_DEFAULT_OFFER_LOGO_INDEX_LIGHT ) );
    return apply_filters( 'pwlpar_offer_logo_url_default', $url );
}

function pwlpar_earn_logo_url_default() {
    $url = pwlpar_default_logo_url( wc_light_or_dark( pwlpar_background_color(), PWLPAR_DEFAULT_EARN_LOGO_INDEX_DARK, PWLPAR_DEFAULT_EARN_LOGO_INDEX_LIGHT ) );
    return apply_filters( 'pwlpar_offer_logo_url_default', $url );
}

function pwlpar_checkout_join_cta() {
    $value = get_option( 'pwlpar_checkout_join_cta', __( "Sign up for our free program and <strong>earn 1 Point for every $1 spent</strong>, receive exclusive offers, and more. Start earning today!\n\nThis order will earn <strong>{cart_points}</strong> points.", 'pw-loyalty-points-and-rewards-for-woocommerce' ) );
    return apply_filters( 'pwlpar_checkout_join_cta', $value );
}

function pwlpar_primary_color() {
    $primary_color = get_option( 'pwlpar_primary_color', pwlpar_primary_color_default() );
    return apply_filters( 'pwlpar_primary_color', $primary_color );
}

function pwlpar_primary_color_default() {
    $primary_color_default = get_option( 'woocommerce_email_base_color' );
    return apply_filters( 'pwlpar_primary_color_default', $primary_color_default );
}

function pwlpar_secondary_color() {
    $secondary_color = get_option( 'pwlpar_secondary_color', pwlpar_secondary_color_default() );
    return apply_filters( 'pwlpar_secondary_color', $secondary_color );
}

function pwlpar_secondary_color_default() {
    $secondary_color_default = get_option( 'woocommerce_email_background_color' );
    return apply_filters( 'pwlpar_secondary_color_default', $secondary_color_default );
}

function pwlpar_background_color() {
    $background_color = get_option( 'pwlpar_background_color', pwlpar_background_color_default() );
    return apply_filters( 'pwlpar_background_color', $background_color );
}

function pwlpar_background_color_default() {
    $background_color_default = get_option( 'woocommerce_email_body_background_color' );
    return apply_filters( 'pwlpar_background_color_default', $background_color_default );
}

function pwlpar_text_color() {
    $text_color = get_option( 'pwlpar_text_color', pwlpar_text_color_default() );
    return apply_filters( 'pwlpar_text_color', $text_color );
}

function pwlpar_text_color_default() {
    $text_color_default = get_option( 'woocommerce_email_text_color' );
    return apply_filters( 'pwlpar_text_color_default', $text_color_default );
}

function pwlpar_earning_includes_tax() {
    $earning_includes_tax = ( 'yes' === get_option( 'pwlpar_earning_includes_tax', 'no' ) );
    return apply_filters( 'pwlpar_earning_includes_tax', $earning_includes_tax );
}

function pwlpar_earning_includes_shipping() {
    $earning_includes_shipping = ( 'yes' === get_option( 'pwlpar_earning_includes_shipping', 'no' ) );
    return apply_filters( 'pwlpar_earning_includes_shipping', $earning_includes_shipping );
}

function pwlpar_earning_excludes_coupons() {
    $earning_excludes_coupons = ( 'yes' === get_option( 'pwlpar_earning_excludes_coupons', 'yes' ) );
    return apply_filters( 'pwlpar_earning_excludes_coupons', $earning_excludes_coupons );
}

function pwlpar_earning_excludes_on_sale() {
    $earning_excludes_on_sale = ( 'yes' === get_option( 'pwlpar_earning_excludes_on_sale', 'no' ) );
    return apply_filters( 'pwlpar_earning_excludes_on_sale', $earning_excludes_on_sale );
}

function pwlpar_rounding_method() {
    $rounding_method = get_option( 'pwlpar_rounding_method', 'round' );
    return apply_filters( 'pwlpar_rounding_method', $rounding_method );
}

function pwlpar_free_label() {
    $free_label = __( 'free', 'pw-loyalty-points-and-rewards-for-woocommerce' );
    return apply_filters( 'pwlpar_free_label', $free_label );
}

function pwlpar_coupon_prefix() {
    return apply_filters( 'pwlpar_coupon_prefix', 'reward-' );
}

function pwlpar_default_logos() {
    $default_logos = array(
        pwlpar_relative_url( '/assets/images/rewards-logo-01-light.png' )   => __( 'Gifts (Light)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        pwlpar_relative_url( '/assets/images/rewards-logo-01-dark.png' )    => __( 'Gifts (Dark)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),

        pwlpar_relative_url( '/assets/images/rewards-logo-02-light.png' )   => __( 'Coin Bank (Light)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        pwlpar_relative_url( '/assets/images/rewards-logo-02-dark.png' )    => __( 'Coin Bank (Dark)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),

        pwlpar_relative_url( '/assets/images/rewards-logo-03-light.png' )   => __( 'Store (Light)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        pwlpar_relative_url( '/assets/images/rewards-logo-03-dark.png' )    => __( 'Store (Dark)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),

        pwlpar_relative_url( '/assets/images/rewards-logo-04-light.png' )   => __( 'Gem (Light)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        pwlpar_relative_url( '/assets/images/rewards-logo-04-dark.png' )    => __( 'Gem (Dark)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),

        pwlpar_relative_url( '/assets/images/rewards-logo-05-light.png' )   => __( 'Heart (Light)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        pwlpar_relative_url( '/assets/images/rewards-logo-05-dark.png' )    => __( 'Heart (Dark)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),

        pwlpar_relative_url( '/assets/images/rewards-logo-06-light.png' )   => __( 'Hands Heart (Light)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        pwlpar_relative_url( '/assets/images/rewards-logo-06-dark.png' )    => __( 'Hands Heart (Dark)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),

        pwlpar_relative_url( '/assets/images/rewards-logo-07-light.png' )   => __( 'Hand Holding Heart (Light)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        pwlpar_relative_url( '/assets/images/rewards-logo-07-dark.png' )    => __( 'Hand Hold Heart (Dark)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),

        pwlpar_relative_url( '/assets/images/rewards-logo-08-light.png' )   => __( 'Heart Outline (Light)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        pwlpar_relative_url( '/assets/images/rewards-logo-08-dark.png' )    => __( 'Heart Outline (Dark)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),

        pwlpar_relative_url( '/assets/images/rewards-logo-09-light.png' )   => __( 'Heart Circle (Light)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        pwlpar_relative_url( '/assets/images/rewards-logo-09-dark.png' )    => __( 'Heart Circle (Dark)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),

        pwlpar_relative_url( '/assets/images/rewards-logo-10-light.png' )   => __( 'Coins (Light)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        pwlpar_relative_url( '/assets/images/rewards-logo-10-dark.png' )    => __( 'Coins (Dark)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),

        pwlpar_relative_url( '/assets/images/rewards-logo-11-light.png' )   => __( 'Trophy (Light)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        pwlpar_relative_url( '/assets/images/rewards-logo-11-dark.png' )    => __( 'Trophy (Dark)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),

        pwlpar_relative_url( '/assets/images/rewards-logo-12-light.png' )   => __( 'Award (Light)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
        pwlpar_relative_url( '/assets/images/rewards-logo-12-dark.png' )    => __( 'Award (Dark)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
    );

    // sort by array values
    asort( $default_logos );

    return apply_filters( 'pwlpar_default_logos', $default_logos );
}

function pwlpar_default_logo_url( $index = 0 ) {
    $default_logo_url = '';

    $logos = pwlpar_default_logos();
    $urls = array_keys( $logos );

    if ( isset( $urls[ $index ] ) ) {
        $default_logo_url = $urls[ $index ];
    }

    return apply_filters( 'pwlpar_default_logo_url', $default_logo_url, $index );
}

function pwlpar_signup_bonus_points() {
    $signup_bonus_points = absint( get_option( 'pwlpar_signup_bonus_points', pwlpar_reward_points() * 100 ) );
    return apply_filters( 'pwlpar_signup_bonus_points', $signup_bonus_points );
}

function pwlpar_signup_bonus_transaction_note() {
    $signup_bonus_transaction_note = apply_filters( 'pwlpar_signup_bonus_transaction_note', __( 'Sign up bonus', 'pw-loyalty-points-and-rewards-for-woocommerce' ) );
    return apply_filters( 'pwlpar_signup_bonus_transaction_note', $signup_bonus_transaction_note );
}


function pwlpar_get_join_page_id() {
    return absint( get_option( 'pwlpar_join_page_id' ) );
}

function pwlpar_get_home_page_id() {
    return absint( get_option( 'pwlpar_home_page_id' ) );
}

function pwlpar_get_join_page() {
    $join_page = null;

    $join_page_id = pwlpar_get_join_page_id();
    if ( !empty( $join_page_id ) ) {
        $join_page = get_post( $join_page_id );
    }

    return apply_filters( 'pwlpar_get_join_page', $join_page );
}

function pwlpar_get_home_page() {
    $home_page = null;

    $home_page_id = pwlpar_get_home_page_id();
    if ( !empty( $home_page_id ) ) {
        $home_page = get_post( $home_page_id );
    }

    return apply_filters( 'pwlpar_get_home_page', $home_page );
}
