<?php

namespace app\service\game;

use addons\webman\model\GamePlatform;
use addons\webman\model\Player;
use addons\webman\model\PlayerDeliveryRecord;
use addons\webman\model\PlayerWalletTransfer;
use Exception;

/**
 * 游戏服务工厂
 */
class GameServiceFactory
{
    const TYPE_LUCKY365 = 'LUCKY365'; // lucky365
    const TYPE_BIGGAMING = 'BIGGAMING'; // BigGaming
    const TYPE_JILI = 'JILI'; // jili
    const TYPE_MEGA888 = 'MEGA888'; // Mega888
    const TYPE_KISS918 = 'KISS918'; // Kiss918
    const TYPE_JDB = 'JDB'; // JDB
    const TYPE_PRAGMATIC = 'PRAGMATIC'; // PRAGMATIC
    const TYPE_MARIOCLUB = 'MARIOCLUB'; // MARIOCLUB
    const TYPE_JOKER = 'JOKER'; // JOKER
    const TYPE_LIONKING = 'LIONKING'; // LionKing
    const TYPE_MONKEY_KING = 'MONKEYKING'; // monkey king
    const TYPE_ASIAGAMING = 'ASIAGAMING'; // AsiaGaming
    const TYPE_TFGAMING = 'TFGAMING'; // TfGaming
    const TYPE_IBC = 'IBC'; // IBC
    const TYPE_PLAYSTAR = 'PLAYSTAR'; // PlayStar
    const TYPE_AWC68 = 'AWC68'; // AWC68
    const TYPE_GAMEPLAY = 'GAMEPLAY'; // GamePlay
    const TYPE_NEXTSPIN = 'NEXTSPIN'; // GamePlay
    const TYPE_WMCASINO = 'WMCASINO'; // WMcasino
    const TYPE_SPADEGAMING = 'SPADEGAMING'; // SpadeGaming
    const TYPE_FUNKYGAME = 'FUNKYGAME'; // FunkyGame
    const TYPE_R5 = 'R5'; // R5
    const TYPE_JK = 'JK'; // JK

    const DEVICE_TYPE_WEB = 1; // web
    const DEVICE_TYPE_IOS = 2; // ios
    const DEVICE_TYPE_ANDROID = 3; // android

    /** @var Player $player */
    public $player;
    /** @var GamePlatform $platform */
    public $platform;

    /**
     * 创建服务
     * @throws Exception
     */
    public static function createService(string $type, $player = null): GameServiceInterface
    {
        switch ($type) {
            case self::TYPE_LUCKY365:
                return new Lucky365ServiceInterface(self::TYPE_LUCKY365, $player);
            case self::TYPE_JILI:
                return new JiLiServiceInterface(self::TYPE_JILI, $player);
            case self::TYPE_MEGA888:
                return new MeGa888ServiceInterface(self::TYPE_MEGA888, $player);
            case self::TYPE_BIGGAMING:
                return new BigGamingServiceInterface(self::TYPE_BIGGAMING, $player);
            case self::TYPE_KISS918:
                return new Kiss918ServiceInterface(self::TYPE_KISS918, $player);
            case self::TYPE_JDB:
                return new JDBServiceInterface(self::TYPE_JDB, $player);
            case self::TYPE_PRAGMATIC:
                return new PragmaticServiceInterface(self::TYPE_PRAGMATIC, $player);
            case self::TYPE_MARIOCLUB:
                return new MarioClubServiceInterface(self::TYPE_MARIOCLUB, $player);
            case self::TYPE_JOKER:
                return new JokerServiceInterface(self::TYPE_JOKER, $player);
            case self::TYPE_NEXTSPIN:
                return new NextSpinServiceInterface(self::TYPE_NEXTSPIN, $player);
            case self::TYPE_MONKEY_KING:
                return new MonkeyKingServiceInterface(self::TYPE_MONKEY_KING, $player);
            case self::TYPE_LIONKING:
                return new LionKingServiceInterface(self::TYPE_LIONKING, $player);
            case self::TYPE_ASIAGAMING:
            case self::TYPE_TFGAMING:
            case self::TYPE_IBC:
            case self::TYPE_PLAYSTAR:
            case self::TYPE_AWC68:
            case self::TYPE_GAMEPLAY:
            case self::TYPE_WMCASINO:
            case self::TYPE_SPADEGAMING:
            case self::TYPE_FUNKYGAME:
            case self::TYPE_R5:
            case self::TYPE_JK:
                return new CSServiceInterface($type, $player);
            default:
                throw new Exception("未知的游戏服务类型: $type");
        }
    }

    /**
     * 报错平台转出/入记录
     * @param int $type
     * @param float $amount
     * @param float $reward
     * @param string $platformNo
     * @return void
     */
    public function createWalletTransfer(int $type = 1, float $amount = 0, float $reward = 0, string $platformNo = '')
    {
        $playerWalletTransfer = new PlayerWalletTransfer();
        $playerWalletTransfer->player_id = $this->player->id;
        $playerWalletTransfer->platform_id = $this->platform->id;
        $playerWalletTransfer->department_id = $this->player->department_id;
        $playerWalletTransfer->type = $type;
        $playerWalletTransfer->amount = abs($amount);
        $playerWalletTransfer->reward = abs($reward);
        $playerWalletTransfer->platform_no = $platformNo;
        $playerWalletTransfer->tradeno = createOrderNo();
        $playerWalletTransfer->save();
        $beforeGameAmount = $this->player->wallet->money;
        $playerDeliveryRecord = new PlayerDeliveryRecord;
        if ($type == PlayerWalletTransfer::TYPE_OUT) {
            $this->player->wallet->money = 0;
            $playerDeliveryRecord->type = PlayerDeliveryRecord::TYPE_GAME_OUT;
        }
        if ($type == PlayerWalletTransfer::TYPE_IN) {
            $this->player->wallet->money = bcadd($this->player->wallet->money, bcadd(abs($amount), abs($reward), 2), 2);
            $playerDeliveryRecord->type = PlayerDeliveryRecord::TYPE_GAME_IN;
        }
        $this->player->push();
        //寫入金流明細
        $playerDeliveryRecord->player_id = $playerWalletTransfer->player_id;
        $playerDeliveryRecord->department_id = $playerWalletTransfer->department_id;
        $playerDeliveryRecord->target = $playerWalletTransfer->getTable();
        $playerDeliveryRecord->target_id = $playerWalletTransfer->id;
        $playerDeliveryRecord->source = 'play_game';
        $playerDeliveryRecord->amount = $playerWalletTransfer->amount;
        $playerDeliveryRecord->amount_before = $beforeGameAmount;
        $playerDeliveryRecord->amount_after = $this->player->wallet->money;
        $playerDeliveryRecord->tradeno = '';
        $playerDeliveryRecord->remark = '';
        $playerDeliveryRecord->save();
    }
}