<?php

namespace Blazemedia\OneSignal\Admin;

/**
 * Admin functionality class
 */
class Admin
{
    /**
     * Initialize admin functionality
     */
    public function __construct()
    {
        add_action('admin_menu', [$this, 'addAdminMenu']);
        add_action('admin_init', [$this, 'initSettings']);
        
        // Debug: log that admin is initialized
        error_log('OneSignal Admin: Initialized');
    }

    /**
     * Add admin menu
     */
    public function addAdminMenu()
    {
        // Debug: log menu addition
        error_log('OneSignal Admin: Adding menu page');
        
        add_options_page(
            __('OneSignal Settings', 'onesignal'),
            __('OneSignal', 'onesignal'),
            'manage_options',
            'onesignal-settings',
            [$this, 'renderSettingsPage']
        );
    }

    /**
     * Initialize settings
     */
    public function initSettings()
    {
        register_setting('onesignal_options', 'onesignal_app_id');
        register_setting('onesignal_options', 'onesignal_key_id');
        register_setting('onesignal_options', 'onesignal_rest_api_key');
        register_setting('onesignal_options', 'onesignal_safari_web_id');
        register_setting('onesignal_options', 'onesignal_auto_push');
        register_setting('onesignal_options', 'onesignal_prompt_message');
        register_setting('onesignal_options', 'onesignal_accept_button');
        register_setting('onesignal_options', 'onesignal_cancel_button');
        register_setting('onesignal_options', 'onesignal_delay_seconds');
        register_setting('onesignal_options', 'onesignal_enabled_post_types');
        register_setting('onesignal_options', 'onesignal_logo_url');
        
    }

    /**
     * Render settings page
     */
    public function renderSettingsPage()
    {
        echo '<div class="wrap">';
        echo '<h1>' . __('OneSignal Settings', 'onesignal') . '</h1>';
        echo '<form method="post" action="options.php">';
        settings_fields('onesignal_options');
        
        echo '<table class="form-table">';
        // --- SEZIONE 1: Regole sui post/autopush ---
        echo '<tr><th colspan="2"><h2>' . __('Regole sui post & Auto Push', 'onesignal') . '</h2></th></tr>';
        echo '<tr>';
        echo '<th scope="row">' . __('Auto Push Notifications', 'onesignal') . '</th>';
        echo '<td><input type="checkbox" name="onesignal_auto_push" value="1" ' . checked(get_option('onesignal_auto_push'), '1', false) . ' /> ' . __('Send push notification automatically when a post is published', 'onesignal') . '</td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th scope="row">' . __('Abilita post type', 'onesignal') . '</th>';
        echo '<td>';
        echo '<input type="text" name="onesignal_enabled_post_types" value="' . esc_attr(get_option('onesignal_enabled_post_types', '')) . '" class="regular-text" placeholder="post,page,custom_type" />';
        echo '<p class="description">' . __('Elenco di post type abilitati per le notifiche push, separati da virgola. Se vuoto, solo "post".', 'onesignal') . '</p>';
        echo '</td>';
        echo '</tr>';
        // Campo URL logo
        echo '<tr>';
        echo '<th scope="row">' . __('Logo URL', 'onesignal') . '</th>';
        echo '<td><input type="text" name="onesignal_logo_url" value="' . esc_attr(get_option('onesignal_logo_url', '')) . '" class="regular-text" placeholder="https://..." /></td>';
        echo '</tr>';
        
        // --- SEZIONE 2: Opzioni identificazione app ---
        echo '<tr><th colspan="2"><h2>' . __('Identificativi App OneSignal', 'onesignal') . '</h2></th></tr>';
        echo '<tr>';
        echo '<th scope="row">' . __('OneSignal App ID', 'onesignal') . '</th>';
        echo '<td><input type="text" name="onesignal_app_id" value="' . esc_attr(get_option('onesignal_app_id')) . '" class="regular-text" /></td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th scope="row">' . __('REST API Key', 'onesignal') . '</th>';
        echo '<td><input type="text" name="onesignal_rest_api_key" value="' . esc_attr(get_option('onesignal_rest_api_key')) . '" class="regular-text" /></td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th scope="row">' . __('Safari Web ID', 'onesignal') . '</th>';
        echo '<td><input type="text" name="onesignal_safari_web_id" value="' . esc_attr(get_option('onesignal_safari_web_id')) . '" class="regular-text" /></td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th scope="row">' . __('Key ID', 'onesignal') . '</th>';
        echo '<td><input type="text" name="onesignal_key_id" value="' . esc_attr(get_option('onesignal_key_id')) . '" class="regular-text" /></td>';
        echo '</tr>';
        
        // --- SEZIONE 3: Setup messaggi prompt ---
        echo '<tr><th colspan="2"><h2>' . __('Setup Messaggi Prompt', 'onesignal') . '</h2></th></tr>';
        echo '<tr>';
        echo '<th scope="row">' . __('Prompt Message', 'onesignal') . '</th>';
        echo '<td><textarea name="onesignal_prompt_message" rows="3" class="large-text">' . esc_textarea(get_option('onesignal_prompt_message', 'Vorremmo inviarti notifiche sulle ultime novità pubblicate su questo sito')) . '</textarea></td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th scope="row">' . __('Accept Button Text', 'onesignal') . '</th>';
        echo '<td><input type="text" name="onesignal_accept_button" value="' . esc_attr(get_option('onesignal_accept_button', 'CONTINUA')) . '" class="regular-text" /></td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th scope="row">' . __('Cancel Button Text', 'onesignal') . '</th>';
        echo '<td><input type="text" name="onesignal_cancel_button" value="' . esc_attr(get_option('onesignal_cancel_button', 'NO GRAZIE')) . '" class="regular-text" /></td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th scope="row">' . __('Delay (seconds)', 'onesignal') . '</th>';
        echo '<td><input type="number" name="onesignal_delay_seconds" value="' . esc_attr(get_option('onesignal_delay_seconds', '10')) . '" min="0" max="60" class="small-text" /></td>';
        echo '</tr>';
        
        echo '</table>';
        
        submit_button();
        echo '</form>';
        echo '</div>';
    }
} 