<?php


namespace app\services\scan;

use freedom\utils\CardPosition;
use freedom\utils\RedisUtil;

/**
 * TODO Baccarat扫描
 * Class ScanBaccaratService
 * @package app\services\scan
 */
class ScanBaccaratService
{
    /**
     * TODO Baccarat扫描处理
     * @param array $event
     * @param array $tableInfo
     * @return array
     */
    public static function doScanBaccarat(array $event, array $tableInfo): array{
        $checkScanRes = ScanCommonService::checkScan($event,$tableInfo);
        if ($checkScanRes['status'] == false){
            return $checkScanRes;
        }else{
            $numberTabInfo = $checkScanRes['numberTabInfo'];
            $cardInfo = $checkScanRes['cardInfo'];
        }
        if (!isset($event['card']) || !isset($event['position'])) return ['status' => false, 'msg' => 'CardPosition Unable Distinguish'];
        $card = CardPosition::interchangeCard($event['card']);
        if (!$card) return ['status' => false, 'msg' => 'CardPosition Unable Distinguish'];
        $position = CardPosition::interchangePosition($event['position']);
        if (!$position) return ['status' => false, 'msg' => 'Position Error'];
        if (isset($cardInfo[$position]) && $cardInfo[$position] > 0 && $cardInfo[$position] != $event['card']) {
            if (!(isset($event['change']) && $event['change'] == 1)) {
                return ['status' => false, 'msg' => 'Already Exists'];
            }
            
        }

        //判断接受牌的先后顺序
        if($event['position'] == 11 || $event['position'] == 12 || $event['position'] == 21 || $event['position'] == 22){
            $returnPosition = intval($event['position']);
            $cardInfo[$position] = intval($event['card']);
            $res = RedisUtil::saveCardPosition($numberTabInfo['id'],$returnPosition,intval($event['card']));
            if (!$res) return ['status' => false, 'msg' => 'Redis Cache Save Error'];
        }else if($event['position'] == 13){
            $change = (isset($event['change']) && $event['change'] == 1)? 1 : 0;
            $returnData = self::baccaratReplenish($cardInfo,$event['position'], $change);
            if($returnData['status'] == true){
                if($returnData['position'] == 'banker_3'){
                    $returnPosition = 23;
                }else{
                    $returnPosition = 13;
                }
                $cardInfo[$returnData['position']] = intval($event['card']);
                $res = RedisUtil::saveCardPosition($numberTabInfo['id'],$returnPosition,intval($event['card']));
                if (!$res) return ['status' => false, 'msg' => 'Redis Cache Save Error'];
            }else{
                return ['status' => false, 'msg' => $returnData['msg']];
            }
        }else if($event['position'] == 23){
            $returnPosition = 23;
            if(isset($cardInfo['player_3']) && $cardInfo['player_3'] > 0){
                $change = (isset($event['change']) && $event['change'] == 1)? 1 : 0;
                $returnData = self::baccaratReplenish($cardInfo,$event['position'], $change);
                if($returnData['status'] == true){
                    $cardInfo[$returnData['position']] = intval($event['card']);
                    $res = RedisUtil::saveCardPosition($numberTabInfo['id'],$returnPosition,intval($event['card']));
                    if (!$res) return ['status' => false, 'msg' => 'Redis Cache Save Error'];
                }else{
                    return ['status' => false, 'msg' => $returnData['msg']];
                }
            }else{
                return ['status' => false, 'msg' => 'player3_is_not_cannot_scan_banker3'];
            }
        }else{
            return ['status' => false, 'msg' => 'position_error'];
        }
        $round = array(
            'tid' => intval($tableInfo['id']),
            'number_tab_id' => $numberTabInfo['id'],
            'card' => intval($event['card']),
            'number' => intval($card),
            'position' => $returnPosition,
            'boot_id' => intval($numberTabInfo['boot_id']),
            'boot_num' => intval($numberTabInfo['boot_num']),
            'number_tab_number' => intval($numberTabInfo['number']),
            'card_info' => $cardInfo
        );
    	if ($numberTabInfo['bet_status'] == 2) {
    		$round['is_scan'] = true;
    	} else {
    		$round['is_scan'] = false;
    	}
        return ['status' => true, 'data' => $round];
    }

