<?php

if ( !defined( 'ABSPATH' ) ) {
    die;
}
require_once dirname( __FILE__ ) . '/vendor/autoload.php';
class GOAuthAdmin
{
    protected  $goauth_group ;
    function __construct()
    {
        $this->goauth_group = 'goauth_group';
        $this->render_menu();
    }
    
    /* Initialize admin menu */
    function menu()
    {
        add_options_page(
            //title
            'GOAuth Settings',
            //id
            'GOAuth',
            //access level
            'administrator',
            //menu unique name
            basename( __FILE__ ),
            //callback
            array( $this, 'settings_page' )
        );
        add_action( 'admin_init', array( $this, 'register_goauth_settings' ) );
        add_action( 'admin_enqueue_scripts', array( $this, 'load_text_security' ) );
        add_action( 'admin_enqueue_scripts', array( $this, 'load_materialize' ) );
        add_action( 'admin_enqueue_scripts', array( $this, 'load_page_style' ) );
    }
    
    function load_page_style()
    {
        $screen = get_current_screen();
        
        if ( strpos( $screen->base, 'goauth-admin' ) === false ) {
            return;
        } else {
            wp_register_script( 'page_script', plugins_url( 'assets/settings_page.js', __FILE__ ) );
            wp_enqueue_script( 'page_script' );
            wp_register_style( 'page_style', plugins_url( 'assets/settings_page.css', __FILE__ ) );
            wp_enqueue_style( 'page_style' );
        }
    
    }
    
    function load_materialize()
    {
        $screen = get_current_screen();
        
        if ( strpos( $screen->base, 'goauth-admin' ) === false ) {
            return;
        } else {
            wp_register_script( 'materialize_js', plugins_url( 'assets/materialize.min.js', __FILE__ ) );
            wp_enqueue_script( 'materialize_js' );
            wp_register_style( 'materialize_css', plugins_url( 'assets/materialize.min.css', __FILE__ ) );
            wp_enqueue_style( 'materialize_css' );
        }
    
    }
    
    function load_text_security()
    {
        $screen = get_current_screen();
        
        if ( strpos( $screen->base, 'goauth-admin' ) === false ) {
            return;
        } else {
            wp_register_style( 'text_security', plugins_url( 'assets/text-security-disc.css', __FILE__ ) );
            wp_enqueue_style( 'text_security' );
        }
    
    }
    
    /* Register html elements in database */
    /* option group => html elements values in database */
    function register_goauth_settings()
    {
        register_setting( $this->goauth_group, 'goauth_enable' );
        register_setting( $this->goauth_group, 'goauth_force_login' );
        register_setting( $this->goauth_group, 'goauth_hide_loginform' );
        register_setting( $this->goauth_group, 'goauth_hide_forget' );
        register_setting( $this->goauth_group, 'goauth_xmlrpc_disabled' );
        register_setting( $this->goauth_group, 'goauth_api_disabled' );
        register_setting( $this->goauth_group, 'goauth_save_on_uninstall' );
        register_setting( $this->goauth_group, 'goauth_antilockout' );
        register_setting( $this->goauth_group, 'goauth_2fa_enable' );
        register_setting( $this->goauth_group, 'goauth_2fa_salt' );
        register_setting( $this->goauth_group, 'goauth_2fa_show_qr' );
        register_setting( $this->goauth_group, 'goauth_google_enable' );
        register_setting( $this->goauth_group, 'goauth_client_id' );
        register_setting( $this->goauth_group, 'goauth_client_secret' );
        register_setting( $this->goauth_group, 'goauth_domain' );
        register_setting( $this->goauth_group, 'goauth_button_style' );
        register_setting( $this->goauth_group, 'goauth_disable_user_enum' );
    }
    
    function show_general_settings()
    {
        
        if ( ga_fs()->is__premium_only() && ga_fs()->is_paying() ) {
            require_once dirname( __FILE__ ) . '/templates/general__premium_only.php';
        } else {
            require_once dirname( __FILE__ ) . '/templates/general.php';
        }
    
    }
    
