package com.ff.api.controller;


import com.ff.annotation.CheckHeader;
import com.ff.api.request.GameLoginRequest;
import com.ff.api.request.MemberCreateApiRequest;
import com.ff.base.constant.Constants;
import com.ff.base.core.controller.BaseController;
import com.ff.base.core.domain.AjaxResult;
import com.ff.base.utils.SecurityUtils;
import com.ff.base.utils.StringUtils;
import com.ff.common.domain.TenantSecretKey;
import com.ff.config.KeyConfig;
import com.ff.game.api.IGamesService;
import com.ff.game.api.request.CreateMemberRequestDTO;
import com.ff.game.api.request.GamesLogin;
import com.ff.game.api.request.MemberInfoRequestDTO;
import com.ff.game.domain.Game;
import com.ff.game.domain.GamePlatform;
import com.ff.game.domain.GameSecretKey;
import com.ff.game.service.IGamePlatformService;
import com.ff.game.service.IGameSecretKeyService;
import com.ff.game.service.IGameService;
import com.ff.member.domain.Member;
import com.ff.member.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * api控制器
 *
 * @author shi
 * @date 2025/02/10
 */
@RestController
@CheckHeader
@RequestMapping("/api/game")
public class ApiGameController extends BaseController {


    @Autowired
    private Map<String, IGamesService> gamesService;


    @Resource
    private IGameService gameService;


    @Resource
    private KeyConfig keyConfig;

    @Resource
    private IGameSecretKeyService gameSecretKeyService;

    @Resource
    private IMemberService memberService;

    @Resource
    private IGamePlatformService gamePlatformService;


    /**
     * 登录
     *
     * @param memberCreateApiRequest 成员创建api请求
     * @return {@link AjaxResult }
     */
    @PostMapping("/login")
    public AjaxResult login(@Validated @RequestBody GameLoginRequest memberCreateApiRequest) {

        Game game = gameService.selectGameById(memberCreateApiRequest.getGameId());
        Assert.notNull(game, "游戏不存在");

        GamePlatform gamePlatform = gamePlatformService.selectGamePlatformById(game.getPlatformId());
        Assert.notNull(gamePlatform, "游戏平台不存在");


        IGamesService iGamesService = gamesService.get(gamePlatform.getPlatformCode() + Constants.SERVICE);
        Assert.notNull(iGamesService, "平台不存在");


        TenantSecretKey tenantSecretKey = keyConfig.get();
        GameSecretKey gameSecretKey = gameSecretKeyService.findSecretKeyByPlatformAndSystemCode(gamePlatform.getPlatformCode(), memberCreateApiRequest.getCurrencyCode());
        Assert.notNull(gameSecretKey, "货币游戏平台不存在");

        GameSecretKey gameSecretKeyLang = gameSecretKeyService.findByPlatformAndSystemLangCode(gamePlatform.getPlatformCode(), memberCreateApiRequest.getLangCode());
        Assert.notNull(gameSecretKeyLang, "当前语言不存在");

        Member member = memberService.selectMemberByGameAccount(StringUtils.addSuffix(memberCreateApiRequest.getAccount(), memberCreateApiRequest.getCurrencyCode() + tenantSecretKey.getTenantSn()));

        GamesLogin gamesLogin = GamesLogin.builder()
                .agentId(gameSecretKey.getCode())
                .agentKey(gameSecretKey.getKey())
                .account(member.getGameAccount())
                .gameId(game.getGameCode())
                .homeUrl(memberCreateApiRequest.getHomeUrl())
                .platform(memberCreateApiRequest.getPlatform())
                .disableFullScreen(memberCreateApiRequest.getDisableFullScreen())
                .lang(gameSecretKeyLang.getLang())
                .build();

        String login = iGamesService.loginWithoutRedirect(gamesLogin);

        return AjaxResult.success(login);
    }





}