    /**
     * TODO Baccarat开牌条件计算方法
     * @param array $cardInfo
     * @param int $position
     * @return array
     */
    public static function baccaratReplenish(array $cardInfo, int $position, int $change = 0): array
    {
        if (!isset($cardInfo['banker_1']) || !isset($cardInfo['banker_2']) || !isset($cardInfo['player_1']) || !isset($cardInfo['player_2'])){
            return array('status' => false, 'msg' => 'player_banker_not_full');
        }
        if($cardInfo['banker_1'] > 0 && $cardInfo['banker_2'] > 0 && $cardInfo['player_1'] > 0 && $cardInfo['player_2'] > 0){
            $player_count = (CardPosition::interchangeNumber(CardPosition::interchangeCard($cardInfo['player_1'])) + CardPosition::interchangeNumber(CardPosition::interchangeCard($cardInfo['player_2']))) % 10;
            $banker_count = (CardPosition::interchangeNumber(CardPosition::interchangeCard($cardInfo['banker_1'])) + CardPosition::interchangeNumber(CardPosition::interchangeCard($cardInfo['banker_2']))) % 10;
            if($position == 13){
                if(isset($cardInfo['player_3']) && $cardInfo['player_3'] > 0 && $change != 1){
                    //第1张增牌已经存在，不允许更改
                    return array('status' => false, 'msg' => 'fail_already_exists');
                }
                if($player_count == 8 || $player_count == 9){
                    //闲家8,9点，不允许增牌
                    return array('status' => false, 'msg' => 'fail_player_8_9');
                }
                if($banker_count == 8 || $banker_count == 9){
                    //庄家8,9点，不允许增牌
                    return array('status' => false, 'msg' => 'fail_banker_8_9');
                }
                if($player_count == 6 || $player_count == 7){
                    return self::baccaratReplenish($cardInfo,23);
                }
                return array('status' => true, 'position' => 'player_3', 'msg' => 'Success');
            }else if($position == 23){
                if(isset($cardInfo['banker_3']) && $cardInfo['banker_3'] > 0  && $change != 1){
                    //第2张增牌已经存在，不允许更改
                    return array('status' => false, 'msg' => 'fail_already_exists');
                }
                if($player_count == 8 || $player_count == 9){
                    //闲家8,9点，不允许增牌
                    return array('status' => false, 'msg' => 'fail_player_8_9');
                }
                if($banker_count == 8 || $banker_count == 9){
                    //庄家8,9点，不允许增牌
                    return array('status' => false, 'msg' => 'fail_banker_8_9');
                }
                if(isset($cardInfo['player_3']) && $cardInfo['player_3'] > 0){
                    //闲已经补牌
                    $player_3_count = (CardPosition::interchangeNumber(CardPosition::interchangeCard($cardInfo['player_3']))) % 10;
                    if($banker_count < 3 || ($banker_count == 3 && in_array($player_3_count,array(1,2,3,4,5,6,7,9,0))) || ($banker_count == 4 && in_array($player_3_count,array(2,3,4,5,6,7))) || ($banker_count == 5 && in_array($player_3_count,array(4,5,6,7))) || ($banker_count == 6 && in_array($player_3_count,array(6,7)))){
                        return array('status' => true, 'position' => 'banker_3', 'msg' => 'Success');
                    }else{
                        //庄家不符合补牌条件，不接受补牌数据
                        return array('status' => false, 'msg' => 'banker_lby_lfx');
                    }
                }else{
                    //闲不需要补牌
                    if($banker_count < 6){
                        return array('status' => true, 'position' => 'banker_3', 'msg' => 'Success');
                    }else{
                        //闲家不补牌，庄家点数>=6点，不接受补牌数据
                        return array('status' => false, 'msg' => 'player3_is_not_banker_num_egt_6');
                    }
                }
            }else{
                //定位错误
                return array('status' => false, 'msg' => 'position_error');
            }
        }else{
            //前面牌数据未全，不接受补牌数据
            return array('status' => false, 'msg' => 'player_banker_not_full');
        }
    }
}
