<?php


namespace app\services\bet;


use app\models\bet\Bet;
use app\models\process\NumberTab;
use app\models\user\User;
use app\models\user\Session;
use app\services\connect\InitTableService;

class ToBetCommonService
{
    /**
     * TODO toBet检验
     * @param array $tableInfo  桌子信息
     * @param array $event  socket属性
     * @param array $data   下注信息
     * @return array
     */
    public static function toBetCheck(array $tableInfo, array $event, array $data): array
    {
        /** 通用检验重复部分开始 */
        $amount = array_sum($data);
        if ($amount == 0){
            return ['status' => false, 'msg' => 'to_bet_fail_6'];
        }
        $numberTabId = isset($event['number_tab_id']) && intval($event['number_tab_id']) > 0 ? intval($event['number_tab_id']) : 0;
        $userId = isset($event['user_id']) && intval($event['user_id']) > 0 ? intval($event['user_id']) : 0;
        $userInfo = User::get(['id' => $userId, 'status' => 1, 'is_delete' => 0]);
        if (!$userInfo) return ['status' => false, 'msg' => 'to_bet_fail_2'];
        if($userInfo['bet_type'] == 2){
            $session_id = $userInfo['api_token'];
            $sessionInfo = Session::get(['session_id' => $session_id]);
            if(!$sessionInfo){
                return ['status' => false, 'msg' => 'to_bet_fail_9'];
            }
            if(!$sessionInfo['server_status']){
                return ['status' => false, 'msg' => 'to_bet_fail_10'];
            }
        }
        // 使用 InitTableService::initTable 获取当前靴的最新一局，与其他服务保持一致
        $numberTabInfo = InitTableService::initTable($tableInfo);
        if ($numberTabInfo['id'] != $numberTabId){
            return ['status' => false, 'msg' => 'to_bet_fail_5'];
        }
        if ($numberTabInfo['bet_status'] != 1 || time() > ($numberTabInfo['bet_start_time'] + $tableInfo['wait_time'])){
            return ['status' => false, 'msg' => 'to_bet_fail_4'];
        }
        // 超过50局幸运6禁止下注
        if ($numberTabInfo['number'] > 50 && isset($event['luck_six_amount']) && $event['luck_six_amount'] > 0){
            return ['status' => false, 'msg' => 'to_bet_fail_7'];
        }
        // 超过30局大小禁止下注
        if ($numberTabInfo['number'] > 30 && isset($event['big_amount']) && $event['big_amount'] > 0){
            return ['status' => false, 'msg' => 'to_bet_fail_8'];
        }
        if ($numberTabInfo['number'] > 30 && isset($event['small_amount']) && $event['small_amount'] > 0){
            return ['status' => false, 'msg' => 'to_bet_fail_8'];
        }
        if($userInfo['win_limit'] > 0){
            $winTotalToday = Bet::getWinTotalToday($userInfo['id']);
            if($winTotalToday >= $userInfo['win_limit']){
                return ['status' => false, 'msg' => 'win_limit_tip'];
            }
        }
        if ($userInfo['money'] < $amount){
            return ['status' => false, 'msg' => 'to_bet_fail_1'];
        }
        return ['status' => true, 'numberTabInfo' => $numberTabInfo, 'userInfo' => $userInfo];
        /** 通用检验重复部分结束 */
    }
}