<?php

namespace App\Http\Controllers\Channel\Book;

use App\Http\Controllers\Channel\BaseController;
use App\Http\Controllers\Channel\Book\Transformers\ChapterListTransformer;
use App\Http\Controllers\Channel\Book\Transformers\ChapterTransformer;
use App\Modules\Book\Services\ChapterService;
use Hashids;
use Illuminate\Http\Request;

class ChapterController extends BaseController
{

    /**
     * @apiDefine book 图书模块
     */


    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取章节内容
     * @api {get} book/getChapterContent 获取章节内容
     * @apiGroup book
     * @apiParam{Number}bid 图书id
     * @apiParam{Number}cid 章节id
     * @apiName getChapterContent
     * @apiSuccess{int}code 状态码
     * @apiSuccess{String}msg 信息
     * @apiSuccess{object}data 结果集
     * @apiSuccess{Int}data.chapter_id 章节id
     * @apiSuccess{String}data.chapter_name 章节名称
     * @apiSuccess{Int}data.chapter_sequence 序号
     * @apiSuccess{Int}data.chapter_is_vip 是否vip
     * @apiSuccess{Int}data.chapter_size 章节大小
     * @apiSuccess{Int}data.prev_cid 上一章节id
     * @apiSuccess{Int}data.next_cid 下一章节
     * @apiSuccess{String}data.recent_update_at 更新时间
     * @apiSuccess{String}data.chapter_content 章节内容
     * @apiSuccess{Int}data.is_need_subscirbe 是否强制关注(删除)
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       "code": 0,
     *       "msg": "",
     *       "data": {
     *               "chapter_id": 5,
     *               "chapter_name": "第1240章 不是我",
     *               "chapter_sequence": 1239,
     *               "chapter_is_vip": 1,
     *               "chapter_size": 2422,
     *               "prev_cid": 0,
     *               "next_cid": 0,
     *               "recent_update_at": 2017-11-20 15:01:56,
     *               "chapter_content": "叶妩被司行霈的阴阳怪气一吓，思路偏得太远了。 她张口结舌，忘记了自己要说什么。",
     *               "is_need_subscirbe": 1,(删除)
     *            }
     *       }
     */
    function getChapterContent(Request $request)
    {
        $bid = $request->has('bid') ? $request->input('bid') : '';
        $cid = $request->has('cid') ? $request->input('cid') : '';
        if(empty($bid) || empty($cid)) return response()->error("PARAM_EMPTY");
        
        $bid = Hashids::decode($bid)[0];

        $chapter = ChapterService::getChapterFromDb($bid, $cid);
        if($chapter->is_vip == 1) $chapter->content = '该章节不可预览';

        return response()->item(new ChapterTransformer(), $chapter);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取前五章章节内容
     * @api {get} book/getTopFiveChapterContents 获取前五章章节内容
     * @apiGroup book
     * @apiParam{Number}bid 图书id
     * @apiName getTopFiveChapterContents
     * @apiSuccess {int}code状态码
     * @apiSuccess {String}msg信息
     * @apiSuccess {object}data结果集
     * @apiSuccess {Int}data.chapter_id 章节id
     * @apiSuccess {String}data.chapter_name 章节名称
     * @apiSuccess {Int}data.chapter_sequence序号
     * @apiSuccess {Int}data.chapter_is_vip是否vip
     * @apiSuccess {Int}data.chapter_size章节大小
     * @apiSuccess {Int}data.prev_cid上一章节id
     * @apiSuccess {Int}data.next_cid下一章节
     * @apiSuccess {String}data.recent_update_at更新时间
     * @apiSuccess {String}data.chapter_content章节内容
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       "code": 0,
     *       "msg": "",
     *       "data": [{
     *               "chapter_id": 5,
     *               "chapter_name": "第1240章 不是我",
     *               "chapter_sequence": 1239,
     *               "chapter_is_vip": 1,
     *               "chapter_size": 2422,
     *               "prev_cid": 0,
     *               "next_cid": 0,
     *               "recent_update_at": 2017-11-20 15:01:56,
     *               "chapter_content": "叶妩被司行霈的阴阳怪气一吓，思路偏得太远了。 她张口结舌，忘记了自己要说什么。",
     *            },
     *           ]
     *       }
     */
    function getTopFiveChapterContents(Request $request)
    {
        $bid = $request->has('bid') ? $request->input('bid') : '';
        if (empty($bid)) return response()->error("PARAM_EMPTY");
        $bid = Hashids::decode($bid)[0];
        $hidden_book = env('HIDE_BOOKS');
        if($hidden_book){
            $hidden_book_array = explode(',',$hidden_book);
            if(in_array($bid,$hidden_book_array)){
                return response()->error("PARAM_ERROR");
            }
        }
        $data = ChapterService::getTopFiveChapter($bid);
        return response()->collection(new ChapterListTransformer(), $data);
    }
}