<?php
/**
 * Its User Reward Log Module
 *
 * @package Vite_Reward_Lite\Modules
 */

namespace Vite_Reward_Lite\Modules;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


use Appsbd_Lite\V5\libs\Ajax_Confirm_Response;
use Appsbd_Lite\V5\libs\Ajax_Data_Response;
use Appsbd_Lite\V5\libs\AppInput;
use Vite_Reward_Lite\Core\Vite_Reward_Module_Lite;
use Vite_Reward_Lite\Models\Database\Mapbd_Reward_Badge;
use Vite_Reward_Lite\Models\Database\Mapbd_Reward_Log;

/**
 * Class User_Reward
 *
 * @package Vite_Reward_Lite\Modules
 */
class Vite_User_Reward_Log extends Vite_Reward_Module_Lite {

	/**
	 * The initialize is generated by appsbd
	 */
	public function initialize() {
	}

	/**
	 * The on init is generated by appsbd
	 */
	public function on_init() {
		parent::on_init();
		$this->add_ajax_action( 'log-details', array( $this, 'log_details' ) );
		$this->add_ajax_action( 'user-details', array( $this, 'user_details' ) );
		$this->add_ajax_action( 'data', array( $this, 'data' ) );
	}

	/**
	 * The data is generated by appsbd
	 */
	public function data() {
		$response = new Ajax_Data_Response();
		$id       = AppInput::post_value( 'id' );
		if ( ! empty( $id ) ) {
			$user_id = $id;
		} else {
			$user_id = get_current_user_id();
		}

		if ( empty( $user_id ) ) {
			$this->add_error( 'Invalid request' );

			return;
		}
		$logs = new Mapbd_Reward_Log();
		$logs->user_id( $user_id );
		$records = $logs->count_all();
		if ( $records > 0 ) {
			$response->set_grid_records( $records );
			$result = $logs->select_all_grid_data(
				'',
				'id',
				'DESC',
				$response->limit,
				$response->limit_start()
			);
			foreach ( $result as $data ) {
				$data->entry_date = appsbd_get_wp_datetime_with_format( $data->entry_date );
				if ( 'U' == $data->ref_type ) {
					$data->u_name = apbd_get_user_title_by_user_id( $data->ref_val );
				}
				if ( 'B' == $data->ref_type ) {
					$data->b_name = Mapbd_Reward_Badge::get_badge_name_by_id( $data->ref_val );
				}
			}
			$response->set_grid_data( $result );
		}
		$response->display_grid_response();
	}

	/**
	 * The log details is generated by appsbd
	 */
	public function log_details() {
		$response = new Ajax_Confirm_Response();
		$id       = AppInput::post_value( 'id' );
		if ( empty( $id ) ) {
			$this->add_error( 'Invalid Request' );
			$response->display_with_response( false );

			return;
		}
		$log_info = new Mapbd_Reward_Log();
		$log_info->id( $id );
		if ( $log_info->select() ) {
			$response->display_with_response( true, $log_info );
		} else {
			$this->add_error( 'No log found with this id' );
			$response->display_with_response( false, null );
		}
	}

	/**
	 * The user details is generated by appsbd
	 */
	public function user_details() {
		$response = new Ajax_Confirm_Response();
		$wp_user  = wp_get_current_user();
		if ( empty( $wp_user ) ) {
			$response->display_with_response( false );

			return;
		}
		$logs = Mapbd_Reward_Log::last_five_logs( $wp_user->ID );
		$bdg  = new Mapbd_Reward_Badge();
		$bdg->status( 'A' );
		$badges     = $bdg->select_all( 'id,name,threshold_points,is_default,img_id', 'threshold_points', 'ASC' );
		$default_bdg = new Mapbd_Reward_Badge();
		$default_bdg->is_default( 'Y' );
		$is_default_badge     = $default_bdg->select( 'threshold_points' );
		$default_badge_points = $is_default_badge ? $default_bdg->threshold_points : 0;
		$badge_data           = array();
		if ( ! empty( $badges ) ) {
			foreach ( $badges as $badge ) {
				if ( $badge->threshold_points >= $default_badge_points ) {
					$data                   = new \stdClass();
					$data->id               = $badge->id;
					$data->name             = $badge->name;
					$data->threshold_points = $badge->threshold_points;
					$data->img_url          = wp_get_attachment_url( $badge->img_id );
					$badge_data[]           = $data;
				}
			}
		}
		$user       = new \stdClass();
		$user->name = $wp_user->first_name ? $wp_user->first_name . ' ' . $wp_user->last_name : $wp_user->display_name;

		$user->create_date         = gmdate( 'Y', strtotime( $wp_user->user_registered ) );
		$user->total_points        = Mapbd_Reward_Log::get_current_points( $wp_user->ID );
		$user->total_earned_points = get_user_meta( $wp_user->ID, 'apbd_reward_in', true );
		if ( ! empty( $user->total_earned_points ) ) {
			$user->total_earned_points = appsbd_wc_amount( $user->total_earned_points );
		}
		$user->total_redeem = get_user_meta( $wp_user->ID, 'apbd_reward_out', true );
		if ( ! empty( $user->total_redeem ) ) {
			$user->total_redeem = appsbd_wc_amount( $user->total_redeem );
		}
		$user->img    = get_avatar_url( $wp_user->ID );
		$user->logs   = $logs;
		$user->badges = $badge_data;

		$response->display_with_response( true, $user );
	}
}
