<?php

defined( 'ABSPATH' ) or exit;

final class PWLPAR_Dashboard {

    function __construct() {
        add_action( 'woocommerce_init', array( $this, 'woocommerce_init' ) );
    }

    function woocommerce_init() {
        add_shortcode( PWLPAR_HOME_SHORTCODE, array( $this, 'home_shortcode' ) );
        add_shortcode( PWLPAR_JOIN_SHORTCODE, array( $this, 'join_shortcode' ) );

        add_action( 'template_redirect', array( $this, 'show_join_or_home_content' ) );
        add_filter( 'woocommerce_get_query_vars', array( $this, 'woocommerce_get_query_vars' ) );
        add_action( 'woocommerce_account_rewards_endpoint', array( $this, 'woocommerce_account_rewards_endpoint' ) );
        add_action( 'woocommerce_account_menu_items', array( $this, 'woocommerce_account_menu_items' ), 10, 2 );
        add_filter( 'woocommerce_endpoint_rewards_title', array( $this, 'set_my_account_rewards_title' ), 10, 2 );
    }

    function show_join_or_home_content( $content ) {
        $home_page_id = pwlpar_get_home_page_id();
        if ( !empty( $home_page_id ) && is_page( $home_page_id ) && !pwlpar_current_user_is_rewards_member() ) {
            wp_redirect( pwlpar_get_join_url() );
            exit();
        }

        $join_page_id = pwlpar_get_join_page_id();
        if ( !empty( $join_page_id ) && is_page( $join_page_id ) && pwlpar_current_user_is_rewards_member() ) {
            wp_redirect( pwlpar_get_home_url() );
            exit();
        }
    }

    function home_shortcode( $shortcode_attributes = false, $shortcode_content = false ) {
        if ( !pwlpar_is_active_or_admin() ) {
            return __( 'Coming soon!', 'pw-loyalty-points-and-rewards-for-woocommerce' );
        }

        $rewards = pwlpar_get_rewards( array(
            'status' => 'pending',
            'user_id' => get_current_user_id(),
        ) );

        $offers = pwlpar_get_offers( array(
            'active' => true,
            'user_id' => get_current_user_id(),
        ) );

        return pwlpar_get_template_html( 'pw-rewards/home.php', array(
            'section' => 'rewards',
            'rewards' => $rewards,
            'offers' => $offers,
        ) );
    }

    function join_shortcode( $shortcode_attributes = false, $shortcode_content = false ) {
        if ( !pwlpar_is_active_or_admin() ) {
            return __( 'Coming soon!', 'pw-loyalty-points-and-rewards-for-woocommerce' );
        }

        return pwlpar_get_template_html( 'pw-rewards/join.php', array(
            /* translators: %s is the rewards program name */
            'join_button_title' => sprintf( __( 'Join %s', 'pw-loyalty-points-and-rewards-for-woocommerce' ), pwlpar_program_name() ),
            'is_rewards_member' => pwlpar_current_user_is_rewards_member(),
        ) );
    }

    function woocommerce_get_query_vars( $query_vars ) {
        $query_vars['rewards'] = get_option( 'woocommerce_myaccount_pwlpar_endpoint', 'rewards' );

        return $query_vars;
    }

    function woocommerce_account_rewards_endpoint( $tab ) {
        global $wp;

        if ( pwlpar_is_active_or_admin() ) {
            if ( pwlpar_current_user_is_rewards_member() ) {
                switch ( $tab ) {
                    case 'history':
                        pwlpar_get_template( 'pw-rewards/history.php', array(
                            'transactions' => pwlpar_get_transactions( array( 'user_id' => get_current_user_id(), 'status__not_in' => array( 'trash' ) ) ),
                        ) );
                    break;

                    default:
                        $home_page = pwlpar_get_home_page();
                        if ( !is_null( $home_page ) ) {
                            echo wp_kses_post( apply_filters( 'the_content', $home_page->post_content ) );
                        } else {
                            esc_html_e( 'Invalid Home Page. Please set a valid Home Page in the plugin settings.', 'pw-loyalty-points-and-rewards-for-woocommerce' );
                        }
                    break;
                }
            } else {
                $join_page = pwlpar_get_join_page();
                if ( !is_null( $join_page ) ) {
                    echo wp_kses_post( apply_filters( 'the_content', $join_page->post_content ) );
                } else {
                    esc_html_e( 'Invalid Join Page. Please set a valid Join Page in the plugin settings.', 'pw-loyalty-points-and-rewards-for-woocommerce' );
                }
            }
        }
    }

    function set_my_account_rewards_title( $title, $endpoint ) {
        return apply_filters( 'pwlpar_my_account_title', pwlpar_program_name(), $endpoint );
    }

    function woocommerce_account_menu_items( $items, $endpoints ) {
        // Prevent a fatal error in case someone has changed the type of the $items array using a hook.
        if ( !is_array( $items ) ) {
            return $items;
        }

        if ( !pwlpar_is_active_or_admin() ) {
            return $items;
        }

        $endpoints['rewards'] = get_option( 'woocommerce_myaccount_pwlpar_endpoint', 'rewards' );

        if ( !empty( $endpoints['rewards'] ) ) {

            // The title that is displayed in the menu
            $page_title = apply_filters( 'pwlpar_my_account_menu_title', pwlpar_program_name() );

            // Where to place this new menu entry.
            $insert_after = apply_filters( 'pwlpar_my_account_after_menu', __( 'Orders', 'pw-loyalty-points-and-rewards-for-woocommerce' ), $items, $endpoints );

            $insert_index = 0;
            foreach( $items as $endpoint => $title ) {
                $insert_index++;
                if ( $title === $insert_after ) {
                    break;
                }
            }

            $items = array_slice( $items, 0, $insert_index, true ) + array( $endpoints['rewards'] => $page_title ) + array_slice( $items, $insert_index, NULL, true );
        }

        return $items;
    }
}

return new PWLPAR_Dashboard();
