<?php
/*
 * This class will run only on Admin section
 */
namespace UmpDMA\Admin;

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

    /**
     * @param array
		 * @param object
     * @return none
     */
    public function __construct( $settings=[], $viewObject=null )
    {
        $this->addOnSettings 		= $settings;
				$this->view 						= $viewObject;
        // admin settings page
        add_filter( 'ihc_magic_feature_list', [ $this, 'addMagicFeatureItem' ], 1, 1 );
        // settings page
        add_action( 'ump_print_admin_page', [ $this, 'printSettingsPage' ], 1, 1 );
        // style & js stuff
        add_action( 'admin_enqueue_scripts', [ $this, 'styleAndScripts' ] );

				//extra actions on Admin settings page
				add_action( 'ump_addon_action_before_print_admin_settings', [ $this, 'beforePrintSettings' ] );
    }

    /**
		 * @param array
		 * @return array
		 */
    public function addMagicFeatureItem( $items=[] )
    {
        $items[ $this->addOnSettings['slug'] ] = array(
                'label'						=> esc_html__( $this->addOnSettings['name'], 'delete-my-account-for-ultimate-membership-pro' ),
                'link' 						=> (IHCACTIVATEDMODE) ? admin_url('admin.php?page=ihc_manage&tab=' . $this->addOnSettings['slug'] ) : '',
                'icon'						=> 'fas fa-user-slash',  //'fas fa-' . $this->addOnSettings['slug'] . '-ihc',
                'extra_class' 		=> 'ihc-extra-extension-box iump-' . $this->addOnSettings['slug'] . '-box',
                'description'			=> esc_html__('Membership users may delete their accounts from user My Account page', 'delete-my-account-for-ultimate-membership-pro'),
                'enabled'					=> ihc_is_magic_feat_active( $this->addOnSettings['slug'] ),
        );

        return $items;
    }

    /**
		 * @param string
		 * @return none
		 */
    public function printSettingsPage( $tab='' )
    {
        if ( $tab != $this->addOnSettings['slug'] ){
            return;
        }
				do_action( 'ump_addon_action_before_print_admin_settings' );

        ihc_save_update_metas( $this->addOnSettings['slug'] );//save update metas
        $data = ihc_return_meta_arr( $this->addOnSettings['slug'] );
				$data['plugin_slug'] = $this->addOnSettings['slug'];
				$data['lang'] = $this->addOnSettings['slug'];
				$data['name'] = $this->addOnSettings['name'];
				$data['description'] = $this->addOnSettings['description'];

        $string = $this->view->setTemplate( $this->addOnSettings['dir_path'] . 'views/admin.php' )
                  			->setContentData( $data )
                  			->getOutput();
				$allowedHtml = [
									'a' 			=> [
																	'href'		=> [],
																	'target'	=> [],
									],
							    'label' 	=> [ 'class' => [] ],
							    'div' 		=> [ 'class' => [] ],
									'h3'			=> [ 'class' => [] ],
									'h2'			=> [],
									'ul'			=> [ 'class' => [] ],
									'li'			=> [],
									'p'				=> [],
									'b'				=> [],
									'input'		=> [
																	'class' 			=> [],
																	'id' 					=> [],
																	'type' 				=> [],
																	'name' 				=> [],
																	'value' 			=> [],
																	'onclick' 		=> [],
																	'checked' 		=> []
									],
									'form'		=> [
																	'action' => [],
																	'method' => []
									],
				];
				echo wp_kses( $string, $allowedHtml );

    }

    /**
		 * @param none
		 * @return none
		 */
    public function styleAndScripts()
    {
        if ( isset( $_GET['page'] ) && $_GET['page'] == 'ihc_manage' ){
						wp_enqueue_style( $this->addOnSettings['slug'] . '-admin-style', $this->addOnSettings['dir_url'] . 'assets/css/admin.css' );
        	   wp_enqueue_script( $this->addOnSettings['slug'] . '-admin-js', $this->addOnSettings['dir_url'] . 'assets/js/admin.js', [], null );
						 wp_enqueue_script( $this->addOnSettings['slug'] . '-admin-font-js', $this->addOnSettings['dir_url'] . 'assets/js/all.min.js', [], null );

						 // old way: wp_enqueue_script($this->addOnSettings['slug'] . '-admin-del-account-kit',  'https://kit.fontawesome.com/a06dcf86f8.js', [], null);
						 wp_enqueue_style( $this->addOnSettings['slug'] . '-admin-style', $this->addOnSettings['dir_url'] . 'assets/css/admin.css' );
						 wp_enqueue_style( $this->addOnSettings['slug'] . '-admin-style', $this->addOnSettings['dir_url'] . 'assets/css/all.min.css' );

				}
    }

		/**
		 * @param none
		 * @return none
		 */
		public function beforePrintSettings()
		{
				// Add your custom code and functionality here
		}

}
