<?php


namespace app\services\bet;

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

/**
 * TODO ToBetTcService
 * Class ToBetTcService
 * @package app\services\bet
 */
class ToBetTcService
{
    /**
     * TODO 处理toBetTc
     * @param array $event
     * @param array $tableInfo
     * @return void
     */
    public static function toBetTc(array $event, array $tableInfo){
        $ws = app('\think\swoole\WebSocket');
        $fd = $ws->getSender();
        $data = array();

        $data['amount_player_1'] = isset($event['amount_player_1']) && intval($event['amount_player_1']) > 0 ? intval($event['amount_player_1']) : 0;
        $data['amount_player_1_times'] = isset($event['amount_player_1_times']) && intval($event['amount_player_1_times']) > 0 ? intval($event['amount_player_1_times']) : 0;
        $data['withhold_player_1_times'] = $data['amount_player_1_times'] * 19;
        $data['amount_player_1_banker'] = isset($event['amount_player_1_banker']) && intval($event['amount_player_1_banker']) > 0 ? intval($event['amount_player_1_banker']) : 0;
        $data['amount_player_1_banker_times'] = isset($event['amount_player_1_banker_times']) && intval($event['amount_player_1_banker_times']) > 0 ? intval($event['amount_player_1_banker_times']) : 0;
        $data['withhold_player_1_banker_times'] = $data['amount_player_1_banker_times'] * 19;

        $data['amount_player_2'] = isset($event['amount_player_2']) && intval($event['amount_player_2']) > 0 ? intval($event['amount_player_2']) : 0;
        $data['amount_player_2_times'] = isset($event['amount_player_2_times']) && intval($event['amount_player_2_times']) > 0 ? intval($event['amount_player_2_times']) : 0;
        $data['withhold_player_2_times'] = $data['amount_player_2_times'] * 19;
        $data['amount_player_2_banker'] = isset($event['amount_player_2_banker']) && intval($event['amount_player_2_banker']) > 0 ? intval($event['amount_player_2_banker']) : 0;
        $data['amount_player_2_banker_times'] = isset($event['amount_player_2_banker_times']) && intval($event['amount_player_2_banker_times']) > 0 ? intval($event['amount_player_2_banker_times']) : 0;
        $data['withhold_player_2_banker_times'] = $data['amount_player_2_banker_times'] * 19;

        $data['amount_player_3'] = isset($event['amount_player_3']) && intval($event['amount_player_3']) > 0 ? intval($event['amount_player_3']) : 0;
        $data['amount_player_3_times'] = isset($event['amount_player_3_times']) && intval($event['amount_player_3_times']) > 0 ? intval($event['amount_player_3_times']) : 0;
        $data['withhold_player_3_times'] = $data['amount_player_3_times'] * 19;
        $data['amount_player_3_banker'] = isset($event['amount_player_3_banker']) && intval($event['amount_player_3_banker']) > 0 ? intval($event['amount_player_3_banker']) : 0;
        $data['amount_player_3_banker_times'] = isset($event['amount_player_3_banker_times']) && intval($event['amount_player_3_banker_times']) > 0 ? intval($event['amount_player_3_banker_times']) : 0;
        $data['withhold_player_3_banker_times'] = $data['amount_player_3_banker_times'] * 19;

        $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['amount_player_1'] = $prevBetInfo['amount_player_1'] + $data['amount_player_1'];
            $betTotalAmount['amount_player_2'] = $prevBetInfo['amount_player_2'] + $data['amount_player_2'];
            $betTotalAmount['amount_player_3'] = $prevBetInfo['amount_player_3'] + $data['amount_player_3'];

            $betTotalAmount['amount_player_1_times'] = $prevBetInfo['amount_player_1_times'] + $data['amount_player_1_times'];
            $betTotalAmount['amount_player_2_times'] = $prevBetInfo['amount_player_2_times'] + $data['amount_player_2_times'];
            $betTotalAmount['amount_player_3_times'] = $prevBetInfo['amount_player_3_times'] + $data['amount_player_3_times'];

            $betTotalAmount['withhold_player_1_times'] = $prevBetInfo['withhold_player_1_times'] + $data['withhold_player_1_times'];
            $betTotalAmount['withhold_player_2_times'] = $prevBetInfo['withhold_player_2_times'] + $data['withhold_player_2_times'];
            $betTotalAmount['withhold_player_3_times'] = $prevBetInfo['withhold_player_3_times'] + $data['withhold_player_3_times'];

            $betTotalAmount['amount_player_1_banker'] = $prevBetInfo['amount_player_1_banker'] + $data['amount_player_1_banker'];
            $betTotalAmount['amount_player_2_banker'] = $prevBetInfo['amount_player_2_banker'] + $data['amount_player_2_banker'];
            $betTotalAmount['amount_player_3_banker'] = $prevBetInfo['amount_player_3_banker'] + $data['amount_player_3_banker'];

            $betTotalAmount['amount_player_1_banker_times'] = $prevBetInfo['amount_player_1_banker_times'] + $data['amount_player_1_banker_times'];
            $betTotalAmount['amount_player_2_banker_times'] = $prevBetInfo['amount_player_2_banker_times'] + $data['amount_player_2_banker_times'];
            $betTotalAmount['amount_player_3_banker_times'] = $prevBetInfo['amount_player_3_banker_times'] + $data['amount_player_3_banker_times'];

            $betTotalAmount['withhold_player_1_banker_times'] = $prevBetInfo['withhold_player_1_banker_times'] + $data['withhold_player_1_banker_times'];
            $betTotalAmount['withhold_player_2_banker_times'] = $prevBetInfo['withhold_player_2_banker_times'] + $data['withhold_player_2_banker_times'];
            $betTotalAmount['withhold_player_3_banker_times'] = $prevBetInfo['withhold_player_3_banker_times'] + $data['withhold_player_3_banker_times'];
        }else{
            $betTotalAmount = $data;
        }
        $withholdKeys = [
            'withhold_player_1_times',
            'withhold_player_2_times',
            'withhold_player_3_times',
            'withhold_player_1_banker_times',
            'withhold_player_2_banker_times',
            'withhold_player_3_banker_times',
        ];
        foreach ($betTotalAmount AS $k => $v){
            $nnLimitHigh = ($userInfo['limit_high_nn'] > 0) ? $userInfo['limit_high_nn'] : $userInfo['limit_high'];
            if ($v > $nnLimitHigh  && !in_array($k,$withholdKeys)){
                $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'exceeds_limit_user']);
                SocketSession::resetRepeat($fd,'user','isToBet');
                return;
            }
        }
        foreach ($betTotalAmount AS $k => $v){
            $nnLimitLow = ($userInfo['limit_low_nn'] > 0) ? $userInfo['limit_low_nn'] : $userInfo['limit_low'];
            if ($v > 0 && $v < $nnLimitLow  && !in_array($k,$withholdKeys)){
                $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']);
        $prevBetInfo = Bet::getPrevBetInfo($userInfo['id'],$numberTabInfo['id']);
        if($prevBetInfo){
            $total_player_1 = $prevBetInfo['amount_player_1'] + $data['amount_player_1'];
            $total_player_2 = $prevBetInfo['amount_player_2'] + $data['amount_player_2'];
            $total_player_3 = $prevBetInfo['amount_player_3'] + $data['amount_player_3'];

            $total_player_1_times = $prevBetInfo['amount_player_1_times'] + $data['amount_player_1_times'];
            $total_player_2_times = $prevBetInfo['amount_player_2_times'] + $data['amount_player_2_times'];
            $total_player_3_times = $prevBetInfo['amount_player_3_times'] + $data['amount_player_3_times'];

            $total_player_1_withhold = $prevBetInfo['withhold_player_1_times'] + $data['withhold_player_1_times'];
            $total_player_2_withhold = $prevBetInfo['withhold_player_2_times'] + $data['withhold_player_2_times'];
            $total_player_3_withhold = $prevBetInfo['withhold_player_3_times'] + $data['withhold_player_3_times'];

            $total_player_1_banker = $prevBetInfo['amount_player_1_banker'] + $data['amount_player_1_banker'];
            $total_player_2_banker = $prevBetInfo['amount_player_2_banker'] + $data['amount_player_2_banker'];
            $total_player_3_banker = $prevBetInfo['amount_player_3_banker'] + $data['amount_player_3_banker'];

            $total_player_1_banker_times = $prevBetInfo['amount_player_1_banker_times'] + $data['amount_player_1_banker_times'];
            $total_player_2_banker_times = $prevBetInfo['amount_player_2_banker_times'] + $data['amount_player_2_banker_times'];
            $total_player_3_banker_times = $prevBetInfo['amount_player_3_banker_times'] + $data['amount_player_3_banker_times'];

            $total_player_1_banker_times_withhold = $prevBetInfo['withhold_player_1_banker_times'] + $data['withhold_player_1_banker_times'];
            $total_player_2_banker_times_withhold = $prevBetInfo['withhold_player_2_banker_times'] + $data['withhold_player_2_banker_times'];
            $total_player_3_banker_times_withhold = $prevBetInfo['withhold_player_3_banker_times'] + $data['withhold_player_3_banker_times'];

        }else{
            $total_player_1 = $data['amount_player_1'];
            $total_player_2 = $data['amount_player_2'];
            $total_player_3 = $data['amount_player_3'];

            $total_player_1_times = $data['amount_player_1_times'];
            $total_player_2_times = $data['amount_player_2_times'];
            $total_player_3_times = $data['amount_player_3_times'];

            $total_player_1_withhold = $data['withhold_player_1_times'];
            $total_player_2_withhold = $data['withhold_player_2_times'];
            $total_player_3_withhold = $data['withhold_player_3_times'];

            $total_player_1_banker = $data['amount_player_1_banker'];
            $total_player_2_banker = $data['amount_player_2_banker'];
            $total_player_3_banker = $data['amount_player_3_banker'];

            $total_player_1_banker_times = $data['amount_player_1_banker_times'];
            $total_player_2_banker_times = $data['amount_player_2_banker_times'];
            $total_player_3_banker_times = $data['amount_player_3_banker_times'];

            $total_player_1_banker_times_withhold = $data['withhold_player_1_banker_times'];
            $total_player_2_banker_times_withhold = $data['withhold_player_2_banker_times'];
            $total_player_3_banker_times_withhold =  $data['withhold_player_3_banker_times'];
        }

        if($total_player_1 > $limitMoneyArray[1] || $total_player_2 > $limitMoneyArray[1] || $total_player_3 > $limitMoneyArray[1] || $total_player_1_times > $limitMoneyArray[1] || $total_player_2_times > $limitMoneyArray[1] || $total_player_3_times > $limitMoneyArray[1] || $total_player_1_banker > $limitMoneyArray[1] || $total_player_2_banker > $limitMoneyArray[1] || $total_player_3_banker > $limitMoneyArray[1] || $total_player_1_banker_times > $limitMoneyArray[1] || $total_player_2_banker_times > $limitMoneyArray[1] || $total_player_3_banker_times > $limitMoneyArray[1]){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'exceeds_limit_table']);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }
        if(($total_player_1 > 0 && $total_player_1 < $limitMoneyArray[0]) || ($total_player_2 > 0 && $total_player_2 < $limitMoneyArray[0]) || ($total_player_3 > 0 && $total_player_3 < $limitMoneyArray[0]) || ($total_player_1_times > 0 && $total_player_1_times < $limitMoneyArray[0]) || ($total_player_2_times > 0 && $total_player_2_times < $limitMoneyArray[0]) || ($total_player_3_times > 0 && $total_player_3_times < $limitMoneyArray[0]) || ($total_player_1_banker > 0 && $total_player_1_banker < $limitMoneyArray[0]) || ($total_player_2_banker > 0 && $total_player_2_banker < $limitMoneyArray[0]) || ($total_player_3_banker > 0 && $total_player_3_banker < $limitMoneyArray[0]) || ($total_player_1_banker_times > 0 && $total_player_1_banker_times < $limitMoneyArray[0]) || ($total_player_2_banker_times > 0 && $total_player_2_banker_times < $limitMoneyArray[0]) || ($total_player_3_banker_times > 0 && $total_player_3_banker_times < $limitMoneyArray[0]) ){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'under_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']);
            // 待确定是否推送即时彩池，现不推送
        }else{
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_bet_fail']);
        }
        SocketSession::resetRepeat($fd,'user','isToBet');
    }
}