package com.ff.api.controller;


import com.ff.annotation.CheckHeader;
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.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.GamesBaseRequestDTO;
import com.ff.game.api.request.MemberInfoRequestDTO;
import com.ff.game.domain.GameSecretKey;
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.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

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


    @Autowired
    private Map<String, IGamesService> gamesService;


    @Resource
    private IGameService gameService;


    @Resource
    private KeyConfig keyConfig;

    @Resource
    private IGameSecretKeyService gameSecretKeyService;

    @Resource
    private IMemberService memberService;


    /**
     * 创建成员
     *
     * @param memberCreateApiRequest 创建成员api请求
     * @return {@link AjaxResult }
     */
    @PostMapping("/create")
    public AjaxResult createMember(@Validated @RequestBody MemberCreateApiRequest memberCreateApiRequest) {

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


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

        String gameAccount = StringUtils.addSuffix(memberCreateApiRequest.getAccount(), memberCreateApiRequest.getCurrencyCode() + tenantSecretKey.getTenantSn());
        //向第三方注册账号
        CreateMemberRequestDTO gamesBaseRequestDTO = CreateMemberRequestDTO.builder()
                .account(gameAccount)
                .agentId(gameSecretKey.getCode())
                .agentKey(gameSecretKey.getKey())
                .build();
        Boolean result = iGamesService.createMember(gamesBaseRequestDTO);
        Assert.isTrue(result, "建立游戏账号失败");

        List<Member> members = memberService.selectMemberList(Member.builder()
                .tenantKey(tenantSecretKey.getTenantKey()).memberAccount(memberCreateApiRequest.getAccount()).build());
        if (CollectionUtils.isEmpty(members)) {
            //注册本地账号
            Member member = Member.builder()
                    .tenantKey(tenantSecretKey.getTenantKey())
                    .memberAccount(memberCreateApiRequest.getAccount())
                    .gameAccount(gameAccount)
                    .platformCode(memberCreateApiRequest.getPlatformCode())
                    .currencyCode(memberCreateApiRequest.getCurrencyCode())
                    .build();
            return toAjax(memberService.insertMember(member));
        }
        return toAjax(Boolean.TRUE);

    }


    /**
     * 获取会员信息
     *
     * @param memberCreateApiRequest 成员创建api请求
     * @return {@link AjaxResult }
     */
    @PostMapping("/info")
    public AjaxResult getMemberInfo(@Validated @RequestBody MemberCreateApiRequest memberCreateApiRequest) {
        IGamesService iGamesService = gamesService.get(memberCreateApiRequest.getPlatformCode() + Constants.SERVICE);
        Assert.notNull(iGamesService, "平台不存在");


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

        Member member = memberService.selectMemberByGameAccount(StringUtils.addSuffix(memberCreateApiRequest.getAccount(), memberCreateApiRequest.getCurrencyCode() + tenantSecretKey.getTenantSn()));
        Assert.notNull(member, "会员不存在");

        //向第三方查询账号
        MemberInfoRequestDTO gamesBaseRequestDTO = MemberInfoRequestDTO.builder()
                .accounts(member.getGameAccount())
                .agentId(gameSecretKey.getCode())
                .agentKey(gameSecretKey.getKey())
                .build();
        return AjaxResult.success(iGamesService.getMemberInfo(gamesBaseRequestDTO));
    }


}
