package com.izouma.nineth.web;

import cn.hutool.core.collection.CollectionUtil;
import com.izouma.nineth.domain.*;
import com.izouma.nineth.dto.MetaRestResult;
import com.izouma.nineth.repo.*;
import com.izouma.nineth.service.MetaPlayerInfoService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/metaPlayerInfo")
@AllArgsConstructor
public class MetaPlayerInfoController {

    private UserRepo userRepo;

    private MetaPlayerInfoService metaPlayerInfoService;

    private MetaPlayerOfflineInfoRepo metaPlayerOfflineInfoRepo;

    private MetaPlayerWearRepo metaPlayerWearRepo;

    private MetaUserRepo metaUserRepo;

    private PurchaseLevelRepo purchaseLevelRepo;

    @GetMapping("/{userId}/detail")
    public MetaRestResult<MetaPlayerInfo> findMetaPlayerInfo(@PathVariable Long userId) {
        User user = userRepo.findById(userId).orElse(null);
        if (Objects.isNull(user)) {
            return MetaRestResult.returnError("用户信息为空");
        }
        MetaPlayerInfo metaPlayerInfo = new MetaPlayerInfo();
        metaPlayerInfo.setAvatar(user.getAvatar());
        metaPlayerInfo.setLevel(user.getLevel());
        int maxStartLevel = purchaseLevelRepo.findMaxStartLevel();
        PurchaseLevel purchaseLevel;
        if (user.getLevel() >= maxStartLevel) {
            purchaseLevel = purchaseLevelRepo.findByStartLevel(maxStartLevel);
        } else {
            purchaseLevel = purchaseLevelRepo.findByLevel(user.getLevel());
        }
        metaPlayerInfo.setRealm(purchaseLevel.getRealm());
        metaPlayerInfo.setTitle(purchaseLevel.getTitle());
        metaPlayerInfo.setNickname(user.getNickname());
        MetaPlayerOfflineInfo metaPlayerOfflineInfo = metaPlayerOfflineInfoRepo.findLastByUserId(userId);
        if (Objects.isNull(metaPlayerOfflineInfo)) {
            return MetaRestResult.returnSuccess(metaPlayerInfo);
        }
        metaPlayerInfo.setSpaceId(metaPlayerOfflineInfo.getSpaceId());
        metaPlayerInfo.setSkinId(metaPlayerOfflineInfo.getSkinId());
        metaPlayerInfo.setRoleId(metaPlayerOfflineInfo.getRoleId());
        List<Long> clothesId = metaPlayerOfflineInfo.getClothesId();
        if (CollectionUtil.isEmpty(clothesId)) {
            return MetaRestResult.returnSuccess(metaPlayerInfo);
        }
        List<MetaPlayerWear> metaPlayerWears = metaPlayerWearRepo.findAllByIdIn(clothesId);
        metaPlayerInfo.setMetaPlayerWearList(metaPlayerWears);
        return MetaRestResult.returnSuccess(metaPlayerInfo);
    }

    @GetMapping("/{userId}/{spaceId}/metaSpaceInfo")
    public MetaRestResult<SpaceObjectsInfo> metaSpaceInfo(@PathVariable Long userId, @PathVariable Long spaceId) {
        return metaPlayerInfoService.metaSpaceInfo(userId, spaceId);
    }

    @PostMapping("/updateMetaPersonalSpace")
    public MetaRestResult<SpaceObjectsInfo> updateMetaPersonalSpace(@RequestBody SpaceObjectsInfo spaceObjectsInfo) {
        return metaPlayerInfoService.updateMetaPersonalSpace(spaceObjectsInfo);
    }

    @PostMapping("/saveOffline")
    public MetaRestResult<MetaPlayerOfflineInfo> saveOffline(@RequestBody MetaPlayerOfflineInfo metaPlayerOfflineInfo) {
        if (Objects.isNull(metaPlayerOfflineInfo.getUserId())) {
            return MetaRestResult.returnError("参数错误，缺少用户id");
        }
        MetaPlayerOfflineInfo save = metaPlayerOfflineInfoRepo.save(metaPlayerOfflineInfo);
        return MetaRestResult.returnSuccess(save);
    }

    @GetMapping("/{phone}/allowLogin")
    public MetaRestResult<Boolean> allowLogin(@PathVariable Long phone) {
        MetaUser metaUser = metaUserRepo.findByPhone(phone);
        if (Objects.isNull(metaUser)) {
            return MetaRestResult.returnSuccess(Boolean.FALSE);
        }
        return MetaRestResult.returnSuccess(metaUser.isAllowLogin());
    }
}
