<?php


namespace freedom\utils;

/**
 * TODO 卡牌工具类(TC)
 * Class CardPositionTc
 * @package freedom\utils
 */
class CardPositionTc
{
    /**
     * TODO 获取倍数
     * @param int $id 卡牌
     * @return int;
     */
    public static function getTimes(int $id): int
    {
        if($id == 1){
            return 1;
        }elseif($id == 2){
            return 2;
        }elseif($id == 3){
            return 3;
        }elseif($id == 4){
            return 4;
        }elseif($id == 5){
            return 5;
        }elseif($id == 6){
            return 6;
        }elseif($id == 7){
            return 7;
        }elseif($id == 8){
            return 8;
        }elseif($id == 9){
            return 9;
        }elseif($id == 10){
            return 10;
        }elseif($id == 11){
            return 12;
        }elseif($id == 12){
            return 15;
        }elseif($id == 13){
            return 20;
        }
        return 0;
    }

    /**
     * TODO 发牌顺序
     * @param int $id 卡牌
     * @return array;
     */
   public static function tcOrder(int $id): array
    {
        $order = array();
        //庄一先发牌
        if($id == 1){
            $order[0]="player_1_card_1";
            $order[1]="player_2_card_1";
            $order[2]="player_3_card_1";
            $order[3]="banker_card_1";
            $order[4]="player_1_card_2";
            $order[5]="player_2_card_2";
            $order[6]="player_3_card_2";
            $order[7]="banker_card_2";
            $order[8]="player_1_card_3";
            $order[9]="player_2_card_3";
            $order[10]="player_3_card_3";
            $order[11]="banker_card_3";
        }
        if($id == 2){
            $order[0]="player_2_card_1";
            $order[1]="player_3_card_1";
            $order[2]="banker_card_1";
            $order[3]="player_1_card_1";
            $order[4]="player_2_card_2";
            $order[5]="player_3_card_2";
            $order[6]="banker_card_2";
            $order[7]="player_1_card_2";
            $order[8]="player_2_card_3";
            $order[9]="player_3_card_3";
            $order[10]="banker_card_3";
            $order[11]="player_1_card_3";
        }
        if($id == 3){
            $order[0]="player_3_card_1";
            $order[1]="banker_card_1";
            $order[2]="player_1_card_1";
            $order[3]="player_2_card_1";
            $order[4]="player_3_card_2";
            $order[5]="banker_card_2";
            $order[6]="player_1_card_2";
            $order[7]="player_2_card_2";
            $order[8]="player_3_card_3";
            $order[9]="banker_card_3";
            $order[10]="player_1_card_3";
            $order[11]="player_2_card_3";
        }
        if($id == 4){
            $order[0]="banker_card_1";
            $order[1]="player_1_card_1";
            $order[2]="player_2_card_1";
            $order[3]="player_3_card_1";
            $order[4]="banker_card_2";
            $order[5]="player_1_card_2";
            $order[6]="player_2_card_2";
            $order[7]="player_3_card_2";
            $order[8]="banker_card_3";
            $order[9]="player_1_card_3";
            $order[10]="player_2_card_3";
            $order[11]="player_3_card_3";
        }
        return $order;
    }

    /**
     * TODO 发牌顺序
     * @param int $id 卡牌
     * @return array;
     */
    public static function tcOrderNum(int $id): array
    {
        $orderNum = array();
        //庄一先发牌
        if($id == 1){
            $orderNum[0]="11";
            $orderNum[1]="21";
            $orderNum[2]="31";
            $orderNum[3]="41";
            $orderNum[4]="12";
            $orderNum[5]="22";
            $orderNum[6]="32";
            $orderNum[7]="42";
            $orderNum[8]="13";
            $orderNum[9]="23";
            $orderNum[10]="33";
            $orderNum[11]="43";
        }
        if($id == 2){
            $orderNum[0]="21";
            $orderNum[1]="31";
            $orderNum[2]="41";
            $orderNum[3]="11";
            $orderNum[4]="22";
            $orderNum[5]="32";
            $orderNum[6]="42";
            $orderNum[7]="12";
            $orderNum[8]="23";
            $orderNum[9]="33";
            $orderNum[10]="43";
            $orderNum[11]="13";
        }
        if($id == 3){
            $orderNum[0]="31";
            $orderNum[1]="41";
            $orderNum[2]="11";
            $orderNum[3]="21";
            $orderNum[4]="32";
            $orderNum[5]="42";
            $orderNum[6]="12";
            $orderNum[7]="22";
            $orderNum[8]="33";
            $orderNum[9]="43";
            $orderNum[10]="13";
            $orderNum[11]="23";
        }
        if($id == 4){
            $orderNum[0]="41";
            $orderNum[1]="11";
            $orderNum[2]="21";
            $orderNum[3]="31";
            $orderNum[4]="42";
            $orderNum[5]="12";
            $orderNum[6]="22";
            $orderNum[7]="32";
            $orderNum[8]="43";
            $orderNum[9]="13";
            $orderNum[10]="23";
            $orderNum[11]="33";
        }
        return $orderNum;
    }

