<?php


namespace App\Http\Controllers\API;


use App\Http\Controllers\API\Redis\CheckArrivalCl;
use App\Http\Controllers\API\Test\TestController;
use App\Http\Controllers\Controller;
use App\Service\RedisKeyController;
use Illuminate\Http\Request;
use App\Http\Controllers\API\BlindBox\BlindBoxController;
use App\Http\Controllers\API\BlindBox\BuyBlindBoxController;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Models\UserWallet;
use App\Http\Controllers\API\User\CreateUser;
use App\Models\BlindBoxList;
use App\Models\SetAddress;


class IndexController extends Controller
{
    /**
     * 获取用户信息
     *
     * @param Request $request
     * @return mixed
     */
    public function getUserInfo(Request $request)
    {
        $address = $request->input('address', null);
        $id = $request->input('id', null);
        if (empty($address)) {
            return $this->success('success', '');
        }
        # 创建用户
        $result = app(CreateUser::class)->useCreateAccount($address, $id);
        return $this->success('success', $result);
    }

    /**
     * 获取收款地址
     */
    public function getReceiptAddress(Request $request)
    {
        $address = Redis::get(RedisKeyController::receipt_account);
        if ($address) {
            return $this->success('success', $address);
        }
        $address = SetAddress::query()->first(['address']);
        return $this->success('success', $address->address);

    }

    /**
     * 获取盲盒列表
     *
     * @param Request $request
     * @return mixed
     */
    public function getList(Request $request)
    {
        $result = app(BlindBoxController::class)->getProgressBoxList();
        return $this->success('success', $result);
    }

    /**
     * 用户订单
     * status:1-进行中,2-未完成，3-已完成
     */
    public function useUserOrder(Request $request)
    {
        $address = $request->input('address', null);
        $status = $request->input('status', null);
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 10);
        if (empty($address) || empty($status)) {
            return $this->error('缺少数据', 400, 400);
        }
        $result = app(BlindBoxController::class)->getUserPayBlindBox($address, $status, $page, $limit, $request);
        return $this->success('success', $result);
    }

    /**
     * 用户能否购买当前盲盒
     */
    public function useCanNotBuy(Request $request)
    {
        $address = $request->input('address', null);
        $id = $request->input('id', null);
        if (empty($address) || empty($id)) {
            return $this->success('success', false);
        }
        #
        $items = BlindBoxList::query()->where('id', $id)->first(['purchased', 'max_participants_num']);
        $purchased = json_decode($items->purchased);
        # 已经买过的
        if ($purchased && count($purchased)) {
            foreach ($purchased as $obj) {
                if ($obj->address == $address) {
                    # 无法购买
                    return $this->success('success', false);
                }
            }
            # 超出最高购买人数的
            if (count($purchased) >= $items->max_participants_num) {
                # 无法购买
                return $this->success('success', false);
            }
        }
        return $this->success('success', true);


    }

    /**
     * 购买盲盒
     */
    public function useBuy(Request $request)
    {
        $address = $request->input('address', null);
        $hash = $request->input('hash', null);
        $id = $request->input('id', null);
        # 邀请码
        $invite_code = $request->input('invite_code', null);
        # 邀请人分享的盲盒id
        $Invitees_box_id = $request->input('Invitees_box_id', null);
        if (empty($address) || empty($hash) || empty($id)) {
            return $this->error('缺少数据', 400, 400);
        }
        # 检查hash是否存在
        $hashList = UserWallet::query()->where('hash', $hash)->first();
        if ($hashList) {
            return $this->error('hash已存在', 400, 400);
        }
        $result = app(BuyBlindBoxController::class)->toBuy($address, $hash, $id, $invite_code, $Invitees_box_id);
        return $this->success('success', $result);
    }

    /**
     * 用户购买轮询
     * 'hash': '0xb38850234001aa9e8533f3662f31a3039ed58d103caed5009122ac2be66082b7',
     * 'notify_url': 'http://127.0.0.1:8000/api/BlindBox/buy/check',
     * 'retry': 1,
     * 'amount': 1,
     * 'form': '0x2c329b92f1d0b2f2c0b28fd1bf9a2f96989a9a4f',
     * 'to': '0xe6df05ce8c8301223373cf5b969afcb1498c5528',
     */
    public function usePayCheck(Request $request)
    {
        $hash = $request->input('hash', null);
        if (empty($hash)) {
            return $this->error('缺少数据', 400, 400);
        }
        # 获取状态
        $item = UserWallet::query()->where('hash', $hash)->first(['status']);
        if ($item->status == 1) {
            return $this->success('success', true);
        } else {
            # 未到账
            return $this->success('success', false);
        }
    }

    /**
     * python 查询hash回调
     */
    public function useCheckCallback(Request $request)
    {
        $hash = $request->input('hash', null);
        $status = $request->input('status', null);
        if (empty($hash) || empty($status) || $status == 0) {
            return false;
        }
        $result = app(BuyBlindBoxController::class)->CheckCallback($hash, $status);
        return $this->success('success', $result);
    }


    /**
     * nodejs - 转账回调
     */
    public function useNodeJsTransfer(Request $request)
    {
        $data = $request->input();
        if (empty($data['to']) || empty($data['hash']) || empty($data['status'])
            || empty($data['amount']) || empty($data['box_id']) || empty($data['type'])) {
            return $this->success('success', 'false');
        }
        $result = app(CheckArrivalCl::class)->TransferCallback($data);
        return $this->success('success', $result);
    }


    /**
     * 测试
     */
    public function useMyTest(Request $request)
    {
        $result = app(TestController::class)->useTest();
        return $this->success('success', $result);
    }
}
