package com.zanxiang.game.module.base.pojo.dto;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author : lingfeng
 * @time : 2024-05-23
 * @description : 游戏区服
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class GameServerDTO implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 超父游戏区服列表
     */
    public List<SuperGameServerBean> superGameServerList;

    /**
     * 通过游戏id获取原始区服
     *
     * @param gameId : 游戏id
     * @return : 返回区服信息
     */
    public List<GameServerBean> getAllGameServerByGameId(Long gameId) {
        if (CollectionUtils.isEmpty(this.superGameServerList)) {
            return null;
        }
        SuperGameServerBean superGameServer = this.superGameServerList.stream()
                .filter(superGameServerBean -> superGameServerBean.getGameIdList().contains(gameId))
                .findFirst().orElse(null);
        if (superGameServer == null) {
            return null;
        }
        return superGameServer.getSourceGameServerList();
    }

    /**
     * 通过超父游戏id获取原始区服
     *
     * @param superGameId : 超父游戏id
     * @return : 返回区服信息
     */
    public List<GameServerBean> getAllGameServerBySupperGameId(Long superGameId) {
        if (CollectionUtils.isEmpty(this.superGameServerList)) {
            return null;
        }
        SuperGameServerBean superGameServer = this.superGameServerList.stream()
                .filter(superGameServerBean -> Objects.equals(superGameId, superGameServerBean.getSuperGameId()))
                .findFirst().orElse(null);
        if (superGameServer == null) {
            return null;
        }
        return superGameServer.getSourceGameServerList();
    }

    /**
     * 通过游戏id,区服id获取原始区服
     *
     * @param gameId   : 游戏id
     * @param serverId : 区服id
     * @return : 返回区服信息
     */
    public GameServerBean getGameServerByGameId(Long gameId, String serverId) {
        if (CollectionUtils.isEmpty(this.superGameServerList)) {
            return null;
        }
        SuperGameServerBean superGameServer = this.superGameServerList.stream()
                .filter(superGameServerBean -> superGameServerBean.getGameIdList().contains(gameId))
                .findFirst().orElse(null);
        if (superGameServer == null) {
            return null;
        }
        return superGameServer.getSourceGameServerList().stream()
                .filter(gameServerBean -> Objects.equals(gameServerBean.getServerId(), serverId))
                .findFirst().orElse(null);
    }

    /**
     * 通过游戏id,区服id获取原始区服
     *
     * @param superGameId : 超父游戏id
     * @param serverId    : 区服id
     * @return : 返回区服信息
     */
    public GameServerBean getGameServerBySupperGameId(Long superGameId, String serverId) {
        if (CollectionUtils.isEmpty(this.superGameServerList)) {
            return null;
        }
        SuperGameServerBean superGameServer = this.superGameServerList.stream()
                .filter(superGameServerBean -> Objects.equals(superGameId, superGameServerBean.getSuperGameId()))
                .findFirst().orElse(null);
        if (superGameServer == null) {
            return null;
        }
        return superGameServer.getSourceGameServerList().stream()
                .filter(gameServerBean -> Objects.equals(gameServerBean.getServerId(), serverId))
                .findFirst().orElse(null);
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class SuperGameServerBean implements Serializable {

        private static final long serialVersionUID = 1L;

        /**
         * 超父游戏id
         */
        private Long superGameId;

        /**
         * 游戏id列表
         */
        private List<Long> gameIdList;

        /**
         * 原始区服列表
         */
        private List<GameServerBean> sourceGameServerList;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class GameServerBean implements Serializable {

        private static final long serialVersionUID = 1L;

        /**
         * 主键id
         */
        private Long id;

        /**
         * 超父游戏id
         */
        private Long superGameId;

        /**
         * 区服id
         */
        private String serverId;

        /**
         * 区服名称
         */
        private String serverName;

        /**
         * 区服冠名
         */
        private String nickName;

        /**
         * 开服时间
         */
        private LocalDateTime startTime;
    }
}