    /**
     * TODO tc结果
     * @param array $card 卡牌
     * @return array ;
     */
    public static function  ThredCardCowCow(array $card): array
    {
        $cardType = [];
        if(!is_array($card) || count($card) !== 3){
            return false;
        }
        //获取三张牌中最大的牌
        $compare = $card;
        for($i=0;$i<2;$i++){
            for($j=$i+1;$j<3;$j++){
                if(substr($compare[$i],1,2) < substr($compare[$j],1,2)){
                    $a = $compare[$i];
                    $compare[$i] = $compare[$j];
                    $compare[$j]=$a;
                }
            }
        }
        $maxCard = substr($compare['0'],1,2);

        if(substr($maxCard,0,1) == 0){
            $maxCard = substr($maxCard,1,1);
        }
        $sameNum = 0;
        for($i=0;$i<3; $i++){
            $cardType[$i] = substr($card[$i],0,1);
            $card[$i] = substr($card[$i],1,2);
            if(substr($card[$i],0,1) == 0){
                $card[$i] = substr($card[$i],1,1);
            }
            if($card[$i] == $maxCard){
                $sameNum++;
            }
        }

        //特殊牌型最大牌判断
        $spaceNum = array_count_values($card);
        if($sameNum == 1){
            $max = $compare['0'];
        }
        if($sameNum > 1){
            $compare = array_slice($compare,0,$sameNum);
            for($i=0;$i<$sameNum-1;$i++){//对5张牌从大到小排序。
                for($j=$i+1;$j<$sameNum;$j++){
                    if($compare[$i] > $compare[$j]){
                        $a = $compare[$i];
                        $compare[$i] = $compare[$j];
                        $compare[$j]=$a;
                    }
                }
            }
            $max = $compare['0'];
            if($sameNum == 2){
                if($compare['0']>300 && $compare['0']<400){
                    $max = $compare['1'];
                }
            }
        }

        $result = array();
        //豹子判断
        if(count($card) != count(array_unique($card))) {
            if (count(array_unique($card)) == 1) {
                $cow = 11;
                $word = '豹子';
                if (array_key_exists(1, $spaceNum)) {
                    if ($spaceNum['1'] > 0) {
                        if ($spaceNum['1'] == 1) {
                            $max = $compare[2];
                        }
                        if ($spaceNum['1'] == 2) {
                            if ($compare[1] > 300 && $compare[1] < 400) {
                                $max = $compare[2];
                            } else {
                                $max = $compare[1];
                            }
                        }
                    }
                }
                $result['word'] = $word;
                $result['cow'] = $cow;
                $result['max'] = $max;
                return $result;
            }
        }

        if(count(array_unique($cardType)) == 1){
            //皇家同花顺
            $example = array(1,12,13);
            $diffResult=array_diff($card,$example);
            if(count($diffResult) == 0){
                $cow = 13;
                $word = '皇家同花顺';
                if(isset($spaceNum['1'])){
                    $max = $compare[2];
                }
                $result['word'] = $word;
                $result['cow'] = $cow;
                $result['max'] = $max;
                return $result;
            }
            //同花顺
            if(count($card) == count(array_unique($card))) {
                $maxC=$minC=-1;
                for ($i=0; $i < 3; $i++) {
                    if($card[$i] > $maxC || $maxC == -1){
                        $maxC = $card[$i];
                    }
                    if($card[$i] < $minC || $minC == -1){
                        $minC = $card[$i];
                    }
                }
                if($maxC - $minC == 2){
                    $cow = 12;
                    $word = '同花顺';
//                    if(isset($spaceNum['1'])){
//                        $max = $compare[2];
//                    }
                    $result['word'] = $word;
                    $result['cow'] = $cow;
                    $result['max'] = $max;
                    return $result;
                }
            }
        }
        if($card[0]>10)$card[0]=10;
        if($card[1]>10)$card[1]=10;
        if($card[2]>10)$card[2]=10;
        $sumNum = $card[0] + $card[1] + $card[2];
        if($sumNum < 10){
            $cow = $sumNum;
        }else{
            $cow = $sumNum%10;
        }
        switch ($cow){
            case 0:
                $word = '牛牛';
                break;
            case 1:
                $word = '牛一';
                break;
            case 2:
                $word = '牛二';
                break;
            case 3:
                $word = '牛三';
                break;
            case 4:
                $word = '牛四';
                break;
            case 5:
                $word = '牛五';
                break;
            case 6:
                $word = '牛六';
                break;
            case 7:
                $word = '牛七';
                break;
            case 8:
                $word = '牛八';
                break;
            case 9:
                $word = '牛九';
                break;
        }
        if($cow == 0){
            $cow = 10;
        }
        $result['word'] = $word;
        $result['cow'] = $cow;
        $result['max'] = $max;
        return $result;
    }

    /**
     * TODO TC比较两张牌大小
     * @param int $card1 卡牌
     * @param int $card2 卡牌
     * @param int $result 结果
     * @return int;
     */
    public static function compareCardTc(int $card1, int $card2, int $result): int
    {
        $card1Num = CardPosition::interchangeCard($card1);
        $card2Num = CardPosition::interchangeCard($card2);
        $card1Color = CardPosition::interchangeColor($card1);
        $card2Color = CardPosition::interchangeColor($card2);
        if($card1Num > $card2Num){
            return 1;
        }elseif($card1Num < $card2Num){
            return 0;
        }else{
            if($card1Color > $card2Color){
                return 0;
            }elseif($card1Color < $card2Color){
                return 1;
            }
        }
    }
}