<?php
/**
 * Main BW Gallery class
 */

if (!defined('ABSPATH')) {
    exit;
}

class BW_Gallery {
    
    private $admin;
    private $shortcode;
    private $taxonomy;
    
    public function init() {
        $this->load_textdomain();
        $this->init_hooks();
        
        // Initialize admin first if in admin area
        if (is_admin()) {
            $this->admin = new BW_Gallery_Admin();
            $this->admin->init();
        }
        
        // Initialize taxonomy after admin to ensure parent menu exists
        $this->taxonomy = new BW_Gallery_Taxonomy();
        $this->taxonomy->init();
        
        $this->shortcode = new BW_Gallery_Shortcode();
        $this->shortcode->init();
    }
    
    private function init_hooks() {
        add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_assets'));
        add_action('init', array($this, 'register_assets'));
    }
    
    private function load_textdomain() {
        load_plugin_textdomain('bw-gallery', false, dirname(plugin_basename(BWG_PLUGIN_DIR)) . '/languages');
    }
    
    public function register_assets() {
        // Register frontend CSS
        wp_register_style('bwg-frontend', BWG_PLUGIN_URL . 'assets/css/frontend.css', array(), BWG_VERSION);
        wp_register_style('bwg-lightbox', BWG_PLUGIN_URL . 'assets/css/lightbox.css', array(), BWG_VERSION);
        
        // Register frontend JS
        wp_register_script('bwg-frontend', BWG_PLUGIN_URL . 'assets/js/frontend.js', array('jquery'), BWG_VERSION, true);
        wp_register_script('bwg-lightbox', BWG_PLUGIN_URL . 'assets/js/lightbox.js', array('jquery'), BWG_VERSION, true);
        
        // Localize script
        wp_localize_script('bwg-frontend', 'bwg_ajax', array(
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('bwg_nonce')
        ));
    }
    
    public function enqueue_frontend_assets() {
        if (has_shortcode(get_post_field('post_content', get_the_ID()), 'bw_gallery')) {
            wp_enqueue_style('bwg-frontend');
            wp_enqueue_style('bwg-lightbox');
            wp_enqueue_script('bwg-frontend');
            wp_enqueue_script('bwg-lightbox');
        }
    }
}