<?php
/*
 * Run on public section
 */
namespace UmpDMA;

class Main
{
		/**
		 * @var array
		 */
		private $addOnSettings				= [];
		/**
		 * @var string
		 */
		private $view 								= null;

		/**
		 * @param array
		 * @param string
		 * @return none
		 */
    public function __construct( $addOnSettings=[], $viewObject=null )
    {
				$this->addOnSettings 	= $addOnSettings;
				$this->view 					= $viewObject;

	    		// check if magic feat is active filter ...
       			 add_filter( 'ihc_is_magic_feat_active_filter', [ $this, 'isMagicFeatActive' ], 1, 2 );

				if ( !get_option( $this->addOnSettings['slug'] . '-enabled' ) ){
						return;
				}

				// css & js
				add_action( 'wp_enqueue_scripts', [ $this, 'styleAndScripts' ] );

				add_shortcode( 'ump-delete-my-account', [ $this, 'html' ] );

				add_action( 'wp_ajax_ump_dma_action', [ $this, 'doDelete' ] );
				add_action( 'wp_ajax_nopriv_ump_dma_action', [ $this, 'doDelete' ] );
    }

		/**
		 * @param none
		 * @return none
		 */
		public function styleAndScripts()
		{
				wp_enqueue_style( $this->addOnSettings['slug'] . '-public-style', $this->addOnSettings['dir_url'] . 'assets/css/public.css' );
				wp_enqueue_script( $this->addOnSettings['slug'] . '-public-js', $this->addOnSettings['dir_url'] . 'assets/js/public.js', [], null );
		}


		/**
		 * @param bool
 		 * @param string
		 * @return bool
		 */
    public function isMagicFeatActive( $isActive=false, $type='' )
    {
        if ( $this->addOnSettings['slug'] != $type ){
            return $isActive;
        }
        // check if is active ...
        $settings = ihc_return_meta_arr( $this->addOnSettings['slug'] );
        if ( !empty( $settings[ $this->addOnSettings['slug'] . '-enabled'] ) ){
            return true;
        }
        return false;
    }

		/**
 		 * @param none
		 * @return none
		 */
		public function html()
		{
				ob_start();
				global $current_user;
				if ( !isset( $current_user->ID ) ){
						return '';
				}
				$data['dir_url'] = $this->addOnSettings['dir_url'];

				$string = $this->view->setTemplate( $this->addOnSettings['dir_path'] . 'views/public.php' )
												->setContentData( $data )
												->getOutput();
				$allowedHtml = [
											    'label' 	=> [],
											    'div' 		=> [
																				'class' 					=> [],
																				'data-title' 			=> [],
																				'style' 					=> [],
																				'id'							=> [],
																				'data-n'					=> [],
																				'data-wrong_msg'	=> [],
																				'data-success_msg'=> [],
													],
													'input'		=> [
																				'class' 				=> [],
																				'type' 					=> [],
																				'name' 					=> [],
																				'value' 				=> [],
													],
				];
				echo wp_kses( $string, $allowedHtml );
				return ob_get_clean();

		}

		/**
		 * @param none
		 * @param bool
		 */
		public function doDelete()
		{
				global $current_user;
				if ( !isset( $_POST['pass'] ) || !isset( $_POST['n'] ) || !wp_verify_nonce( $_POST['n'], 'ump-dma-nonce' ) ){
						echo 0;
						die;
				}
				$uid = isset( $current_user->ID ) ? $current_user->ID : 0;
				if ( !$uid ){
						echo 0;
						die;
				}
				$hashPassword = $this->getPasswordForUid( $uid );
				if ( !wp_check_password( esc_sql( $_POST['pass'] ), $hashPassword, $uid ) ){
						echo 0;
						die;
				}

				do_action( 'ihc_delete_user_action', $uid);
				wp_delete_user( $uid );
				wp_clear_auth_cookie();
				do_action( 'wp_logout' );

				nocache_headers();
				// do delete

				$redirectTo = get_option( 'ihc_general_login_default_page' );
				if ( $redirectTo ){
						$redirectTo = get_permalink( $redirectTo );
				}
				if ( !$redirectTo ){
						$redirectTo = get_home_url();
				}

				echo json_encode( [
						'status'			=> 0,
						'redirect'		=> $redirectTo,
				] );
				die;
		}

		private function getPasswordForUid( $uid=0 )
		{
				global $wpdb;
				if (!$uid){
						return '';
				}
				$query = $wpdb->prepare( "SELECT user_pass FROM {$wpdb->users} WHERE ID=%d;", $uid );
				return $wpdb->get_var( $query );
		}

}
