<?php


namespace app\services\bet;


use app\models\bet\Bet;
use freedom\utils\SocketSession;

/**
 * TODO ToBetDtService
 * Class ToBetDtService
 * @package app\services\bet
 */
class ToBetDtService
{
    /**
     * TODO 处理toBetDt
     * @param array $event
     * @param array $tableInfo
     * @return void
     */
    public static function toBetDt(array $event, array $tableInfo){
        $ws = app('\think\swoole\WebSocket');
        $fd = $ws->getSender();
        $data = array();
        $data['banker_amount'] = isset($event['banker_amount']) && intval($event['banker_amount']) > 0 ? intval($event['banker_amount']) : 0;
        $data['player_amount'] = isset($event['player_amount']) && intval($event['player_amount']) > 0 ? intval($event['player_amount']) : 0;
        $data['tie_amount'] = isset($event['tie_amount']) && intval($event['tie_amount']) > 0 ? intval($event['tie_amount']) : 0;
        $seat_num = isset($event['seat_num']) && intval($event['seat_num']) > 0 ? intval($event['seat_num']) : 0;

        $time = time();
        $toBetCheck = ToBetCommonService::toBetCheck($tableInfo,$event,$data);
        if ($toBetCheck['status'] == false){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => $toBetCheck['msg']]);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }
        $numberTabInfo = $toBetCheck['numberTabInfo'];
        $userInfo = $toBetCheck['userInfo'];
        $amount = array_sum($data);

        /***** User Limit Start *****/
        $prevBetInfo = Bet::getPrevBetInfo($userInfo['id'],$numberTabInfo['id']);
        $betTotalAmount = [];
        if($prevBetInfo){
            $betTotalAmount['banker_amount'] = $prevBetInfo['banker_amount'] + $data['banker_amount'];
            $betTotalAmount['player_amount'] = $prevBetInfo['player_amount'] + $data['player_amount'];
            $betTotalAmount['tie_amount'] = $prevBetInfo['tie_amount'] + $data['tie_amount'];
        }else{
            $betTotalAmount = $data;
        }
        if($betTotalAmount['banker_amount'] > $userInfo['limit_high'] || $betTotalAmount['player_amount'] > $userInfo['limit_high'] || $betTotalAmount['tie_amount'] > $userInfo['limit_high_tie']){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'exceeds_limit_user']);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }
        if(($betTotalAmount['banker_amount'] > 0 && $betTotalAmount['banker_amount'] < $userInfo['limit_low']) || ($betTotalAmount['player_amount'] > 0 && $betTotalAmount['player_amount'] < $userInfo['limit_low']) || ($betTotalAmount['tie_amount'] > 0 && $betTotalAmount['tie_amount'] < $userInfo['limit_low_tie'])){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'under_limit_user']);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }
        /***** User Limit End *****/

        /***** Table Limit Start *****/
        $limitMoneyArray = explode('-',$tableInfo['limit_money']);
        $limitMoneyTieArray = explode('-',$tableInfo['limit_money_tie']);
        $prevBetInfo = Bet::getPrevBetInfo($userInfo['id'],$numberTabInfo['id']);
        if($prevBetInfo){
            $totalBankerAmount = $prevBetInfo['banker_amount'] + $data['banker_amount'];
            $totalPlayerAmount = $prevBetInfo['player_amount'] + $data['player_amount'];
            $totalTieAmount = $prevBetInfo['tie_amount'] + $data['tie_amount'];
        }else{
            $totalBankerAmount = $data['banker_amount'];
            $totalPlayerAmount = $data['player_amount'];
            $totalTieAmount = $data['tie_amount'];
        }
        $curNumberTabBankerAmount = $numberTabInfo['banker_amount'] + $data['banker_amount'];
        $curNumberTabPlayerAmount = $numberTabInfo['player_amount'] + $data['player_amount'];
        $curNumberTabTieAmount = $numberTabInfo['tie_amount'] + $data['tie_amount'];
//
//        $difference = abs(round(($curNumberTabBankerAmount - $curNumberTabPlayerAmount),2));
//        if($difference > $limitMoneyArray[1] || $curNumberTabTieAmount > $limitMoneyTieArray[1]){
//            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'exceeds_limit_table']);
//            SocketSession::resetRepeat($fd,'user','isToBet');
//            return;
//        }

        if($totalBankerAmount > $limitMoneyArray[1] || $totalPlayerAmount > $limitMoneyArray[1] || $totalTieAmount > $limitMoneyTieArray[1]){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'exceeds_limit_table']);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }
        if(($totalBankerAmount > 0 && $totalBankerAmount < $limitMoneyArray[0]) || ($totalPlayerAmount > 0 && $totalPlayerAmount < $limitMoneyArray[0]) || ($totalTieAmount > 0 && $totalTieAmount < $limitMoneyTieArray[0])){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'under_limit_table']);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }

        //如果桌子设置了单口最高下注，则所有的下注不能超过桌子单口下注限红
        if(($tableInfo['limit_money_total'] > 0 && $curNumberTabBankerAmount > $tableInfo['limit_money_total']) || ($tableInfo['limit_money_total'] > 0 && $curNumberTabPlayerAmount > $tableInfo['limit_money_total']) || ($tableInfo['limit_money_tie_total'] > 0 && $curNumberTabTieAmount > $tableInfo['limit_money_tie_total']) ){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'exceeds_limit_table']);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }
        /***** Table Limit End *****/

        $res = Bet::toBet($tableInfo,$userInfo,$numberTabInfo,$prevBetInfo,$data,$seat_num,$betTotalAmount,$time,0);
        if($res){
            $ws->emit('toBet',['status' => true, 'table_id' => $tableInfo['id'], 'bet_amount_msg' => $betTotalAmount, 'money' => ($userInfo['money'] - $amount),'seat_num' => $seat_num, 'msg' => 'to_bet_success']);
            if($userInfo['bet_type'] == 2){
                $tableManager = app('swoole.table.manager');
                $managerSession = $tableManager->get((string) $userInfo['manager_id']);
                if ($managerSession && is_array($managerSession)){
                    $ws->setSender(0)->to($managerSession['fd'])->emit('toBet',['status' => true, 'table_id' => $tableInfo['id'], 'user_id' => $userInfo['id'], 'manager_id' => $userInfo['manager_id'] ,'bet_amount_msg' => $betTotalAmount, 'money' => ($userInfo['money'] - $amount),'seat_num' => $seat_num, 'msg' => 'to_bet_success']);
                }
            }
            $round = array(
                'banker_amount' => intval($numberTabInfo['banker_amount'] + $data['banker_amount']),
                'player_amount' => intval($numberTabInfo['player_amount'] + $data['player_amount']),
                'tie_amount' => intval($numberTabInfo['tie_amount'] + $data['tie_amount']),
                'amount_item' => array(
                    'banker_amount' => intval($data['banker_amount']),
                    'player_amount' => intval($data['player_amount']),
                    'tie_amount' => intval($data['tie_amount']),
                    'user_id' => $userInfo['id'],
                ),
            );
            $ws->to(SocketSession::HOUSE_NAME)->emit('allBetAmount',['status' => true, 'table_id' => $tableInfo['id'], 'round' => $round]);
        }else{
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_bet_fail']);
        }
        SocketSession::resetRepeat($fd,'user','isToBet');
    }
}