<?php

namespace App\Livewire;

use App\Services\ExamPdfExportService;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Livewire\Component;

/**
 * 题目预览验证工具
 * 用于验证题目在网页和PDF中的显示效果
 */
class QuestionPreviewTool extends Component
{
    // 表单输入
    public string $stem = '';
    public string $optionA = '';
    public string $optionB = '';
    public string $optionC = '';
    public string $optionD = '';
    public string $answer = '';
    public string $solution = '';

    // 预览状态
    public bool $showWebPreview = false;
    public bool $showPdfPreview = false;
    public ?string $pdfUrl = null;
    public ?string $pdfError = null;

    /**
     * 触发网页预览
     */
    public function previewWeb(): void
    {
        $this->showWebPreview = true;
        $this->dispatch('render-math');
    }

    /**
     * 触发 PDF 预览
     */
    public function previewPdf(): void
    {
        $this->pdfError = null;
        $this->pdfUrl = null;

        try {
            // 构建题目数据
            $questionData = $this->buildQuestionData();

            // 调用 PDF 生成服务
            $pdfService = app(ExamPdfExportService::class);
            $result = $pdfService->generatePreviewPdf($questionData);

            if ($result && isset($result['url'])) {
                $this->pdfUrl = $result['url'];
                $this->showPdfPreview = true;
            } else {
                $this->pdfError = 'PDF 生成失败，请检查输入内容';
            }
        } catch (\Exception $e) {
            Log::error('QuestionPreviewTool: PDF生成失败', [
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString(),
            ]);
            $this->pdfError = 'PDF 生成出错: ' . $e->getMessage();
        }
    }

    /**
     * 同时预览网页和 PDF
     */
    public function previewBoth(): void
    {
        $this->previewWeb();
        $this->previewPdf();
    }

    /**
     * 清空表单
     */
    public function clearForm(): void
    {
        $this->stem = '';
        $this->optionA = '';
        $this->optionB = '';
        $this->optionC = '';
        $this->optionD = '';
        $this->answer = '';
        $this->solution = '';
        $this->showWebPreview = false;
        $this->showPdfPreview = false;
        $this->pdfUrl = null;
        $this->pdfError = null;
    }

    /**
     * 构建题目数据结构
     */
    private function buildQuestionData(): array
    {
        $hasOptions = !empty($this->optionA) || !empty($this->optionB) ||
                      !empty($this->optionC) || !empty($this->optionD);

        $options = null;
        $questionType = 'fill'; // 默认填空题

        if ($hasOptions) {
            $questionType = 'choice';
            $options = [];
            if (!empty($this->optionA)) $options['A'] = $this->optionA;
            if (!empty($this->optionB)) $options['B'] = $this->optionB;
            if (!empty($this->optionC)) $options['C'] = $this->optionC;
            if (!empty($this->optionD)) $options['D'] = $this->optionD;
        }

        return [
            'stem' => $this->stem,
            'options' => $options,
            'answer' => $this->answer,
            'solution' => $this->solution,
            'question_type' => $questionType,
        ];
    }

    /**
     * 获取用于网页预览的数据
     */
    public function getPreviewDataProperty(): array
    {
        return $this->buildQuestionData();
    }

    public function render()
    {
        return view('livewire.question-preview-tool')
            ->layout('layouts.preview-tool');
    }
}
