package com.fs.qw.service.impl;


import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.StringUtils;
import com.fs.qw.domain.QwDevice;
import com.fs.qw.domain.QwOnline;

import com.fs.qw.domain.QwVerify;
import com.fs.qw.event.EventAuthqrcode;
import com.fs.qw.service.IQwAccountService;
import com.fs.qw.service.IQwLoginService;
import com.fs.qw.utils.QwAllUtil;
import com.fs.qw.utils.QwRedisUtil;
import com.fs.qw.utils.QwUtil;
import com.fs.qw.vo.QwLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

import static com.fs.qw.utils.QwAllUtil.*;


@Service
public class QwLoginServiceImpl implements IQwLoginService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private QwRedisUtil qwRedisUtil;
//    @Autowired
//    private QwUtil qwUtil;
    @Autowired
    private RedisCache redisCache;




    @Override
    public QwLoginVO getQrCode(String deviceId) {
        return getqrCode(deviceId);
    }

    @Override
    public R verify(QwVerify qwVerify) {
        String json = redisCache.getCacheObject("qw_login:"+qwVerify.getDeviceId());
        if (StringUtils.isNotEmpty(json)){
            EventAuthqrcode authqrcode = QwAllUtil.GsonFromJson(json,EventAuthqrcode.class);
            if (authqrcode.getStatus()==4){
                qwVerify.setQrCodeKey(authqrcode.getKey());
                Map<String,Object> map = qwVerify.toMap();
                return QwAllUtil.verify(map,qwVerify.getDeviceId());
            }
        }else {
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R ban(String deviceId) {

       return QwAllUtil.ban(deviceId);
    }

    @Override
    public QwOnline getCheck(String deviceId) {
        return QwAllUtil.check(deviceId);
    }

    @Override
    public QwDevice getDevice(String deviceId) {
        return QwAllUtil.authDevice(deviceId);
    }

    @Override
    public int getLoginStatus(String deviceId) {
        String json = qwRedisUtil.HashOpsGet("qwLoginqrcodeConfig", deviceId);
        if (StringUtils.isNotEmpty(json)) {
            EventAuthqrcode authqrcode = QwAllUtil.GsonFromJson(json, EventAuthqrcode.class);
            return authqrcode.getStatus();
        }else {
            return 0;
        }
    }
}
