<?php


namespace app\services\bet;

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

/**
 * TODO ToBetToningService
 * Class ToBetToningService
 * @package app\services\bet
 */
class ToBetToningService
{
    /**
     * TODO 处理色碟下注
     * @param array $event
     * @param array $tableInfo
     * @return void
     */
    public static function toBetToning(array $event, array $tableInfo){
        $ws = app('\think\swoole\WebSocket');
        $fd = $ws->getSender();
        $data = array();
        $data['toning_zero'] = isset($event['toning_zero']) && intval($event['toning_zero']) > 0 ? intval($event['toning_zero']) : 0;
        $data['toning_four'] = isset($event['toning_four']) && intval($event['toning_four']) > 0 ? intval($event['toning_four']) : 0;
        $data['toning_one'] = isset($event['toning_one']) && intval($event['toning_one']) > 0 ? intval($event['toning_one']) : 0;
        $data['toning_three'] = isset($event['toning_three']) && intval($event['toning_three']) > 0 ? intval($event['toning_three']) : 0;
        $data['toning_big'] = isset($event['toning_big']) && intval($event['toning_big']) > 0 ? intval($event['toning_big']) : 0;
        $data['toning_small'] = isset($event['toning_small']) && intval($event['toning_small']) > 0 ? intval($event['toning_small']) : 0;
        $data['toning_singular'] = isset($event['toning_singular']) && intval($event['toning_singular']) > 0 ? intval($event['toning_singular']) : 0;
        $data['toning_plural'] = isset($event['toning_plural']) && intval($event['toning_plural']) > 0 ? intval($event['toning_plural']) : 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);

        $prevBetInfo = Bet::getPrevBetInfo($userInfo['id'],$numberTabInfo['id']);
        $betTotalAmount = [];
        if($prevBetInfo){
            $betArray = string_to_array($prevBetInfo['toning_amount']);
            $betTotalAmount['toning_zero'] = $betArray['toning_zero'] + $data['toning_zero'];
            $betTotalAmount['toning_four'] = $betArray['toning_four'] + $data['toning_four'];
            $betTotalAmount['toning_one'] = $betArray['toning_one'] + $data['toning_one'];
            $betTotalAmount['toning_three'] = $betArray['toning_three'] + $data['toning_three'];
            $betTotalAmount['toning_big'] = $betArray['toning_big'] + $data['toning_big'];
            $betTotalAmount['toning_small'] = $betArray['toning_small'] + $data['toning_small'];
            $betTotalAmount['toning_singular'] = $betArray['toning_singular'] + $data['toning_singular'];
            $betTotalAmount['toning_plural'] = $betArray['toning_plural'] + $data['toning_plural'];
        }else{
            $betTotalAmount = $data;
        }
        /***** User Limit Start *****/
        $totalAmount = array_sum($betTotalAmount);
        if ($totalAmount > $userInfo['limit_high']){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'exceeds_limit_user']);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }
        if ($totalAmount > 0 && $totalAmount < $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 *****/
        $totalAmount = array_sum($betTotalAmount);
        $limitMoneyArray = explode('-',$tableInfo['limit_money']);
        if($totalAmount > $limitMoneyArray[1]){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'exceeds_limit_table']);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }
        if(($totalAmount > 0 && $totalAmount < $limitMoneyArray[0]) ){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'under_limit_table']);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }
        $res = Bet::toBet($tableInfo,$userInfo,$numberTabInfo,$prevBetInfo,$data,$seat_num,$betTotalAmount,$time,0);
        $numberTabAmount = string_to_array($numberTabInfo['toning_amount']);
        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']);
            $round = array(
                'toning_zero' => isset($numberTabAmount['toning_zero']) ? intval($numberTabAmount['toning_zero'] + $data['toning_zero']) : $data['toning_zero'],
                'toning_four' => isset($numberTabAmount['toning_four']) ? intval($numberTabAmount['toning_four'] + $data['toning_four']) : $data['toning_four'],
                'toning_one' => isset($numberTabAmount['toning_one']) ? intval($numberTabAmount['toning_one'] + $data['toning_one']) : $data['toning_one'],
                'toning_three' => isset($numberTabAmount['toning_three']) ? intval($numberTabAmount['toning_three'] + $data['toning_three']) : $data['toning_three'],
                'toning_big' => isset($numberTabAmount['toning_big']) ? intval($numberTabAmount['toning_big'] + $data['toning_big']) : $data['toning_big'],
                'toning_small' => isset($numberTabAmount['toning_small']) ? intval($numberTabAmount['toning_small'] + $data['toning_small']) : $data['toning_small'],
                'toning_singular' => isset($numberTabAmount['toning_singular']) ? intval($numberTabAmount['toning_singular'] + $data['toning_singular']) : $data['toning_singular'],
                'toning_plural' => isset($numberTabAmount['toning_plural']) ? intval($numberTabAmount['toning_plural'] + $data['toning_plural']) : $data['toning_plural'],
                'amount_item' => array(
                    'toning_zero' => $data['toning_zero'],
                    'toning_four' => $data['toning_four'],
                    'toning_one' => $data['toning_one'],
                    'toning_three' => $data['toning_three'],
                    'toning_big' => $data['toning_big'],
                    'toning_small' => $data['toning_small'],
                    'toning_singular' => $data['toning_singular'],
                    'toning_plural' => $data['toning_plural'],
                    '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');
    }
}