package shop.alien.gateway.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.LifeUser;
import shop.alien.entity.store.vo.LifeUserVo;
import shop.alien.gateway.config.BaseRedisService;
import shop.alien.gateway.mapper.LifeUserMapper;
import shop.alien.util.common.JwtUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户
 */
@Service
@RequiredArgsConstructor
public class LifeUserService extends ServiceImpl<LifeUserMapper, LifeUser> {

    private final LifeUserMapper lifeUserMapper;

    private final BaseRedisService baseRedisService;

    @Value("${jwt.expiration-time}")
    private String effectiveTime;

    public LifeUserVo userLogin(String phoneNum) {
        int effectiveTimeInt = Integer.parseInt(effectiveTime.substring(0, effectiveTime.length() - 1));
        String effectiveTimeUnit = effectiveTime.substring(effectiveTime.length() - 1);
        long effectiveTimeIntLong = 0L;
        switch (effectiveTimeUnit) {
            case "s": {
                effectiveTimeIntLong = effectiveTimeInt * 1000L;
                break;
            }
            case "m": {
                effectiveTimeIntLong = effectiveTimeInt * 60L * 1000L;
                break;
            }
            case "h": {
                effectiveTimeIntLong = effectiveTimeInt * 60L * 60L * 1000L;
                break;
            }
            case "d": {
                effectiveTimeIntLong = effectiveTimeInt * 24L * 60L * 60L * 1000L;
                break;
            }
        }

        LifeUser user = getUserByPhone(phoneNum);
        if (user == null) {
            LifeUser lifeUser = new LifeUser();
            lifeUser.setUserPhone(phoneNum);
            lifeUser.setUserName(phoneNum);
            lifeUser.setRealName(phoneNum);
            lifeUser.setCreatedTime(new Date());
            int ret = lifeUserMapper.insert(lifeUser);
            if (ret == 1) {
                LifeUser user2 = getUserByPhone(phoneNum);
                LifeUserVo userVo = new LifeUserVo();
                BeanUtils.copyProperties(user2, userVo);
                Map<String, String> tokenMap = new HashMap<>();
                tokenMap.put("phone", phoneNum);
                tokenMap.put("userName", lifeUser.getUserName());
                tokenMap.put("userId", lifeUser.getId().toString());
                tokenMap.put("userType", "user");
                userVo.setToken(JwtUtil.createJWT("user_" + phoneNum, lifeUser.getUserName(), JSONObject.toJSONString(tokenMap), effectiveTimeIntLong));
//                userVo.setToken(JWTUtils.createToken(tokenMap));
                baseRedisService.setString("user_" + phoneNum, userVo.getToken());
                return userVo;
            } else {
                return null;
            }
        } else {
            LifeUserVo userVo = new LifeUserVo();
            BeanUtils.copyProperties(user, userVo);
            Map<String, String> tokenMap = new HashMap<>();
            tokenMap.put("phone", phoneNum);
            tokenMap.put("userName", user.getUserName());
            tokenMap.put("userId", user.getId().toString());
            tokenMap.put("userType", "user");
//            String token = JWTUtils.createToken(tokenMap);
            String token = JwtUtil.createJWT("user_" + phoneNum, user.getUserName(), JSONObject.toJSONString(tokenMap), effectiveTimeIntLong);
            userVo.setToken(token);
            baseRedisService.setString("user_" + phoneNum, token);
            return userVo;
        }
    }

    public LifeUser getUserByPhone(String phoneNum) {
        LambdaQueryWrapper<LifeUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LifeUser::getUserPhone, phoneNum);
        return this.getOne(lambdaQueryWrapper);
    }

}
