<?php


namespace app\services\connect;


use freedom\utils\CardPosition;
use freedom\utils\CardPositionNn;
use freedom\utils\CardPositionTc;
use freedom\utils\RedisUtil;

/**
 * TODO 登录成功卡片获取服务
 * Class GetCardService
 * @package app\services\connect
 */
class GetCardService
{

    /**
     * TODO 控制台登录处理
     * @param array $tableInfo
     * @param array $numberTabInfo
     * @return array
     */
    public static function getCard(array $tableInfo, array $numberTabInfo): array
    {
        if ($tableInfo['is_scavenging'] != 1 || $numberTabInfo['bet_status'] != 2) return [];
        $showCard = array();
        if ($tableInfo['game_id'] == 4){
            $cardInfo = RedisUtil::getCard($numberTabInfo['id']);
            if (!$cardInfo) return [];
            if($cardInfo['card_first'] > 0){
                $pushArray = array('card' => $cardInfo['card_first'], 'order_num' => '0');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_1_card_1'] > 0){
                $pushArray = array('card' => $cardInfo['player_1_card_1'], 'order_num' => '11');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_1_card_2'] > 0){
                $pushArray = array('card' => $cardInfo['player_1_card_2'], 'order_num' => '12');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_1_card_3'] > 0){
                $pushArray = array('card' => $cardInfo['player_1_card_3'], 'order_num' => '13');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_1_card_4'] > 0){
                $pushArray = array('card' => $cardInfo['player_1_card_4'], 'order_num' => '14');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_1_card_5'] > 0){
                $player1Card[0] = $cardInfo['player_1_card_1'];
                $player1Card[1] = $cardInfo['player_1_card_2'];
                $player1Card[2] = $cardInfo['player_1_card_3'];
                $player1Card[3] = $cardInfo['player_1_card_4'];
                $player1Card[4] = $cardInfo['player_1_card_5'];
                $player1Result = CardPositionNn::JudgeCowCow($player1Card);
                $pushArray = array('card' => $cardInfo['player_1_card_5'], 'order_num' => '15', 'result' => $player1Result['word']);
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_2_card_1'] > 0){
                $pushArray = array('card' => $cardInfo['player_2_card_1'], 'order_num' => '21');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_2_card_2'] > 0){
                $pushArray = array('card' => $cardInfo['player_2_card_2'], 'order_num' => '22');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_2_card_3'] > 0){
                $pushArray = array('card' => $cardInfo['player_2_card_3'], 'order_num' => '23');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_2_card_4'] > 0){
                $pushArray = array('card' => $cardInfo['player_2_card_4'], 'order_num' => '24');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_2_card_5'] > 0){
                $player2Card[0] = $cardInfo['player_2_card_1'];
                $player2Card[1] = $cardInfo['player_2_card_2'];
                $player2Card[2] = $cardInfo['player_2_card_3'];
                $player2Card[3] = $cardInfo['player_2_card_4'];
                $player2Card[4] = $cardInfo['player_2_card_5'];
                $player2Result = CardPositionNn::JudgeCowCow($player2Card);
                $pushArray = array('card' => $cardInfo['player_2_card_5'], 'order_num' => '25', 'result' => $player2Result['word']);
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_3_card_1'] > 0){
                $pushArray = array('card' => $cardInfo['player_3_card_1'], 'order_num' => '31');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_3_card_2'] > 0){
                $pushArray = array('card' => $cardInfo['player_3_card_2'], 'order_num' => '32');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_3_card_3'] > 0){
                $pushArray = array('card' => $cardInfo['player_3_card_3'], 'order_num' => '33');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_3_card_4'] > 0){
                $pushArray = array('card' => $cardInfo['player_3_card_4'], 'order_num' => '34');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_3_card_5'] > 0){
                $player3Card[0] = $cardInfo['player_3_card_1'];
                $player3Card[1] = $cardInfo['player_3_card_2'];
                $player3Card[2] = $cardInfo['player_3_card_3'];
                $player3Card[3] = $cardInfo['player_3_card_4'];
                $player3Card[4] = $cardInfo['player_3_card_5'];
                $player3Result = CardPositionNn::JudgeCowCow($player3Card);
                $pushArray = array('card' => $cardInfo['player_3_card_5'], 'order_num' => '35', 'result' => $player3Result['word']);
                array_push($showCard,$pushArray);
            }
            if($cardInfo['banker_card_1'] > 0){
                $pushArray = array('card' => $cardInfo['banker_card_1'], 'order_num' => '41');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['banker_card_2'] > 0){
                $pushArray = array('card' => $cardInfo['banker_card_2'], 'order_num' => '42');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['banker_card_3'] > 0){
                $pushArray = array('card' => $cardInfo['banker_card_3'], 'order_num' => '43');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['banker_card_4'] > 0){
                $pushArray = array('card' => $cardInfo['banker_card_4'], 'order_num' => '44');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['banker_card_5'] > 0){
                $bankerCard[0] = $cardInfo['banker_card_1'];
                $bankerCard[1] = $cardInfo['banker_card_2'];
                $bankerCard[2] = $cardInfo['banker_card_3'];
                $bankerCard[3] = $cardInfo['banker_card_4'];
                $bankerCard[4] = $cardInfo['banker_card_5'];
                $bankerResult = CardPositionNn::JudgeCowCow($bankerCard);
                $pushArray = array('card' => $cardInfo['banker_card_5'], 'order_num' => '45', 'result' => $bankerResult['word']);
                array_push($showCard,$pushArray);
            }
        }elseif($tableInfo['game_id'] == 5){
            $cardInfo = RedisUtil::getCard($numberTabInfo['id']);
            if (!$cardInfo) return [];
            if($cardInfo['card_first'] > 0){
                $pushArray = array('card' => $cardInfo['card_first'], 'order_num' => '0');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_1_card_1'] > 0){
                $pushArray = array('card' => $cardInfo['player_1_card_1'], 'order_num' => '11');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_1_card_2'] > 0){
                $pushArray = array('card' => $cardInfo['player_1_card_2'], 'order_num' => '12');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_1_card_3'] > 0){
                $player1Card[0] = $cardInfo['player_1_card_1'];
                $player1Card[1] = $cardInfo['player_1_card_2'];
                $player1Card[2] = $cardInfo['player_1_card_3'];
                $player1Result = CardPositionTc::ThredCardCowCow($player1Card);
                $pushArray = array('card' => $cardInfo['player_1_card_3'], 'order_num' => '13', 'result' => $player1Result['word']);
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_2_card_1'] > 0){
                $pushArray = array('card' => $cardInfo['player_2_card_1'], 'order_num' => '21');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_2_card_2'] > 0){
                $pushArray = array('card' => $cardInfo['player_2_card_2'], 'order_num' => '22');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_2_card_3'] > 0){
                $player2Card[0] = $cardInfo['player_2_card_1'];
                $player2Card[1] = $cardInfo['player_2_card_2'];
                $player2Card[2] = $cardInfo['player_2_card_3'];
                $player2Result = CardPositionTc::ThredCardCowCow($player2Card);
                $pushArray = array('card' => $cardInfo['player_2_card_3'], 'order_num' => '23', 'result' => $player2Result['word']);
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_3_card_1'] > 0){
                $pushArray = array('card' => $cardInfo['player_3_card_1'], 'order_num' => '31');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_3_card_2'] > 0){
                $pushArray = array('card' => $cardInfo['player_3_card_2'], 'order_num' => '32');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['player_3_card_3'] > 0){
                $player3Card[0] = $cardInfo['player_3_card_1'];
                $player3Card[1] = $cardInfo['player_3_card_2'];
                $player3Card[2] = $cardInfo['player_3_card_3'];
                $player3Result = CardPositionTc::ThredCardCowCow($player3Card);
                $pushArray = array('card' => $cardInfo['player_3_card_3'], 'order_num' => '33', 'result' => $player3Result['word']);
                array_push($showCard,$pushArray);
            }
            if($cardInfo['banker_card_1'] > 0){
                $pushArray = array('card' => $cardInfo['banker_card_1'], 'order_num' => '41');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['banker_card_2'] > 0){
                $pushArray = array('card' => $cardInfo['banker_card_2'], 'order_num' => '42');
                array_push($showCard,$pushArray);
            }
            if($cardInfo['banker_card_3'] > 0){
                $bankerCard[0] = $cardInfo['banker_card_1'];
                $bankerCard[1] = $cardInfo['banker_card_2'];
                $bankerCard[2] = $cardInfo['banker_card_3'];
                $bankerResult = CardPositionTc::ThredCardCowCow($bankerCard);
                $pushArray = array('card' => $cardInfo['banker_card_3'], 'order_num' => '43', 'result' => $bankerResult['word']);
                array_push($showCard,$pushArray);
            }
        }else{
            $cardInfo = RedisUtil::getCardPosition($numberTabInfo['id']);
            foreach ($cardInfo AS $k => $v){
                switch ($k){
                    case 'banker_1':
                        $showCard[] = ['position' => 21, 'card' => intval($v), 'number' => CardPosition::interchangeCard($v)];
                        break;
                    case 'banker_2':
                        $showCard[] = ['position' => 22, 'card' => intval($v), 'number' => CardPosition::interchangeCard($v)];
                        break;
                    case 'banker_3':
                        $showCard[] = ['position' => 23, 'card' => intval($v), 'number' => CardPosition::interchangeCard($v)];
                        break;
                    case 'player_1':
                        $showCard[] = ['position' => 11, 'card' => intval($v), 'number' => CardPosition::interchangeCard($v)];
                        break;
                    case 'player_2':
                        $showCard[] = ['position' => 12, 'card' => intval($v), 'number' => CardPosition::interchangeCard($v)];
                        break;
                    case 'player_3':
                        $showCard[] = ['position' => 13, 'card' => intval($v), 'number' => CardPosition::interchangeCard($v)];
                        break;
                }
            }
        }
        return $showCard;
    }
}