<?php
/**
 * Class LSC_Admin
 * Main admin class: registers menus, settings, enqueues assets.
 */
class LSC_Admin {

    public function __construct() {
        add_action( 'admin_menu',            [ $this, 'register_menus' ] );
        add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
        add_action( 'admin_init',            [ $this, 'register_settings' ] );
    }

    /**
     * Register top-level admin menu and submenus.
     */
    public function register_menus() {
        add_menu_page(
            __( 'Live Support Chat', 'live-support-chat' ),
            __( 'Live Support', 'live-support-chat' ),
            'lsc_manage_chat',
            'live-support-chat',
            [ $this, 'page_dashboard' ],
            'dashicons-format-chat',
            30
        );

        add_submenu_page(
            'live-support-chat',
            __( 'Dashboard', 'live-support-chat' ),
            __( 'Dashboard', 'live-support-chat' ),
            'lsc_manage_chat',
            'live-support-chat',
            [ $this, 'page_dashboard' ]
        );

        add_submenu_page(
            'live-support-chat',
            __( 'Chats', 'live-support-chat' ),
            __( 'Chats', 'live-support-chat' ),
            'lsc_manage_chat',
            'lsc-chats',
            [ 'LSC_Admin_Chats', 'page_chats' ]
        );

        add_submenu_page(
            'live-support-chat',
            __( 'Chatter Users', 'live-support-chat' ),
            __( 'Chatters', 'live-support-chat' ),
            'lsc_manage_users',
            'lsc-users',
            [ 'LSC_Admin_Users', 'page_users' ]
        );

        add_submenu_page(
            'live-support-chat',
            __( 'Settings', 'live-support-chat' ),
            __( 'Settings', 'live-support-chat' ),
            'lsc_manage_chat',
            'lsc-settings',
            [ $this, 'page_settings' ]
        );

        add_submenu_page(
            'live-support-chat',
            __( 'Activity Log', 'live-support-chat' ),
            __( 'Activity Log', 'live-support-chat' ),
            'lsc_manage_chat',
            'lsc-sessions',
            [ $this, 'page_sessions' ]
        );
    }

    /**
     * Enqueue admin CSS and JS on our own pages.
     *
     * @param string $hook Current admin page hook.
     */
    public function enqueue_assets( $hook ) {
        $our_pages = [
            'toplevel_page_live-support-chat',
            'live-support_page_lsc-chats',
            'live-support_page_lsc-users',
            'live-support_page_lsc-settings',
            'live-support_page_lsc-sessions',
        ];

        if ( ! in_array( $hook, $our_pages, true ) ) {
            return;
        }

        wp_enqueue_style(
            'lsc-admin',
            LSC_PLUGIN_URL . 'assets/css/admin.css',
            [],
            LSC_VERSION
        );

        wp_enqueue_script(
            'lsc-admin',
            LSC_PLUGIN_URL . 'assets/js/admin.js',
            [ 'jquery' ],
            LSC_VERSION,
            true
        );

        wp_localize_script( 'lsc-admin', 'lscAdmin', [
            'ajaxUrl'      => admin_url( 'admin-ajax.php' ),
            'nonce'        => wp_create_nonce( 'lsc_admin_nonce' ),
            'chatterNonce' => wp_create_nonce( 'lsc_chatter_nonce' ),
            'i18n'         => [
                'confirmDelete' => __( 'Are you sure you want to delete this chat?', 'live-support-chat' ),
                'online'        => __( 'Online', 'live-support-chat' ),
                'offline'       => __( 'Offline', 'live-support-chat' ),
            ],
        ] );
    }

    /**
     * Register settings via the Settings API.
     */
    public function register_settings() {
        register_setting( 'lsc_settings_group', 'lsc_chat_status',             [ 'sanitize_callback' => 'sanitize_text_field' ] );
        register_setting( 'lsc_settings_group', 'lsc_widget_position',        [ 'sanitize_callback' => 'sanitize_text_field' ] );
        register_setting( 'lsc_settings_group', 'lsc_widget_text',             [ 'sanitize_callback' => 'sanitize_text_field' ] );
        register_setting( 'lsc_settings_group', 'lsc_offline_message',         [ 'sanitize_callback' => 'sanitize_textarea_field' ] );
        register_setting( 'lsc_settings_group', 'lsc_chatter_dashboard_page',  [ 'sanitize_callback' => 'absint' ] );
        register_setting( 'lsc_settings_group', 'lsc_notification_email',         [ 'sanitize_callback' => 'sanitize_email' ] );
        register_setting( 'lsc_settings_group', 'lsc_notification_email_subject', [ 'sanitize_callback' => 'sanitize_text_field' ] );
        register_setting( 'lsc_settings_group', 'lsc_chatter_login_page',      [ 'sanitize_callback' => 'absint' ] );
    }

    /* ------------------------------------------------------------------
     * Admin pages
     * ------------------------------------------------------------------ */

    /**
     * Dashboard page.
     */
    public function page_dashboard() {
        $status      = get_option( 'lsc_chat_status', 'offline' );
        $open_chats  = count( LSC_DB::get_chats( [ 'status' => 'open' ] ) );
        $total_chats = count( LSC_DB::get_chats() );
        $unread      = LSC_DB::count_unread();
        require_once LSC_PLUGIN_DIR . 'admin/views/view-dashboard.php';
    }

    /**
     * Activity log page.
     */
    public function page_sessions() {
        require_once LSC_PLUGIN_DIR . 'admin/views/view-sessions.php';
    }

    /**
     * Settings page.
     */
    public function page_settings() {
        $pages = get_pages();
        require_once LSC_PLUGIN_DIR . 'admin/views/view-settings.php';
    }
}
