<?php


namespace app\services\bet;

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

/**
 * TODO ToBetBaccaratService
 * Class ToBetBaccaratService
 * @package app\services\bet
 */
class ToBetBaccaratService
{
    /**
     * TODO 处理toBetBaccarat
     * @param array $event
     * @param array $tableInfo
     * @return void
     */
    public static function toBetBaccarat(array $event, array $tableInfo){
        $ws = app('\think\swoole\WebSocket');
        $fd = $ws->getSender();
        $baccaratType = intval($event['baccarat_type']);
        $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;
        $data['banker_pair_amount'] = isset($event['banker_pair_amount']) && intval($event['banker_pair_amount']) > 0 ? intval($event['banker_pair_amount']) : 0;
        $data['player_pair_amount'] = isset($event['player_pair_amount']) && intval($event['player_pair_amount']) > 0 ? intval($event['player_pair_amount']) : 0;
        $data['luck_six_amount'] = isset($event['luck_six_amount']) && intval($event['luck_six_amount']) > 0 ? intval($event['luck_six_amount']) : 0;
        $data['big_amount'] = isset($event['big_amount']) && intval($event['big_amount']) > 0 ? intval($event['big_amount']) : 0;
        $data['small_amount'] = isset($event['small_amount']) && intval($event['small_amount']) > 0 ? intval($event['small_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'];
            $betTotalAmount['banker_pair_amount'] = $prevBetInfo['banker_pair_amount'] + $data['banker_pair_amount'];
            $betTotalAmount['player_pair_amount'] = $prevBetInfo['player_pair_amount'] + $data['player_pair_amount'];
            $betTotalAmount['luck_six_amount'] = $prevBetInfo['luck_six_amount'] + $data['luck_six_amount'];
            $betTotalAmount['big_amount'] = $prevBetInfo['big_amount'] + $data['big_amount'];
            $betTotalAmount['small_amount'] = $prevBetInfo['small_amount'] + $data['small_amount'];
        }else{
            $betTotalAmount = $data;
        }
        foreach ($betTotalAmount AS $value){
            if ($value > $userInfo['limit_high']){
                $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'exceeds_limit_user']);
                SocketSession::resetRepeat($fd,'user','isToBet');
                return;
            }
            if ($value > 0 && $value < $userInfo['limit_low']){
                $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']);
        $limitMoneyPairArray = explode('-',$tableInfo['limit_money_pair']);
        $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'];
            $totalBPAmount = $prevBetInfo['banker_pair_amount'] + $data['banker_pair_amount'];
            $totalPPAmount = $prevBetInfo['player_pair_amount'] + $data['player_pair_amount'];
        }else{
            $totalBankerAmount = $data['banker_amount'];
            $totalPlayerAmount = $data['player_amount'];
            $totalTieAmount = $data['tie_amount'];
            $totalBPAmount = $data['banker_pair_amount'];
            $totalPPAmount = $data['player_pair_amount'];
        }
        $curNumberTabBankerAmount = $numberTabInfo['banker_amount'] + $data['banker_amount'];
        $curNumberTabPlayerAmount = $numberTabInfo['player_amount'] + $data['player_amount'];
        $curNumberTabTieAmount = $numberTabInfo['tie_amount'] + $data['tie_amount'];
        $curNumberTabBPAmount = $numberTabInfo['banker_pair_amount'] + $data['banker_pair_amount'];
        $curNumberTabPPAmount = $numberTabInfo['player_pair_amount'] + $data['player_pair_amount'];

        //对冲
//        $difference = abs(round(($curNumberTabBankerAmount - $curNumberTabPlayerAmount),2));
//        if($difference > $limitMoneyArray[1] || $curNumberTabTieAmount > $limitMoneyTieArray[1] || $curNumberTabBPAmount > $limitMoneyPairArray[1] || $curNumberTabPPAmount > $limitMoneyPairArray[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] || $totalBPAmount > $limitMoneyPairArray[1] || $totalPPAmount >  $limitMoneyPairArray[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]) || ($totalBPAmount > 0 && $totalBPAmount < $limitMoneyPairArray[0]) || ($totalPPAmount > 0 && $totalPPAmount < $limitMoneyPairArray[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']) || ($tableInfo['limit_money_pair_total'] > 0 && $curNumberTabBPAmount > $tableInfo['limit_money_pair_total']) || ($tableInfo['limit_money_pair_total'] > 0 && $curNumberTabPPAmount > $tableInfo['limit_money_pair_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,$baccaratType);
        if($res){
            $ws->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']);
            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']),
                'banker_pair_amount' => intval($numberTabInfo['banker_pair_amount'] + $data['banker_pair_amount']),
                'player_pair_amount' => intval($numberTabInfo['player_pair_amount'] + $data['player_pair_amount']),
                'luck_six_amount' => intval($numberTabInfo['luck_six_amount'] + $data['luck_six_amount']),
                'big_amount' => intval($numberTabInfo['big_amount'] + $data['big_amount']),
                'small_amount' => intval($numberTabInfo['small_amount'] + $data['small_amount']),
                'amount_item' => array(
                    'banker_amount' => intval($data['banker_amount']),
                    'player_amount' => intval($data['player_amount']),
                    'tie_amount' => intval($data['tie_amount']),
                    'banker_pair_amount' => intval($data['banker_pair_amount']),
                    'player_pair_amount' => intval($data['player_pair_amount']),
                    'luck_six_amount' => intval($data['luck_six_amount']),
                    'big_amount' => intval($data['big_amount']),
                    'small_amount' => intval($data['small_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');
    }
}
