<?php

defined( 'ABSPATH' ) or exit;

function pwlpar_heading( $args = array() ) {

    if ( !isset( $args['member'] ) || !is_a( $args['member'], 'PWLPAR_Member' ) ) {
        $args['member'] = pwlpar_get_member( get_current_user_id() );
    }

    $balance = 0;
    $is_rewards_member = false;
    if ( is_a( $args['member'], 'PWLPAR_Member' ) ) {
        $balance = $args['member']->get_balance();
        $is_rewards_member = $args['member']->get_is_enrolled();
    }

    $default_args = apply_filters( 'pwlpar_heading_default_args', array(
        'logo_url'              => pwlpar_logo_url(),
        'program_name'          => pwlpar_program_name(),
        'point_balance'         => $balance,
        'is_rewards_member'     => $is_rewards_member,
        'history_url'           => pwlpar_get_history_url(),
        'show_history_button'   => true,
    ) );

    $args = wp_parse_args( $args, $default_args );

    pwlpar_get_template( 'pw-rewards/header.php', $args );
}

function pwlpar_reward( $args = array() ) {
    if ( is_a( $args, 'PWLPAR_Reward' ) ) {
        $args = array( 'reward' => $args );
    }

    $reward = ( empty( $args['reward'] ) || !is_a( $args['reward'], 'PWLPAR_Reward' ) ) ? new PWLPAR_Reward() : $args['reward'];

    $reward = $args['reward'];

    $args = wp_parse_args( $args, array(
        'reward'                    => $reward,
        'offer_title'               => $reward->get_offer_title(),
        'offer_description'         => $reward->get_offer_description(),
        'offer_image_url'           => $reward->get_offer_image_url(),
        'apply_url'                 => $reward->get_apply_url(),
        'remove_url'                => $reward->get_remove_url(),
        'is_reward_in_cart'         => pwlpar_is_reward_in_cart( $reward ),
    ) );

    pwlpar_get_template( 'pw-rewards/reward.php', $args );
}

function pwlpar_offer( $args = array() ) {
    if ( is_a( $args, 'PWLPAR_Offer' ) ) {
        $args = array( 'offer' => $args );
    }

    $offer = ( empty( $args['offer'] ) || !is_a( $args['offer'], 'PWLPAR_Offer' ) ) ? new PWLPAR_Offer() : $args['offer'];

    $default_args = apply_filters( 'pwlpar_offer_default_args', array(
        'offer'                 => $offer,
        'offer_id'              => $offer->get_id(),
        'title'                 => $offer->get_title(),
        'description'           => $offer->get_description(),
        'points'                => $offer->get_points(),
        'image_url'             => $offer->get_image_url(),
        'claim_url'             => $offer->get_claim_url(),
        'button_title'          => $offer->get_button_title(),
        'can_claim'             => $offer->get_user_can_claim_offer(),
    ) );

    $args = wp_parse_args( $args, $default_args );

    pwlpar_get_template( 'pw-rewards/offer.php', $args );
}