<?php

namespace App\Http\Controllers\Share;

use App\Exceptions\ApiException;
use App\Models\Share\RedpackListModel;
use App\Services\Share\RedpackService;
use Illuminate\Http\Request;
use App\Http\Controllers\Core\Auth;
use App\Http\Controllers\Controller;

class RedpackController extends Controller
{
    /**
     * 最近10个成功的气泡
     */
    public function bannerlist()
    {
        $rs = new RedpackService();
        $data = $rs->bannerlist();
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 某人正在进行的现金红包
     */
    public function ingredpack()
    {
        $uid = Auth::auth();
        $redpackService = new RedpackService();
        $data = $redpackService->ingredpack($uid);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 获取红包信息
     * @param  int  $list_id
     * @return array
     */
    public function get(int $list_id)
    {
        $rs = new RedpackService();
        $data = $rs->get($list_id);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 检验助力现金红包
     * @param  int  $list_id
     * @return array
     * @throws \Throwable
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function check(int $list_id)
    {
        $uid = Auth::auth();
        $rs = new RedpackService();
        $data = $rs->pcheck($uid, $list_id);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }


    public function succlist(Request $request)
    {
        $uid = Auth::auth();
        $page = $request->get('page') ?? 1;
        $pages = array(
            'limit' => 20,
            'page' => $page
        );
        $rs = new RedpackService();
        $data = $rs->susslist($uid, $pages);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    public function faillist(Request $request)
    {
        $uid = Auth::auth();
        $page = $request->get('page') ?? 1;
        $pages = array(
            'limit' => 20,
            'page' => $page
        );
        $rs = new RedpackService();
        $data = $rs->faillist($uid, $pages);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 创建现金红包
     */
    public function store()
    {
        $uid = Auth::auth();
        $listModel = new RedpackListModel();
        $data = $listModel->where([
            ['uid', $uid],
            ['expired_at', '>', time()],
            ['is_withdraw', 0]
        ])->OrWhere([
            ['uid', $uid],
            ['completeness', '>=', 100],
            ['is_withdraw', 0]
        ])->first();
        if (collect($data)->isEmpty()) {
            $rs = new RedpackService();
            $data = $rs->store($uid);
            $data['get_fee'] = (string)sprintf('%.2f', $data['fee'] * $data['completeness'] / 100);
            return array(
                'code' => 200,
                'message' => 'success',
                'data' => $data
            );
        } else {
            $data['get_fee'] = (string)sprintf('%.2f', $data['fee'] * $data['completeness'] / 100);
            return array(
                'code' => 200,
                'message' => 'success',
                'data' => $data
            );
        }
    }

    /**
     * 切换通知
     * @param  int  $list_id
     * @return array
     * @throws ApiException
     * @throws \App\Exceptions\AlertException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function notice(int $list_id)
    {
        $uid = Auth::auth();
        $rs = new RedpackService();
        if ($rs->notice($uid, $list_id)) {
            return array(
                'code' => 200,
                'message' => 'success'
            );
        } else {
            throw new ApiException("系统异常", 500);
        }
    }

    /**
     * 现金红包提现
     * @param  int  $list_id
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function withdraw(int $list_id)
    {
        $uid = Auth::auth();
        $rs = new RedpackService();
        $rs->withdraw($uid, $list_id);
        return array(
            'code' => 200,
            'message' => 'success'
        );
    }
}
