<?php

namespace App\Http\Controllers\Book;


use App\Libs\ApiResponse;
use App\Services\Book\BookService;
use App\Transformer\Book\BookTransformer;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;

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 hotSearches(Request $request) {
        $result = $this->bookService->getHotSearches();
        return $this->success($result);
    }

    /**
     * 书籍详情
     * @param Request $request
     * @return mixed
     */
    public function bookDetail(Request $request) {
        $data = $request->all();

        $result = $this->bookService->getBookDetail($data);
        return $this->success($result);
    }

    /**
     * 获取某本书的阅读记录
     * @param Request $request
     * @return mixed
     */
    public function recentChapter(Request $request) {
        $data = $request->all();

        $result = $this->bookService->recentChapter($data);
        return $this->success($result);
    }

    /**
     * 生成分享链接参数(小程序内部调用)
     * @param Request $request
     * @return mixed
     */
    public function setUrlLink(Request $request) {
        $data = $request->all();

        $result = $this->bookService->setUrlLink($data);
        return $this->success($result);
    }

    /**
     * 生成分享链接(外网)
     * @param Request $request
     * @return mixed
     */
    public function setDyLink(Request $request) {
        $data = $request->all();

        $result = $this->bookService->setDyLink($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 setBookChargeType(Request $request) {
        $data = $request->all();
        $result = $this->bookService->setBookChargeType($data);
        return $this->success(['success'=>$result ? 1 : 0]);
    }

    /**
     * 章节信息
     * @param Request $request
     * @return mixed
     */
    public function chapterInfo(Request $request) {
        $data = $request->all();

        $result = $this->bookService->getChapterInfo($data);

        return $this->success($result);
    }

    /**
     * 生成派单链接
     * @param Request $request
     * @return mixed
     */
    public function setSendOrder(Request $request) {
        $data = $request->all();
        $result = $this->bookService->setSendOrder($data);
        return $this->success(['success'=>$result ? 1 : 0]);
    }

    /**
     * 一级分类列表
     * @param Request $request
     * @return mixed
     */
    public function categoryList(Request $request) {
        $data = $request->all();

        $result = $this->bookService->getCategoryList($data);
        return $this->success(['list'=>$result]);
    }

    /**
     * 分类书籍
     * @param Request $request
     * @return mixed
     */
    public function categoryBooks(Request $request) {
        $data = $request->all();

        $result = $this->bookService->getCategoryBooks($data);
        return $this->success($result, [new BookTransformer(), 'newBuildCategoryBooks']);
    }
}
