<?php
namespace app\services\bet;

use app\models\bet\Bet;
use app\models\process\NumberTab;
use app\models\user\User;
use freedom\utils\SocketSession;

/**
 * TODO ToSeatService
 * Class ToSeatService
 * @package app\services\bet
 */
class ToSeatService
{
    /**
     * TODO 处理toSeat
     * @param array $event
     * @param array $tableInfo
     * @return void
     */
    public static function toSeat(array $event, array $tableInfo){
        $ws = app('\think\swoole\WebSocket');
        $fd = $ws->getSender();
        $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;
        $seatNum = isset($event['seat_num']) && intval($event['seat_num']) > 0 ? intval($event['seat_num']) : 0;
        $numberTabInfo = NumberTab::getByTableIdOrderByIdDesc($tableInfo);

        /** 验证是否和当前铺同一 */
        if ($numberTabInfo['id'] != $numberTabId){
            $ws->emit('toSeat',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_seat_fail_1']);
            SocketSession::resetRepeat($fd,'user','isToSeat');
            return;
        }

        $userInfo = User::get(['id' => $userId, 'status' => 1, 'is_delete' => 0]);
        /** 验证会员有效 */
        if (!$userInfo || $userInfo['bet_type'] != 2){
            $ws->emit('toSeat',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_seat_fail_2']);
            SocketSession::resetRepeat($fd,'user','isToSeat');
            return;
        }
        /** 验证座位号参数 */
        $table_seat_num = $tableInfo['seat_num'] >= 4 ? $tableInfo['seat_num'] + 1 : $tableInfo['seat_num'];
        if($seatNum < 1 || $seatNum > $table_seat_num){
            $ws->emit('toSeat',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_seat_fail_3']);
            SocketSession::resetRepeat($fd,'user','isToSeat');
            return;
        }

        $seatJson = $numberTabInfo['seat_json'];
        $seatArr = json_decode($seatJson,true);
        /** 验证座位是否有玩家 */
        if(isset($seatArr[$userInfo['manager_id']])){
            if($seatArr[$userInfo['manager_id']][$seatNum] && $seatArr[$userInfo['manager_id']][$seatNum] != $userId){
                $ws->emit('toSeat',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_seat_fail_4']);
                SocketSession::resetRepeat($fd,'user','isToSeat');
                return;
            }
        }
        $prevBetInfo = Bet::getPrevBetInfo($userInfo['id'],$numberTabInfo['id']);
        if($prevBetInfo && $prevBetInfo['seat_num'] != $seatNum){
            $ws->emit('toSeat',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_seat_fail_5']);
            SocketSession::resetRepeat($fd,'user','isToSeat');
            return;
        }

        $res = Bet::toSeat($userInfo,$numberTabInfo,$tableInfo,$seatNum);
        if ($res){
            $numberTabInfo = NumberTab::getByTableIdOrderByIdDesc($tableInfo);
            $table_seat_info = json_decode($numberTabInfo['seat_json'],true);
            $SeatMsg = ['user_id' => $userInfo['id'], 'username' => $userInfo['username'],'manager_id' => $userInfo['manager_id'],'seat_num' => $seatNum];
            $ws->to(SocketSession::HOUSE_NAME)->emit('toSeat',['status' => true, 'table_id' => $tableInfo['id'], 'SeatMsg' => $SeatMsg, 'table_seat_info' => $table_seat_info[$userInfo['manager_id']], 'msg' => 'to_seat_success']);
        }else{
            $ws->emit('toSeat',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_seat_fail']);
        }
        SocketSession::resetRepeat($fd,'user','isToSeat');
    }

}