<?php

namespace App\Http\Controllers\Book;

use App\Facade\Site;
use App\Consts\ErrorConst;
use App\Exceptions\ApiException;
use App\Libs\ApiResponse;
use App\Libs\Utils;
use App\Services\Book\BookService;
use App\Transformer\Book\BookTransformer;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;

class BookController extends BaseController
{
    use ApiResponse;
    protected $bookService;

    public function __construct(
        BookService $bookService
    ) {
        $this->bookService = $bookService;
    }

    /**
     * 书籍列表
     *
     * @param Request $request
     * @return mixed
     */
    public function bookList(Request $request) {
        $data = $request->all();
        $result = $this->bookService->getBookList($data);
        return $this->success($result, [new BookTransformer(), 'newBuildBookList']);
    }

    /**
     * 可选书籍
     *
     * @param Request $request
     * @return mixed
     */
    public function allBooks(Request $request) {
        $data = $request->all();
        $result = $this->bookService->getAllBooks($data);
        return $this->success($result);
    }

    /**
     * 可选版本
     *
     * @param Request $request
     * @return mixed
     */
    public function bookVersion(Request $request) {
        $data = $request->all();
        $result = $this->bookService->getBookVersion($data);
        return $this->success($result);
    }

    /**
     * 新增书籍版本
     *
     * @param Request $request
     * @return mixed
     */
    public function addBookVersion(Request $request) {
        $data = $request->all();
        $result = $this->bookService->addBookVersion($data);
        return $this->success($result);
    }

    /**
     * 修改版本名称
     *
     * @param Request $request
     * @return mixed
     */
    public function editBookVersion(Request $request) {
        $data = $request->all();
        $result = $this->bookService->editBookVersion($data);
        return $this->success($result);
    }

    /**
     * 章节列表
     *
     * @param Request $request
     * @return mixed
     */
    public function chapterList(Request $request) {
        $data = $request->all();
        $result = $this->bookService->getChapterList($data);
        return $this->success($result, [new BookTransformer(), 'newBuildChapterList']);
    }

    /**
     * 章节内容
     *
     * @param Request $request
     * @return mixed
     */
    public function chapterContent(Request $request) {
        $data = $request->all();
        $result = $this->bookService->getChapterContent($data);
        return $this->success($result);
    }

    /**
     * 版本列表
     *
     * @param Request $request
     * @return mixed
     */
    public function versionList(Request $request) {
        $data = $request->all();
        $result = $this->bookService->getVersionList($data);
        return $this->success($result, [new BookTransformer(), 'newBuildVersionList']);
    }

    /**
     * 合成任务列表
     *
     * @param Request $request
     * @return mixed
     */
    public function taskList(Request $request) {
        $data = $request->all();
        $result = $this->bookService->getTaskList($data);
        return $this->success($result, [new BookTransformer(), 'newBuildTaskList']);
    }

    /**
     * 合成任务详情
     *
     * @param Request $request
     * @return mixed
     */
    public function taskDetail(Request $request) {
        $data = $request->all();
        $result = $this->bookService->getTaskDetail($data);
        return $this->success($result);
    }

    // 音效管理
    public function audioEffectList(Request $request) {
        $data = $request->all();
        $result = $this->bookService->audioEffectList($data);
        return $this->success($result, [new BookTransformer(), 'newBuildAudioEffectList']);
    }

    // 可选音效
    public function audioEffects(Request $request) {
        $data = $request->all();
        $result = $this->bookService->audioEffects($data);
        return $this->success($result);
    }

    // 上传音效
    public function uploadAudioEffect(Request $request) {
        $data = $request->all();
        $file = $request->file('file');
        $result = $this->bookService->uploadAudioEffect($data, $file);
        return $this->success($result);
    }

    // 编辑音效
    public function editAudioEffect(Request $request) {
        $data = $request->all();
        $file = $request->file('file');
        $result = $this->bookService->editAudioEffect($data, $file);
        return $this->success(['success' => $result ? 1 : 0]);
    }

    
    // bgm管理
    public function bgmList(Request $request) {
        $data = $request->all();
        $result = $this->bookService->bgmList($data);
        return $this->success($result, [new BookTransformer(), 'newBuildBgmList']);
    }

    // 可选bgm
    public function bgms(Request $request) {
        $data = $request->all();
        $result = $this->bookService->bgms($data);
        return $this->success($result);
    }

    // 上传bgm
    public function uploadBgm(Request $request) {
        $data = $request->all();
        $file = $request->file('file');
        $result = $this->bookService->uploadBgm($data, $file);
        return $this->success($result);
    }

    // 上传bgm
    public function editBgm(Request $request) {
        $data = $request->all();
        $file = $request->file('file');
        $result = $this->bookService->editBgm($data, $file);
        return $this->success(['success' => $result ? 1 : 0]);
    }
}
