package com.izouma.dingdong.service.backstage;

import com.izouma.dingdong.domain.User;
import com.izouma.dingdong.domain.backstage.BlackList;
import com.izouma.dingdong.domain.merchant.Merchant;
import com.izouma.dingdong.domain.rider.Rider;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.UserRepo;
import com.izouma.dingdong.repo.backstage.BlackListRepo;
import com.izouma.dingdong.repo.merchant.MerchantRepo;
import com.izouma.dingdong.repo.rider.RiderRepo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@AllArgsConstructor
public class BlackListService {

    private BlackListRepo blackListRepo;
    private UserRepo      userRepo;
    private MerchantRepo  merchantRepo;
    private RiderRepo     riderRepo;

    /*
    加入黑名单
     */
    public BlackList move(Long userId, String reason) {
        User user = userRepo.findById(userId).orElseThrow(new BusinessException("不存在"));

        BlackList build = BlackList.builder()
                .userId(userId)
                .reason(reason)
                .identity(user.getIdentity())
                .remove(false)
                .moveTime(LocalDateTime.now())
                .build();

        switch (user.getIdentity()) {
            case MERCHANT:
                Merchant merchant = merchantRepo.findByUserId(userId).orElseThrow(new BusinessException("商家不存在"));
                merchant.setBlacklist(true);
                merchantRepo.save(merchant);
                build.setOtherId(merchant.getId());
                break;
            case RIDER:
                Rider rider = riderRepo.findByUserId(userId).orElseThrow(new BusinessException("骑手不存在"));
                rider.setBlacklist(true);
                riderRepo.save(rider);
                build.setOtherId(rider.getId());
                break;
            case USER:
                user.setBlacklist(true);
                userRepo.save(user);
                build.setOtherId(userId);
                break;
        }

/*        if (Identity.MERCHANT.equals(user.getIdentity())) {
            Merchant merchant = merchantRepo.findByUserId(userId).orElseThrow(new BusinessException("商家不存在"));
            merchant.setBlacklist(true);
            merchantRepo.save(merchant);
            build.setOtherId(merchant.getId());

        } else if (Identity.RIDER.equals(user.getIdentity())) {
            Rider rider = riderRepo.findByUserId(userId).orElseThrow(new BusinessException("骑手不存在"));
            rider.setBlacklist(true);
            riderRepo.save(rider);
            build.setOtherId(rider.getJobNumber());

        } else {
            user.setBlacklist(true);
            userRepo.save(user);
            build.setOtherId(userId);
        }*/

        return blackListRepo.save(build);

    }


    /*
    移出黑名单
     */
    public void remove(Long id) {
        BlackList blackList = blackListRepo.findById(id).orElseThrow(new BusinessException("无记录"));
        blackList.setRemove(true);
        blackList.setRemoveTime(LocalDateTime.now());
        blackListRepo.save(blackList);

        switch (blackList.getIdentity()) {
            case USER:
                User user = userRepo.findById(blackList.getOtherId()).orElseThrow(new BusinessException("无用户"));
                user.setBlacklist(false);
                userRepo.save(user);
                break;

            case MERCHANT:
                Merchant merchant = merchantRepo.findById(blackList.getOtherId())
                        .orElseThrow(new BusinessException("无商户"));
                merchant.setBlacklist(false);
                merchantRepo.save(merchant);
                break;

            case RIDER:
                Rider rider = riderRepo.findById(blackList.getOtherId())
                        .orElseThrow(new BusinessException("无骑手"));
                rider.setBlacklist(false);
                riderRepo.save(rider);
                break;

        }
    }

}
