<?php


namespace freedom\utils;

/**
 * TODO 卡牌工具类
 * Class CardPosition
 * @package freedom\utils
 */
class CardPosition
{
    /**
     * TODO 卡牌编号转换数字
     * @param int $id 卡牌
     * @return int;
     */
    public static function interchangeCard(int $id): int
    {
        if($id == 101 || $id == 201 || $id == 301 || $id == 401){
            return 1;
        }
        if($id == 102 || $id == 202 || $id == 302 || $id == 402){
            return 2;
        }
        if($id == 103 || $id == 203 || $id == 303 || $id == 403){
            return 3;
        }
        if($id == 104 || $id == 204 || $id == 304 || $id == 404){
            return 4;
        }
        if($id == 105 || $id == 205 || $id == 305 || $id == 405){
            return 5;
        }
        if($id == 106 || $id == 206 || $id == 306 || $id == 406){
            return 6;
        }
        if($id == 107 || $id == 207 || $id == 307 || $id == 407){
            return 7;
        }
        if($id == 108 || $id == 208 || $id == 308 || $id == 408){
            return 8;
        }
        if($id == 109 || $id == 209 || $id == 309 || $id == 409){
            return 9;
        }
        if($id == 110 || $id == 210 || $id == 310 || $id == 410){
            return 10;
        }
        if($id == 111 || $id == 211 || $id == 311 || $id == 411){
            return 11;
        }
        if($id == 112 || $id == 212 || $id == 312 || $id == 412){
            return 12;
        }
        if($id == 113 || $id == 213 || $id == 313 || $id == 413){
            return 13;
        }
        return 0;
    }

    /**
     * TODO 卡牌编号转换字段名称
     * @param int $id 卡牌
     * @return string;
     */
    public static function interchangePosition(int $id): string
    {
        if($id == 11){
            return 'player_1';
        }
        if($id == 12){
            return 'player_2';
        }
        if($id == 13){
            return 'player_3';
        }
        if($id == 21){
            return 'banker_1';
        }
        if($id == 22){
            return 'banker_2';
        }
        if($id == 23){
            return 'banker_3';
        }
        return '';
    }

    /**
     * TODO 判断花式大小
     * @param int $id 卡牌
     * @return string|int;
     */
    public static function interchangeColor(int $id): string
    {
        return substr( $id, 0, 1 );
    }

    //卡牌位置信息转换
    /**
     * TODO 卡牌位置信息转换
     * @param int $id 卡牌
     * @return int;
     */
    public static function interchangeNumber(int $id): int
    {
        if($id >= 10){
            $id = 0;
        }
        return $id;
    }

    /**
     * TODO Baccarat开牌条件判断
     * @param array $cardInfo 卡牌
     * @return bool;
     */
    public static function checkOpenScan(array $cardInfo): bool
    {
        $playerCount = (self::interchangeNumber(self::interchangeCard($cardInfo['player_1'])) + self::interchangeNumber(self::interchangeCard($cardInfo['player_2']))) % 10;
        $bankerCount = (self::interchangeNumber(self::interchangeCard($cardInfo['banker_1'])) + self::interchangeNumber(self::interchangeCard($cardInfo['banker_2']))) % 10;
        $player3Count = (self::interchangeNumber(self::interchangeCard($cardInfo['player_3']))) % 10;
        if(empty($cardInfo['player_1']) || empty($cardInfo['player_2']) || empty($cardInfo['banker_1']) || empty($cardInfo['banker_2'])){
            return false;
        }
        if(($playerCount == 8 || $playerCount == 9 || $bankerCount == 8 || $bankerCount == 9) && empty($cardInfo['player_3']) && empty($cardInfo['banker_3'])){
            return true;
        }
        if($playerCount >= 6 && $bankerCount >= 6 && empty($cardInfo['player_3']) && empty($cardInfo['banker_3'])){
            return true;
        }
        if(($playerCount == 6 || $playerCount == 7) && $bankerCount < 6 && empty($cardInfo['player_3']) && $cardInfo['banker_3'] > 0){
            return true;
        }
        if($playerCount < 6 && $cardInfo['player_3'] > 0 && $bankerCount == 7 && empty($cardInfo['banker_3'])){
            return true;
        }
        if($playerCount < 6 && $cardInfo['player_3'] > 0 && (($bankerCount == 6 && in_array($player3Count,array(6,7)) && $cardInfo['banker_3'] > 0) || ($bankerCount == 6 && in_array($player3Count,array(1,2,3,4,5,8,9,0)) && empty($cardInfo['banker_3'])))){
            return true;
        }
        if($playerCount < 6 && $cardInfo['player_3'] > 0 && (($bankerCount == 5 && in_array($player3Count,array(4,5,6,7)) && $cardInfo['banker_3'] > 0) || ($bankerCount == 5 && in_array($player3Count,array(1,2,3,8,9,0)) && empty($cardInfo['banker_3'])))){
            return true;
        }
        if($playerCount < 6 && $cardInfo['player_3'] > 0 && (($bankerCount == 4 && in_array($player3Count,array(2,3,4,5,6,7)) && $cardInfo['banker_3'] > 0) || ($bankerCount == 4 && in_array($player3Count,array(1,8,9,0)) && empty($cardInfo['banker_3'])))){
            return true;
        }
        if($playerCount < 6 && $cardInfo['player_3'] > 0 && (($bankerCount == 3 && in_array($player3Count,array(0,1,2,3,4,5,6,7,9)) && $cardInfo['banker_3'] > 0) || ($bankerCount == 3 && in_array($player3Count,array(8)) && empty($cardInfo['banker_3'])))){
            return true;
        }
        if($playerCount < 6 && $cardInfo['player_3'] > 0 && $bankerCount < 3 && $cardInfo['banker_3'] > 0){
            return true;
        }
        return false;
    }
}