<?php


namespace App\Http\Controllers\API\BlindBox;

use App\Models\BlindBoxList;
use App\Models\UserWallet;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Log;

class BlindBoxController
{

    /**
     * 需要的参数
     */

    private $Parameter = ['end_time', 'id', 'invite_up_rate', 'max_participants_num', 'price',
        'projectName', 'project_icon', 'rules', 'start_time', 'status', 'purchased', 'winner_list'];

    /**
     * 进行中的盲盒列表
     */
    public function getProgressBoxList()
    {
        $list = BlindBoxList::query()->where('status', '!=', 3)->get($this->Parameter) ?? [];
        if ($list && count($list)) {
            # 计算购买倒计时
            foreach ($list as $item) {
                # 获取当前时间
                $cur_time = ToTimestamp('now');
                $end_time = ToTimestamp($item->end_time);
                # 购买倒计时
                $countdown = $end_time - $cur_time;
                # 购买人数list
                $purchased = json_decode($item->purchased);
                $buy_num = 0;
                if ($purchased && count($purchased)) {
                    $buy_num = count($purchased);
                }
                # 删除原始购买人数字段
                unset($item->purchased);
                # 修改购买人数展示形式
                $item->buy_num = $buy_num;
                if ($countdown > 0) {
                    $item->countdown = $countdown * 1000;
                } else {
                    $item->countdown = 0;
                }
            }
            return $list;
        } else {
            return [];
        }

    }

    /**
     * 用户订单
     * status
     * 1-未完成-购买人数不够
     * 2-进行中-购买人数已达到指定数，未到开奖时间的
     * 3-已完成--已开奖
     */
    public function getUserPayBlindBox($address, $status, $page, $limit, $request)
    {
        # 获取用户购买成功的订单id
        $ID_LIST = UserWallet::query()->where('user_address', $address)->where('status', 1)->get(['blind_box_id']);
        if (count($ID_LIST)) {
            #  存在购买的订单-前往盲盒列表查询
            $list = [];
            foreach ($ID_LIST as $value) {
                if ($status == 3) {
                    # 获取已完成的订单
                    $obj = self::getSuccessOrder($value->blind_box_id, 3);
                    if ($obj) {
                        $buy_num = json_decode($obj->purchased);
                        if ($buy_num && count($buy_num) >= $obj->min_participants_num) {
                            # 购买人数已达到指定数
                            $obj->buy_num = count($buy_num);
                            # - 是否中奖
                            $winner_list = json_decode($obj->winner_list);
                            $obj->is_sucess = false;
                            if ($winner_list && count($winner_list)) {
                                foreach ($winner_list as $item) {
                                    if ($address == $item) {
                                        $obj->is_sucess = true;
                                        break;
                                    }
                                }
                            }
                            unset($obj->purchased);
                            array_push($list, $obj);
                        }
                    }
                } else if ($status == 2) {
                    # 进行中 --
                    $obj = self::getSuccessOrder($value->blind_box_id, 2);
                    if ($obj) {
                        unset($obj->winner_list);
                        $buy_num = json_decode($obj->purchased);
                        if ($buy_num && count($buy_num) >= $obj->min_participants_num) {
                            # 购买人数已达到指定数
                            $obj->buy_num = count($buy_num);
                            unset($obj->purchased);
                            array_push($list, $obj);
                        }
                    }
                } else if ($status == 1) {
                    # 未完成
                    $obj = self::getSuccessOrder($value->blind_box_id, 2);
                    if ($obj) {
                        unset($obj->winner_list);
                        $buy_num = json_decode($obj->purchased);
                        if ($buy_num && count($buy_num) < $obj->min_participants_num) {
                            # 未到达指定人数
                            $obj->buy_num = count($buy_num);
                            unset($obj->purchased);
                            array_push($list, $obj);
                        }
                    }
                }
            }
            # 处理分页
            $total = count($list);   //总数
            $item = array_slice($list, ($page - 1) * $limit, $limit);
            return new LengthAwarePaginator($item, $total, $limit, $page, [
                'path' => $request->url(), // 当前页面的url
                'pageName' => 'page',    //参数名
            ]);
            # return $list;
        } else {
            return new LengthAwarePaginator([], 0, $limit, $page, [
                'path' => $request->url(), // 当前页面的url
                'pageName' => 'page',    //参数名
            ]);
        }
    }

    private function getSuccessOrder($id, $status)
    {
        return BlindBoxList::query()->where('id', $id)->where('status', $status)->first($this->Parameter);
    }
}
