<?php

namespace App\Http\Controllers\Manage\Book;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Redis;

class ReadRecordController extends Controller
{


    /**
     * @apiDefine readrecord 阅读记录
     */


    /**
     * @apiVersion 1.0.0
     * @apiDescription 保存阅读记录
     * @api {post} books/readrecord 保存阅读记录
     * @apiGroup readrecord
     * @apiName saveRecordRecord
     * @apiParam {Int}  uid
     * @apiParam {Int}  bid
     * @apiParam {Int}  cid
     * @apiParam {String}  book_name
     * @apiParam {String}  chapter_name
     * @apiParam {Int}  cid
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data: {
     *            }
     *       }
     */
    public function saveRecordRecord(Request $request){
        $uid = $request->input('uid');
        $bid = $request->input('bid');
        $cid = $request->input('cid');
        $book_name = $request->input('book_name');
        $chapter_name = $request->input('chapter_name');
        if(empty($uid) || empty($bid) || empty($cid) || empty($book_name) || empty($chapter_name)){
            return response()->error('PARAM_ERROR');
        }
        Redis::hset('book_base:'.$uid, 'last_read', "{$bid}_{$cid}_{$book_name}_{$chapter_name}_".time());
        //Redis::hset('book_read:'.$uid, $bid, $cid."_".time());
        Redis::hset('book_read:'.$uid, $bid, "{$cid}_{$book_name}_{$chapter_name}_".time());
        return response()->success();

    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取阅读记录
     * @api {get} books/readrecord 获取阅读记录
     * @apiGroup readrecord
     * @apiName getReadRecord
     * @apiParam {Int}  uid
     * @apiSuccess {Int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {Array}       data 结果集
     * @apiSuccess {Int}         data.bid  书籍id
     * @apiSuccess {Int}         data.cid  章节id
     * @apiSuccess {Int}         data.time 时间
     * @apiSuccess {String}      data.book_name  书名
     * @apiSuccess {String}      data.chapter_name  章节名
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data:[
     *             {
     *                 bid:5,
     *                 cid:564,
     *                 book_name:"hello world",
     *                 chapter_name:"hello world",
     *                 time:1511246882
     *             },
     *             {
     *                 bid:5,
     *                 cid:564,
     *                 book_name:"hello world",
     *                 chapter_name:"hello world",
     *                 time:1511246882,
     *             },
     *         ]
     *
     *       }
     */
    public function getReadRecord(Request $request){
        $uid = $request->input('uid');
        //$bid = $request->input('bid');
        if(empty($uid)) return response()->error('PARAM_ERROR');
        $read_bids = Redis::hgetall('book_read:'.$uid);
        if(!$read_bids) return response()->success();
        $res = [];
        $i = 0;
        foreach ($read_bids as $key=>$v){
            $record = explode('_',$v);
            $latest_read_cid = $record[0];
            $book_name = $record[1];
            $chapter_name = $record[2];
            $latest_read_time = $record[count($record)-1];
            $res[$i] = ['book_name'=>$book_name,'bid'=>$key,'cid'=>(int)$latest_read_cid,'time'=>(int)$latest_read_time,'chapter_name'=>$chapter_name];
            //$res[$i] = ['bid'=>$key,'cid'=>$latest_read_cid,'book_name'=>,'time'=>$latest_read_time];
            $i++;
        }
        usort($res,function($a,$b){
            if($a['time'] >= $b['time']) return -1;
            return 1;

        });

        return response()->success($res);
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 删除阅读记录
     * @api {post} books/del/readrecord 删除阅读记录
     * @apiGroup readrecord
     * @apiName deleteRecord
     * @apiParam {Int}  uid
     * @apiParam {Int}  bid
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data: {
     *            }
     *       }
     */
    public function deleteRecord(Request $request){
        $uid = $request->input('uid');
        $bid = $request->input('bid');
        if(empty($uid) || empty($cid)){
            response()->error('PARAM_ERROR');
        }
        if(Redis::hexists('book_read:'.$uid,$bid)){
            Redis::hdel('book_read:'.$uid,$bid);
        }
        return response()->success();
    }
}
