<?php


namespace App\Http\Middleware;

use App\Consts\ErrorConst;
use App\Facade\Site;
use App\Libs\Utils;
use Closure;
use App\Exceptions\ApiException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Vinkla\Hashids\Facades\Hashids;

class CheckBook
{
    /**
     * @param         $request
     * @param Closure $next
     * @return mixed
     * @throws ApiException
     */
    public function handle($request, Closure $next)
    {
        $params = $request->all();
        $cid = getProp($params, 'cid');
        $bid = getProp($params, 'bid');
        if ($bid) {
            if (mb_strlen($bid) === 32) {
                $bid = Hashids::decode($bid)[0];
                if (!$bid) {    // 无法解密的是数据异常
                    Utils::throwError(ErrorConst::DATA_EXCEPTION);
                }
            }

            $isOnShelf = (int)DB::table('book_configs')->where('bid', $bid)->value('is_on_shelf');
            if (!in_array($isOnShelf, [1, 2])) {
                Utils::throwError(ErrorConst::BOOK_NOT_EXIST);
            }
        }

        if (!$cid) return $next($request);
        if (mb_strlen($cid) === 32) {
            $cid = Hashids::decode($cid)[0];
            if (!$cid) {    // 无法解密的是数据异常
                Utils::throwError(ErrorConst::DATA_EXCEPTION);
            }
        }

        // 获取章节信息
        $chapter_info = DB::table('chapters')->leftJoin('book_configs', 'chapters.bid', 'book_configs.bid')
            ->whereIn('book_configs.is_on_shelf', [1, 2])->where('chapters.id', $cid)
            ->select('chapters.is_vip', 'chapters.sequence')->first();
        if (!$chapter_info) {
            Utils::throwError(ErrorConst::DATA_EXCEPTION);
        }

        $uid = Site::getUid();

        // 获取站点信息
        $distribution_channel_id = Site::getChannelId();
        $distribution_channel_info = DB::table('distribution_channels')->where('id', $distribution_channel_id)->first();
        $channel_type = getProp($distribution_channel_info, 'channel_type');

        switch ($channel_type) {
            case 'PERIOD':      // 时间周期(会员制)
                // 用户未登录不可看第10章以后的章节
                if (!$uid && getProp($chapter_info, 'sequence') > 10) {
                    Utils::throwError(ErrorConst::NOT_LOGIN);
                }

                // 验证收费章节是否可以阅读
                if ($uid && getProp($chapter_info, 'is_vip')) {
                    $vip_limit_date = DB::table('users')->where('id', $uid)->value('vip_limit_date');
                    $vip_limit_date = transDate($vip_limit_date, 'Y-m-d');
                    if (!$vip_limit_date || $vip_limit_date < date('Y-m-d')) {
                        Utils::throwError(ErrorConst::VIP_VALID);
                    }
                }
                break;
            case 'FREE':        // 全免费
                //TODO 全免费站点无需检验章节内容
                break;
            case 'RECHARGE':    // 充值
                //TODO 预留逻辑(后续开发充值类型的章节判断)
                break;
            default:
                break;
        }
        return $next($request);
    }
}
