<?php
defined('ABSPATH') or die('Direct access not allowed');

class CodeEditor_DiscuzPreview {
    private $discuz_path = '';
    
    public function __construct() {
        add_action('wp_ajax_code_editor_preview_discuz_theme', [$this, 'handle_theme_preview']);
        add_action('wp_ajax_code_editor_preview_discuz_plugin', [$this, 'handle_plugin_preview']);
        add_action('wp_ajax_code_editor_set_discuz_path', [$this, 'handle_set_discuz_path']);
        
        // 从选项加载Discuz路径
        $this->discuz_path = get_option('code_editor_discuz_path', '');
    }

    public function handle_set_discuz_path() {
        check_ajax_referer('code_editor_nonce', 'security');
        
        if (!current_user_can('manage_options')) {
            wp_send_json_error('Unauthorized', 403);
        }

        $path = isset($_POST['path']) ? sanitize_text_field($_POST['path']) : '';
        
        if (!file_exists($path) || !is_dir($path)) {
            wp_send_json_error('路径不存在或不是目录', 400);
        }

        update_option('code_editor_discuz_path', $path);
        $this->discuz_path = $path;
        
        wp_send_json_success(['message' => 'Discuz路径已设置']);
    }

    public function set_discuz_path($path) {
        $this->discuz_path = $path;
    }

    public function handle_theme_preview() {
        check_ajax_referer('code_editor_nonce', 'security');
        
        if (!current_user_can('manage_options')) {
            wp_send_json_error('Unauthorized', 403);
        }

        $theme_name = isset($_POST['theme_name']) ? sanitize_text_field($_POST['theme_name']) : '';
        $theme_content = isset($_POST['theme_content']) ? $_POST['theme_content'] : '';
        
        try {
            if (empty($this->discuz_path)) {
                throw new Exception('未设置Discuz路径');
            }

            $temp_dir = $this->discuz_path . '/template/' . $theme_name;
            if (!file_exists($temp_dir)) {
                wp_mkdir_p($temp_dir);
            }

            file_put_contents($temp_dir . '/preview.css', $theme_content);
            
            wp_send_json_success([
                'message' => '主题预览已生成',
                'preview_url' => $this->get_discuz_url() . '?preview_theme=' . $theme_name
            ]);
        } catch (Exception $e) {
            wp_send_json_error('主题预览失败: ' . $e->getMessage(), 500);
        }
    }

    public function handle_plugin_preview() {
        check_ajax_referer('code_editor_nonce', 'security');
        
        if (!current_user_can('manage_options')) {
            wp_send_json_error('Unauthorized', 403);
        }

        $plugin_name = isset($_POST['plugin_name']) ? sanitize_text_field($_POST['plugin_name']) : '';
        $plugin_content = isset($_POST['plugin_content']) ? $_POST['plugin_content'] : '';
        
        try {
            if (empty($this->discuz_path)) {
                throw new Exception('未设置Discuz路径');
            }

            $temp_dir = $this->discuz_path . '/plugin/' . $plugin_name;
            if (!file_exists($temp_dir)) {
                wp_mkdir_p($temp_dir);
            }

            file_put_contents($temp_dir . '/preview.php', $plugin_content);
            
            wp_send_json_success([
                'message' => '插件预览已生成',
                'preview_url' => $this->get_discuz_url() . 'plugin.php?id=' . $plugin_name . ':preview'
            ]);
        } catch (Exception $e) {
            wp_send_json_error('插件预览失败: ' . $e->getMessage(), 500);
        }
    }

    private function get_discuz_url() {
        return rtrim($this->discuz_path, '/') . '/';
    }
}

new CodeEditor_DiscuzPreview();