package com.red.circle.other.adapter.app.boot;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.app.dto.task.TaskDTO;
import com.red.circle.other.app.service.task.TaskService;
import com.red.circle.other.infra.database.rds.entity.sys.EnumConfig;
import com.red.circle.other.infra.database.rds.entity.user.user.AuthType;
import com.red.circle.other.infra.database.rds.entity.user.user.BaseInfo;
import com.red.circle.other.infra.database.rds.service.sys.EnumConfigService;
import com.red.circle.other.infra.database.rds.service.user.device.LatestMobileDeviceService;
import com.red.circle.other.infra.database.rds.service.user.user.AuthTypeService;
import com.red.circle.other.infra.database.rds.service.user.user.BaseInfoService;
import com.red.circle.other.inner.asserts.ActivityErrorCode;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import com.red.circle.other.inner.enums.user.AuthTypeEnum;
import com.red.circle.wallet.inner.endpoint.bank.UserBankBalanceClient;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/app/boot", produces = MediaType.APPLICATION_JSON_VALUE)
public class IndexController {
    private final UserBankBalanceClient userBankBalanceClient;
    private final EnumConfigService enumConfigService;
    private final LatestMobileDeviceService latestMobileDeviceService;
    private final AuthTypeService authTypeService;
    private final BaseInfoService baseInfoService;

    @GetMapping
    public Map<String, Object> boot(AppExtCommand cmd) {
        HashMap<String, Object> resultMap = new HashMap<>();

        AuthType authType = authTypeService.getAuthInfo(
                AuthTypeEnum.ACCOUNT.name(),
                cmd.requiredReqUserIdStr()
        );

        // 4032 已被绑定
        // ResponseAssert.isNull(UserErrorCode.ALREADY_USED, authType);
        resultMap.put("pwdBind", Objects.isNull(authType));

        //游戏可见策略：
        boolean gameSwitch = false;
        EnumConfig enumConfig = enumConfigService.getByEnumConfig("GAME_BLACKLIST", cmd.getReqSysOrigin().getOrigin());
        if (Objects.nonNull(enumConfig)) {
            if (enumConfig.getVal().contains("ALL")) {
                resultMap.put("gameSwitch", false);
                //所有人不可见
                return resultMap;
            }
            if (!enumConfig.getVal().contains(String.valueOf(cmd.requiredReqUserId()))) {
                gameSwitch = true;
            } else {
                resultMap.put("gameSwitch", false);
                //1、黑名单永远不可见：谷歌审核账号：1941965
                return resultMap;
            }
        }
        Long balance = userBankBalanceClient.getTotalBalance(cmd.requiredReqUserId()).getBody();
        if (50000L <= balance) {
            gameSwitch = true;
        }

        resultMap.put("gameSwitch", gameSwitch);
        //1、黑名单永远不可见：谷歌审核账号：1941965
        //2、普通用户满足任一条件，即可见（1）累计充值超过10美金（2）财富等级达到10级
        return resultMap;
    }

    @GetMapping("/checkLogin")
    public Map<String, Object> checkDeviceLogin(AppExtCommand cmd) {
        HashMap<String, Object> resultMap = new HashMap<>();

        // 设备登录开关逻辑
        List<Long> deviceUsers = latestMobileDeviceService.listUserIdByImei(
                cmd.getReqImei(),
                cmd.getReqSysOrigin().getOrigin()
        );
        boolean deviceLoginSwitch = false;
        boolean accountLoginSwitch = false;
        boolean deviceFlag = CollectionUtils.isEmpty(deviceUsers);
        if (!deviceFlag) {
            //老设备

            //判断有没有绑定密码
            // 设备登录开关逻辑
            AuthType imeiUser = authTypeService.getAuthInfo(
                    cmd.getReqImei().concat("_").concat(cmd.requireReqSysOrigin()));
            if (!Objects.isNull(imeiUser)) {
                AuthType accountUser = authTypeService.getAuthInfo(
                        AuthTypeEnum.ACCOUNT.name(),
                        imeiUser.getUserId().toString()
                );
                if (Objects.isNull(accountUser)) {
                    deviceLoginSwitch = true;
                }
                List<BaseInfo> baseInfos = baseInfoService.listByUserId(Collections.singleton(imeiUser.getUserId()));
                if (!CollectionUtils.isEmpty(baseInfos)) {
                    BaseInfo baseInfo = baseInfos.get(0);
                    resultMap.put("account", baseInfo.getAccount());
                }

            }


            accountLoginSwitch = true;

        }
        resultMap.put("deviceLoginSwitch", deviceLoginSwitch);
        resultMap.put("accountLoginSwitch", accountLoginSwitch);
        return resultMap;
    }


}
