<?php
/**
 * Main Function Goes
 *
 */

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


/**
 * Get Options Fixes
 * @param $db_field string
 * @param $default string
 */
function hpw_display($db_field, $default){
  $get_option = get_option($db_field);
  if(empty($get_option)){
    $get_option = $default;
  }
  return $get_option;
}

/**
 * Returns empty string
 */
function hpw_return_empty( $value ) {
  return '';
}

/**
 * Returns Null
 */
function hpw_return_null( $value ) {
  return NULL;
}

/**
 * remove shipping method price from cart
 */
function hpw_remove_shipping_method( $label, $method) {
  return $method->label;
}

/**
 * Remove total from thankyou page
 * 
 * @return array $total_rows
 */
function hpw_remove_thankyoupage_total( $total_rows ) {
  unset( $total_rows[ 'cart_subtotal' ] );
  return $total_rows;
}



/**
 * Check if user approve or not
 * @param $admin_check to check if user is admin
 * @return bool $is_user_admin True if the user is admin, false otherwise
 */
function hpw_user_admin_check($admin_check) {
  $current_user = wp_get_current_user();
  
  if(!$admin_check){
    $user_id = $current_user->ID;
    $key = 'pw_user_status';
    $single = true;
    $get_meta_status = get_user_meta( $user_id, $key, $single );
     return $get_meta_status;
  }
  if($admin_check){
    $current_user_roles = $current_user->roles;
     $is_user_admin = in_array("administrator", $current_user_roles) ? true: false;
    return $is_user_admin;
  }
}

/**
 * Add body class to a not-verified user
 * 
 * @return string "user-not-verified"
 */
function hpw_body_class_for_user( $classes ) {
  
  $get_meta_status = hpw_user_admin_check(false);
  $is_user_admin = hpw_user_admin_check(true);

  if( "approved" == $get_meta_status ){
    $classes[] = "user-verified";
  }elseif("pending" == $get_meta_status || "denied" == $get_meta_status ){
     $classes[] = "user-not-verified";
  } else {
    if($is_user_admin){
      $classes[] = "user-admin";
    } else {
      $classes[] = "user-guest";
    }
  }
  return $classes;
}


/**
 * Returns Text String
 */
function hpw_return_login_text($value) {
  $user_role = hpw_body_class_for_user("")[0];
  
  if($user_role == "user-not-verified" || $user_role == "denied"){
    return '<div class="hpw-needs-verification"><a href="' . hpw_display('hpw_option_3', get_permalink(woocommerce_get_page_id('myaccount'))) . '">'.hpw_display('hpw_option_3', 'Waiting for verification').'</a></div>';
  }
  return '<div class="hpw-login-to-reveal"><a href="' . hpw_display('hpw_option_2', get_permalink(woocommerce_get_page_id('myaccount'))) . '">'.hpw_display('hpw_option_1', 'Login to see pricing').'</a></div>';
}

/**
 * Echo Login Text
 */
function hpw_echo_login_text() {
  $user_role = hpw_body_class_for_user("")[0];
  // return $value . "yehey";
  if($user_role == "user-not-verified" || $user_role == "denied"){
    echo '<div class="hpw-waiting-for-verification"><a class="checkout-button button alt" href="' . hpw_display('hpw_option_3', get_permalink(woocommerce_get_page_id('myaccount'))) . '">'.hpw_display('hpw_option_3', 'Waiting for verification').'</a></div>';
  } else {
    echo '<div class="hpw-login-to-reveal"><a class="checkout-button button alt" href="' . hpw_display('hpw_option_2', get_permalink(woocommerce_get_page_id('myaccount'))) . '">'.hpw_display('hpw_option_1', 'Login to see pricing').'</a></div>';
  }  
}
 
/**
 * Theme Setup Init Code
 * @return string
 */
function hpw_show_price_when_logged($price){
  if ( class_exists( 'WooCommerce' ) ) {

    $get_meta_status = hpw_user_admin_check(false);

    // var_dump(hpw_user_admin_check(true));

      // Add body class for verified users
    add_filter('body_class', 'hpw_body_class_for_user');

    // Only execute if not admin user
    if(!hpw_user_admin_check(true)){

      // This will only work for `New User Approve` plugin
      // @see http://wordpress.org/support/plugin/new-user-approve
      if ( ! is_user_logged_in() || "approved" != $get_meta_status){  

        remove_action( 'woocommerce_view_order', 'woocommerce_order_details_table', 10 );
        remove_action( 'woocommerce_thankyou', 'woocommerce_order_details_table', 10 );
        add_filter( 'woocommerce_cart_totals_taxes_total_html', 'hpw_return_empty', 10, 1 );
        add_filter( 'woocommerce_cart_totals_order_total_html', 'hpw_return_empty', 10, 1 );
        add_filter( 'woocommerce_cart_totals_coupon_html', 'hpw_return_empty', 10 );
        add_filter( 'woocommerce_cart_totals_fee_html', 'hpw_return_empty', 10);
        add_filter( 'woocommerce_cart_shipping_method_full_label', 'hpw_remove_shipping_method', 10, 2 );
        // remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
        add_filter( 'woocommerce_cart_item_price', 'hpw_return_null', 10);
        add_filter( 'woocommerce_cart_item_subtotal', 'hpw_return_null', 10);
        add_filter( 'woocommerce_cart_item_price', 'hpw_return_null', 10, 1 );
        add_filter( 'woocommerce_cart_subtotal', 'hpw_return_empty', 10, 1 );
        add_filter( 'woocommerce_get_formatted_order_total', 'hpw_return_null', 10);
        add_filter( 'woocommerce_get_order_item_totals', 'hpw_remove_thankyoupage_total', 10, 1 );
        remove_action( 'woocommerce_thankyou_order_received_text', 'woocommerce_order_details_table', 10 );
        remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
        // remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
        add_filter( 'woocommerce_get_price_html', 'hpw_return_login_text', 10 );
        add_filter( 'woocommerce_cart_contents_total', 'hpw_return_empty', 10, 1 );
        add_filter( 'woocommerce_get_variation_price_html', 'hpw_return_empty', 10);
        add_filter( 'woocommerce_order_formatted_line_subtotal', 'hpw_return_empty', 10 );  

        add_action( 'woocommerce_proceed_to_checkout', 'hpw_echo_login_text', 10, 1);
    //    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
        // remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
        // remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        // remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    
      }
    }
  }
}

add_filter('init', 'hpw_show_price_when_logged');

/**
 * Theme Setup Init Code
 * @return void
 */
function hpw_set_after_theme_setup(){
    add_filter('woocommerce_get_price_html', 'hpw_show_price_when_logged');
}

// add_action('after_setup_theme','hpw_set_after_theme_setup') ; 


