<?php
/**
 * Reward Badge 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;

/**
 * Class Reward_Badge
 *
 * @package Vite_Reward_Lite\Modules
 */
class Vite_Reward_Badge extends Vite_Reward_Module_Lite {


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

	/**
	 * The on init is generated by appsbd
	 */
	public function on_init() {
		parent::on_init();
		$this->add_admin_ajax_action( 'add-badge', array( $this, 'add_badge' ) );
		$this->add_admin_ajax_action( 'get-badge-details', array( $this, 'get_badge_details' ) );
		$this->add_admin_ajax_action( 'update-badge', array( $this, 'update_badge' ) );
		$this->add_admin_ajax_action( 'delete-badge', array( $this, 'delete_badge' ) );
		$this->add_admin_ajax_action( 'change-default', array( $this, 'change_default' ) );
		$this->add_admin_ajax_action( 'change-status', array( $this, 'change_status' ) );
	}


	/**
	 * The data is generated by appsbd
	 */
	public function data() {
		$response = new Ajax_Data_Response();
		$mainobj  = new Mapbd_Reward_Badge();
		$records  = $mainobj->count_all();
		if ( $response->set_grid_records( $records ) ) {
			$data = $mainobj->select_all_grid_data( '', 'threshold_points', 'ASC' );
			$response->set_grid_data( $data );
		}
		$response->display_grid_response();
	}

	/**
	 * The add badge is generated by appsbd
	 */
	public function add_badge() {
		$response = new Ajax_Confirm_Response();
		if ( APPSBD_IS_POST_BACK ) {
			$uobject = new Mapbd_Reward_Badge();
			if ( $uobject->set_from_post_data( true ) ) {
				if ( $uobject->save() ) {
					$this->add_info( 'Successfully added' );
					$response->display_with_response( true );

					return;
				}
			}
		}
		$response->display_with_response( false );
	}


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

			return;
		}
		$details = new Mapbd_Reward_Badge();
		$details->id( $badge_id );
		if ( $details->select() ) {
			$details->img_url = wp_get_attachment_image_url( $details->img_id );
			$response->display_with_response( true, $details );
		} else {
			$this->add_error( 'No Badge found with this id' );
			$response->display_with_response( false, null );
		}
	}


	/**
	 * The update badge details is generated by appsbd
	 */
	public function update_badge() {
		$response = new Ajax_Confirm_Response();
		$id       = AppInput::post_value( 'id' );
		if ( empty( $id ) ) {
			$this->add_error( 'invalid update request' );
			$response->display_with_response( false );

			return;
		}
		if ( APPSBD_IS_POST_BACK ) {
			$uobject = new Mapbd_Reward_Badge();
			if ( $uobject->set_from_post_data( false ) ) {
				$props = 'name,description,threshold_points,welcome_points,cal_val,cal_type,is_additional,img_id,status';
				$uobject->unset_all_excepts( $props );
				$uobject->set_where_update( 'id', $id );
				if ( $uobject->update() ) {
					$this->add_info( 'Successfully updated' );
					$response->display_with_response( true );

					return;
				}
			}
		}
		$response->display_with_response( false );
	}


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

			return;
		}
		$uobject = new Mapbd_Reward_Badge();
		$uobject->id( $id );
		if ( $uobject->select() ) {
			if ( $uobject->delete_by_id( $id ) ) {
				$this->add_info( 'Successfully Deleted' );
				$response->display_with_response( true );
			} else {
				$this->add_error( 'Delete failed' );
				$response->display_with_response( false );
			}
		}
	}

	/**
	 * The change default is generated by appsbd
	 */
	public function change_default() {
		$response = new Ajax_Confirm_Response();
		$param    = AppInput::post_value( 'id' );
		if ( empty( $param ) ) {
			$this->add_error( 'invalid update request ' );
			$response->display_with_response( false );

			return;
		}

		$mr = new Mapbd_Reward_Badge();
		$mr->id( $param );
		if ( $mr->select( 'is_default' ) ) {
			$new_status = ( 'Y' == $mr->is_default ? 'N' : 'Y' );
			$up         = new Mapbd_Reward_Badge();
			$up->is_default( 'N' );
			$up->set_where_update( 'is_default', 'Y' );
			$up->update( true );

			if ( 'N' == $new_status ) {
				$this->add_info( 'Successfully Updated' );
				$response->display_with_response( true );
			} else {
				$uo = new Mapbd_Reward_Badge();
				$uo->is_default( $new_status );
				$uo->set_where_update( 'id', $param );
				if ( $uo->update() ) {
					$this->add_info( 'Successfully Updated' );
					$response->display_with_response( true );
				} else {
					$this->add_error( 'Update failed try again' );
					$response->display_with_response( false );
				}
			}
		}
	}

	/**
	 * The change status is generated by appsbd
	 */
	public function change_status() {
		$response = new Ajax_Confirm_Response();
		$id       = AppInput::post_value( 'id' );
		if ( empty( $id ) ) {
			$this->add_error( 'Invalid update request' );
			$response->display_with_response( false );

			return;
		}
		if ( APPSBD_IS_POST_BACK ) {
			$uobject = new Mapbd_Reward_Badge();
			if ( $uobject->set_from_post_data( false ) ) {
				$props = 'status';
				$uobject->unset_all_excepts( $props );
				$uobject->set_where_update( 'id', $id );
				if ( $uobject->update() ) {
					$this->add_info( 'Successfully status changed' );
					$response->display_with_response( true );

					return;
				}
			}
		}
		$response->display_with_response( false );
	}
}
