package com.github.microservice.auth.server.core.service.local;

import com.github.microservice.auth.client.content.ResultContent;
import com.github.microservice.auth.client.content.ResultState;
import com.github.microservice.auth.client.model.UserNFCCardUpdateModel;
import com.github.microservice.auth.client.service.UserNFCCardService;
import com.github.microservice.auth.server.core.dao.UserDao;
import com.github.microservice.auth.server.core.domain.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserNFCCardServiceImpl implements UserNFCCardService {

    @Autowired
    UserDao userDao;

    public ResultContent update(UserNFCCardUpdateModel userNFCCardUpdateModel) {
        User user = userDao.findTop1ById(userNFCCardUpdateModel.getUserId());
        if (user == null){
            return ResultContent.build(ResultState.UserNotExists);
        }
        //判断卡号是否重复
        if (userDao.existsByNfcCardContains(userNFCCardUpdateModel.getNfcCard())){
            return ResultContent.build(ResultState.NFCCardExists);
        }
        List<String> nfcCardList = user.getNfcCard();
        if (nfcCardList == null){
            nfcCardList = new ArrayList<>();
        }

        if (StringUtils.isNotBlank(userNFCCardUpdateModel.getUpdateCard())){
            if (!nfcCardList.contains(userNFCCardUpdateModel.getUpdateCard())){
                return ResultContent.build(ResultState.NFCCardNotExists);
            }
            nfcCardList.set(nfcCardList.indexOf(userNFCCardUpdateModel.getUpdateCard()), userNFCCardUpdateModel.getNfcCard());
        }else {
            nfcCardList.add(userNFCCardUpdateModel.getNfcCard());
        }
        user.setNfcCard(nfcCardList);
        userDao.save(user);
        return ResultContent.build(ResultState.Success);
    }

    public ResultContent delete(String userId, String nfcCard){
        User user = userDao.findTop1ById(userId);
        if (user == null){
            return ResultContent.build(ResultState.UserNotExists);
        }
        List<String> nfcCardList = user.getNfcCard();
        if (nfcCardList!=null && nfcCardList.contains(nfcCard)){
            nfcCardList.remove(nfcCard);
            user.setNfcCard(nfcCardList);
            userDao.save(user);
            return ResultContent.build(ResultState.Success);
        }else {
            return ResultContent.build(ResultState.NFCCardNotExists);
        }
    }

    public ResultContent<List<String>> get(String userId){
        User user = userDao.findTop1ById(userId);
        if (user == null){
            return ResultContent.build(ResultState.UserNotExists);
        }
        List<String> nfcCard = user.getNfcCard();
        return ResultContent.buildContent(nfcCard!=null?nfcCard:new ArrayList<String>());
    }

    public ResultContent<String> matches(String nfcCard){

        User cardContainsUser = userDao.findTopByNfcCardContains(nfcCard);
        if (cardContainsUser == null){
            return ResultContent.build(ResultState.NFCCardNotBind);
        }
        return ResultContent.buildContent(cardContainsUser.getId());
    }
}
