<?php


namespace App\Http\Controllers\API\BlindBox;

use App\Models\BlindBoxList;
use App\Models\SetAddress;
use App\Models\UserWallet;
use App\Models\UseUser;
use Illuminate\Support\Facades\Log;
use App\Jobs\CheckArrival;


class BuyBlindBoxController
{
    /**
     *
     */
    public function toBuy($address, $hash, $id, $invite_code, $Invitees_box_id)
    {
        # 需要hash--address--盲盒id
        # 获取盲盒价格
        $price = BlindBoxList::query()->where('id', $id)->first(['price']);
        # 检查提交的盲盒id是否存在
        $box_id_item = BlindBoxList::query()->where('id', $Invitees_box_id)->first(['id']);
        if ($price) {
            # 判断是否存在邀请人
            if ($invite_code && $box_id_item) {
                # 是分享的链接
                # 筛选出邀请码对应的地址
                $item = UseUser::query()->where('invitation_code', $invite_code)->first(['user_address']);
                $cur_code_address = '';
                if ($item->user_address) {
                    $cur_code_address = $item->user_address;
                }
                # 存在邀请人--且--购买账户与与邀请人账户不一致
                if ($cur_code_address && $cur_code_address != $address) {
                    # 向order表插入邀请人地址和邀请人购买的盒子id
                    UserWallet::query()->create(['user_address' => $address, 'status' => 0, 'price' => $price->price,
                        'blind_box_id' => $id, 'hash' => $hash, 'Invitees_address' => $cur_code_address, 'Invitees_box_id' => $Invitees_box_id]);
                } else {
                    # 创建订单
                    UserWallet::query()->create(['user_address' => $address, 'status' => 0, 'price' => $price->price, 'blind_box_id' => $id, 'hash' => $hash]);
                }
            } else {
                # 创建订单
                UserWallet::query()->create(['user_address' => $address, 'status' => 0, 'price' => $price->price, 'blind_box_id' => $id, 'hash' => $hash]);
            }
            $address = SetAddress::query()->first();
            $to = $address->address;
            # 添加订单后
            # 异步检查到账情况
            dispatch(new CheckArrival($hash, $price->price, $address, $to));
            return true;
        } else {
            return false;
        }
    }

    /**
     * python检查回调
     */
    public function CheckCallback($hash, $status)
    {
        # status没有改变就直接return
        $change = UserWallet::query()->where('hash', $hash)->first(['status']);
        if ($change->status == $status) {
            return '';
        }
        # 更新订单状态
        UserWallet::query()->where('hash', $hash)->update(['status' => $status]);
        if ($status == 1) {
            # 获取当前hash的购买地址-和盲盒id
            $item = UserWallet::query()->where('hash', $hash)->first();
            #获取已购买列表
            $purchased = BlindBoxList::query()->where('id', $item->blind_box_id)->first(['purchased', 'min_participants_num']);
            #更新盲盒购买状态
            $pur_list = json_decode($purchased->purchased);
            if ($pur_list && count($pur_list)) {
                # 先判断是否存在地址了
                foreach ($pur_list as $values) {
                    if ($values->address == $item->user_address) {
                        return '已存在';
                    }
                }
                # 盲盒购买者是否需要添加邀请人数
                self::AddInviteNum($item, $status);
                $obj = (object)[];
                $obj->address = $item->user_address;
                $obj->invite_num = 0;
                $obj->rate = 0;
                # 往数组添加
                array_push($pur_list, $obj);
                # 更新当前盲盒购买人数
                BlindBoxList::query()->where('id', $item->blind_box_id)->update(['purchased' => json_encode($pur_list)]);
            } else {
                # 首次添加
                # 盲盒购买者是否需要添加邀请人数
                self::AddInviteNum($item, $status);
                $list = [];
                $obj = (object)[];
                $obj->address = $item->user_address;
                $obj->invite_num = 0;
                $obj->rate = 0;
                array_push($list, $obj);
                BlindBoxList::query()->where('id', $item->blind_box_id)->update(['purchased' => json_encode($list)]);
            }
            #
        }
        return '';
    }

    /**
     * 给邀请人添加邀请人数记录
     */
    private function AddInviteNum($item, $status)
    {
        # 只操作购买成功的-且两个人购买的是相同盲盒--且两个地址不一致
        if ($status == 1 && $item && $item->blind_box_id == $item->Invitees_box_id && $item->user_address != $item->Invitees_address) {
            # 找到盲盒
            $values = BlindBoxList::query()->where('id', $item->Invitees_box_id)->first(['purchased', 'can_invite_num', 'max_participants_num']);
            $purchased = json_decode($values->purchased);
            if ($purchased && count($purchased)) {
                foreach ($purchased as $obj) {
                    # 最高邀请人数-1.不能超过盲盒最大购买数量减一,,-2.不能超过后台设置的可邀请人数
                    if ($obj->address == $item->Invitees_address && $obj->invite_num < $values->can_invite_num && $obj->invite_num < $values->max_participants_num) {
                        $obj->invite_num += 1;
                        return;
                    }
                }
            }
            # 更新盲盒
            BlindBoxList::query()->where('id', $item->Invitees_box_id)->update(['purchased' => json_encode($purchased)]);
        }
    }
}
