<?php

namespace App\Http\Controllers\Wap\Book;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Wap\BaseController;
use App\Http\Controllers\Wap\Book\Transformers\BookGiftsTransformer;
use App\Modules\Book\Services\BookGiftsService;
use App\Modules\User\Services\UserService;
use Illuminate\Http\Request;
use Hashids;

class BooksGiftController extends BaseController
{
    /**
     * 获取所有可送礼物
     * @return mixed
     */
   public function getAllGifts() {
       $gifts = BookGiftsService::getAllGifts();
       $user = UserService::getById($this->uid);
       $res = ['gift'=>$gifts,'balance'=>$user->balance];
       return response()->success($res);
   }

    /**
     * 用户打赏图书
     * @param Request $request
     * @return mixed
     */
   public function sendGifts(Request $request) {
       if(!$this->checkUid()){
           return response()->error('WAP_NOT_LOGIN');
       }
       $uid = $this->uid;
       $bid = $request->input('bid','');
       if(empty($bid)) {
           return response()->error('PARAM_EMPTY');
       }
       $bid = Hashids::decode($bid)[0];
       $gift_id = $request->input('gift_id','');
       if(empty($gift_id)){
           return response()->error('PARAM_EMPTY');
       }
       $gift_info = BookGiftsService::getOneGift($gift_id);
       if(!$gift_info){
           return response()->error('PARAM_ERROR');
       }
       $icon = $gift_info->icon;
       $name_desc = $gift_info->name_desc;
       $cost = $gift_info->cost;
       $added = BookGiftsService::sendGiftToBook(compact('uid','bid','gift_id','icon','cost','name_desc'));
       if(!$added) {
           return response()->error('WAP_SEND_GIFT_FAILED');
       }
       return response()->success($added);
   }

    /**
     * 获取送礼记录
     * @param Request $request
     * @return mixed
     */
   public function getUserSendGiftsRecord(Request $request) {
       if(!$this->checkUid()){
           return response()->error('WAP_NOT_LOGIN');
       }
       $uid = $this->uid;
       $bid = $request->input('bid','');
       if(empty($bid)) {
           return response()->error('PARAM_EMPTY');
       }
       $bid = Hashids::decode($bid)[0];
       $records = BookGiftsService::getSendRecords($bid);
       return response()->pagination(new BookGiftsTransformer(), $records);
   }

    /**
     * @param Request $request
     * @return mixed
     */
   public function getSendGiftsForConsumeRecord(Request $request){
       if(!$this->checkUid()){
           return response()->error('WAP_NOT_LOGIN');
       }
       $uid = $this->uid;
       $records = BookGiftsService::getConsumeSendRecords($uid);
       return response()->pagination(new BookGiftsTransformer(),$records);
   }

    /**
     * @param Request $request
     * @return mixed
     */
   public function getUserSendGiftsRecordV2(Request $request) {
        if(!$this->checkUid()){
            return response()->error('WAP_NOT_LOGIN');
        }
        $uid = $this->uid;
        $bid = $request->input('bid','');
        if(empty($bid)) {
            return response()->error('PARAM_EMPTY');
        }
        $bid = Hashids::decode($bid)[0];
        $records = BookGiftsService::getSendRecordsV2($bid);

        return response()->success(['records'=>collectionTransform(new BookGiftsTransformer(), $records['record']),'rank'=>$records['rank']]);
   }
}