    function show_security_settings()
    {
        
        if ( ga_fs()->is__premium_only() && ga_fs()->is_paying() ) {
            require_once dirname( __FILE__ ) . '/templates/security__premium_only.php';
        } else {
            require_once dirname( __FILE__ ) . '/templates/security.php';
        }
    
    }
    
    function show_2fa_settings()
    {
        require_once dirname( __FILE__ ) . '/templates/2fa.php';
    }
    
    function show_google_settings()
    {
        require_once dirname( __FILE__ ) . '/templates/google.php';
    }
    
    /* HTML page template */
    function settings_page()
    {
        ?>
		<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
		<div class="wrap">
			<h1><?php 
        _e( 'GOAuth Settings', 'goauth' );
        ?></h1>
			<br>
			<a id="scale-demo" href="<?php 
        echo  plugin_dir_url( __FILE__ ) . 'help.php' ;
        ?>" target="_blank" class="btn-floating btn-large"><i class="material-icons">library_books</i></a> <?php 
        _e( 'Help', 'goauth' );
        ?>
			<a id="scale-demo" href="https://wordpress.org/support/plugin/goauth" target="_blank" class="btn-floating btn-large"><i class="material-icons">feedback</i></a> <?php 
        _e( 'Support', 'goauth' );
        ?>
			<br>
			<br>
			<h2 class="nav-tab-wrapper">
				<div class="col s12">
					<ul class="tabs transparent">
    					<li class="tab"><a class="active blue-grey-text text-darken-4" href="#general"><b><?php 
        _e( 'General', 'goauth' );
        ?></b></a></li>
						<li class="tab"><a class="blue-grey-text text-darken-4" href="#security"><b><?php 
        _e( 'Security', 'goauth' );
        ?></b></a></li>
						<?php 
        
        if ( ga_fs()->is__premium_only() && ga_fs()->is_paying() ) {
            ?>
							<li class="tab"><a class="blue-grey-text text-darken-4" href="#button_style"><b><?php 
            _e( 'Button Style', 'goauth' );
            ?></b></a></li>
						<?php 
        }
        
        ?>
    					<li class="tab"><a class="blue-grey-text text-darken-4" href="#2fa"><b>2FA</b></a></li>
    					<li class="tab"><a class="blue-grey-text text-darken-4" href="#google"><b>Google</b></a></li>
  					</ul>
				</div>
			</h2>
			<form method="post" name ="options" action="options.php" class="col s12" autocomplete="off">
				<div id="general">
				<?php 
        do_settings_sections( $this->show_general_settings() );
        ?>
				</div>
				<div id="security">
				<?php 
        do_settings_sections( $this->show_security_settings() );
        ?>
				</div>
				<?php 
        
        if ( ga_fs()->is__premium_only() && ga_fs()->is_paying() ) {
            ?>
					<div id="button_style">
						<?php 
            do_settings_sections( $this->show_button_settings__premium_only() );
            ?>
					</div>
				<?php 
        }
        
        ?>
				<div id="2fa">
				<?php 
        do_settings_sections( $this->show_2fa_settings() );
        ?>
				</div>
				<div id="google">
				<?php 
        do_settings_sections( $this->show_google_settings() );
        ?>
				</div>
				<?php 
        settings_fields( $this->goauth_group );
        ?>
				<button class="btn waves-effect waves-light" type="submit" name="submit" id="submit" onclick="CheckIP(document.options.goauth_antilockout.value)"><?php 
        _e( 'Save Changes', 'goauth' );
        ?><i class="material-icons right">send</i></button>
			</form>
		</div>
<?php 
    }
    
    /* Render admin menu */
    function render_menu()
    {
        add_action(
            'admin_menu',
            //admin area hook
            array( $this, 'menu' )
        );
    }
    
    function __destruct()
    {
        
        if ( ga_fs()->is__premium_only() && ga_fs()->is_paying() ) {
            $this->update_default_role__premium_only();
            
            if ( get_option( 'goauth_recover_button_style' ) == 'on' ) {
                $this->recover_css__premium_only();
            } else {
                $this->save_button_style__premium_only();
            }
        
        }
    
    }

}
/* Initialize class */
if ( class_exists( 'GOAuthAdmin' ) ) {
    new GOAuthAdmin();
}