<?php

namespace App\Http\Controllers\Share;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Core\Auth;
use App\Models\Share\FlowerListModel;
use App\Services\Share\FlowerService;
use Illuminate\Http\Request;

class FlowerController extends Controller
{
    /**
     * 最近10个成功的气泡
     */
    public function bannerlist()
    {
        $fs = new FlowerService();
        $data = $fs->bannerlist();
        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
        );
        $fs = new FlowerService();
        $data = $fs->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
        );
        $fs = new FlowerService();
        $data = $fs->faillist($uid, $pages);
        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();
        $fs = new FlowerService();
        $data = $fs->check($uid, $list_id);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    public function store()
    {
        $uid = Auth::auth();
        $listModel = new FlowerListModel();
        $data = $listModel->where([
            ['uid', $uid],
            ['completeness', '<', 100],
            ['expired_at', '>', time()]
        ])->first();
        if (collect($data)->isEmpty()) {
            $fs = new FlowerService();
            $data = $fs->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
            );
        }
    }

    public function get(int $list_id)
    {
        $fs = new FlowerService();
        $data = $fs->get($list_id);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

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

    /**
     * 上一个小花红包
     */
    public function latest()
    {
        $uid = Auth::auth();
        $fs = new FlowerService();
        $data = $fs->latest($uid);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

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