<?php


namespace app\services\scan;

use app\models\process\NumberTab;
use freedom\utils\CardPosition;
use freedom\utils\CardPositionNn;
use freedom\utils\CardPositionTc;
use freedom\utils\RedisUtil;

/**
 * TODO TC扫描
 * Class ScanTcService
 * @package app\services\scan
 */
class ScanTcService
{
    /**
     * TODO Tc扫描处理
     * @param array $event
     * @param array $tableInfo
     * @return array
     */
    public static function doScanTc(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']) || empty(intval($event['card']))) return ['status' => false, 'msg' => 'CardPosition Unable Distinguish'];
        $card = intval($event['card']);
        $round = [
            'card' => $card,
            'number' => CardPosition::interchangeCard($card),
            'boot_id' => intval($numberTabInfo['boot_id']),
            'boot_num' => intval($numberTabInfo['boot_num']),
            'number_tab_id' => intval($numberTabInfo['id']),
            'number_tab_number' => intval($numberTabInfo['number'])
        ];
        if (!$cardInfo){
            // 没有牌数据，创建并保存
            $cardInfo = ['card_first' => $card];
            $cardInfo['player_1_card_1'] = 0;
            $cardInfo['player_1_card_2'] = 0;
            $cardInfo['player_1_card_3'] = 0;
            $cardInfo['player_2_card_1'] = 0;
            $cardInfo['player_2_card_2'] = 0;
            $cardInfo['player_2_card_3'] = 0;
            $cardInfo['player_3_card_1'] = 0;
            $cardInfo['player_3_card_2'] = 0;
            $cardInfo['player_3_card_3'] = 0;
            $cardInfo['banker_card_1'] = 0;
            $cardInfo['banker_card_2'] = 0;
            $cardInfo['banker_card_3'] = 0;
            $positionFirst = CardPositionNn::nnPosition($card);
            $cardInfo['position_first'] = $positionFirst;
            NumberTab::where(['id' => $numberTabInfo['id']])->update(['position_first' => $positionFirst]);
            RedisUtil::saveCard($numberTabInfo['id'],$cardInfo);
            $round['tid'] = $tableInfo['id'];
            $round['number_tab_id'] = $numberTabInfo['id'];
            $round['position'] = 0;
            $round['order_num'] = 0;
            $round['result'] = '';
            $round['card_info'] = $cardInfo;
            if ($numberTabInfo['bet_status'] == 2) {
                $round['is_scan'] = true;
            } else {
                $round['is_scan'] = false;
            }
            return ['status' => true, 'msg' => 'Scan Success', 'data' => $round];
        }
        // 判断是否已经存在过相同牌
        $beforeCard = $cardInfo;
        //unset($beforeCard['card_first']);
        unset($beforeCard['position_first']);
        if(in_array($card,$beforeCard)) return ['status' => false, 'msg' => 'Card Exists'];
        $cardCountValues = array_count_values($beforeCard);
        // 获取当前定位
        if (isset($cardCountValues[0])){
            $positionNum = 12 - intval($cardCountValues[0]) + 1;
        } else {
            $positionNum = 12;
        }
        $player1Card[0] = $cardInfo['player_1_card_1'];
        $player1Card[1] = $cardInfo['player_1_card_2'];
        $player2Card[0] = $cardInfo['player_2_card_1'];
        $player2Card[1] = $cardInfo['player_2_card_2'];
        $player3Card[0] = $cardInfo['player_3_card_1'];
        $player3Card[1] = $cardInfo['player_3_card_2'];
        $bankerCard[0] = $cardInfo['banker_card_1'];
        $bankerCard[1] = $cardInfo['banker_card_2'];
        $resultWord = '';

        $order = CardPositionTc::tcOrder($cardInfo['position_first']);
        $orderPosition = $positionNum - 1;
        $position = $order[$orderPosition];
        $orderNumArr = CardPositionTc::tcOrderNum($cardInfo['position_first']);
        $orderNum = $orderNumArr[$orderPosition];
        $cardInfo[$position] = $card;
        if($positionNum >= 9){
            if($orderNum > 10 && $orderNum < 20){
                $player1Card[2] = $card;
                $player1Result = CardPositionTc::ThredCardCowCow($player1Card);
                $resultWord = $player1Result['word'];
            }
            if($orderNum > 20 && $orderNum < 30){
                $player2Card[2] = $card;
                $player2Result = CardPositionTc::ThredCardCowCow($player2Card);
                $resultWord = $player2Result['word'];
            }
            if($orderNum > 30 && $orderNum < 40){
                $player3Card[2] = $card;
                $player3Result = CardPositionTc::ThredCardCowCow($player3Card);
                $resultWord = $player3Result['word'];
            }
            if($orderNum > 40){
                $bankerCard[2] = $card;
                $bankerResult = CardPositionTc::ThredCardCowCow($bankerCard);
                $resultWord = $bankerResult['word'];
            }
        }
        RedisUtil::saveCard($numberTabInfo['id'],$cardInfo);
        $round['tid'] = $tableInfo['id'];
        $round['number_tab_id'] = $numberTabInfo['id'];
        $round['position'] = $positionNum;
        $round['order_num'] = $orderNum;
        $round['result'] = $resultWord;
        $round['card_info'] = $cardInfo;
        if ($numberTabInfo['bet_status'] == 2) {
            $round['is_scan'] = true;
        } else {
            $round['is_scan'] = false;
        }
        return ['status' => true, 'msg' => 'Scan Success', 'data' => $round];
    }
}