package com.template.controller;


import cn.hutool.core.codec.Base64;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alibaba.fastjson.JSON;
import com.template.annotation.DESRespondSecret;
import com.template.annotation.PassToken;
import com.template.api.SmartQrcodeControllerAPI;
import com.template.common.utils.TimeExchange;
import com.template.model.enumModel.eQrcodeType;
import com.template.model.pojo.SmartUser;
import com.template.model.pojo.SmartVisitor;
import com.template.model.result.CommonResult;
import com.template.model.vo.BaseImageVo;
import com.template.model.vo.QrcodeImageVo;
import com.template.services.SmartUserService;
import com.template.services.SmartVisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * <p>
 * 扫码记录 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-01-18
 */
@RestController
//返回参数加密注解
@DESRespondSecret
public class SmartQrcodeController implements SmartQrcodeControllerAPI {

    @Autowired
    private SmartUserService smartUserService;
    @Autowired
    private SmartVisitorService smartVisitorService;

    @Override
    @DESRespondSecret(validated = true)
    public CommonResult  generateQrcode(int userId) {
        SmartUser user = smartUserService.getSmartById(userId);
        if (user == null) {
            return CommonResult.fail("用户信息无效，无法生成二维码");
        }

        QrcodeImageVo qiv = new QrcodeImageVo();
        qiv.setId(user.getId());
        qiv.setType(eQrcodeType.Identity.getValue());
        qiv.setPhone(user.getPhone());//身份码不需要添加访客数据ID
        qiv.setTimestamp(TimeExchange.getTime());

        String qrStr = JSON.toJSONString(qiv);

        BaseImageVo result = new BaseImageVo();
        result.setQrcode(qrStr);

        return CommonResult.ok(result);
    }

    @Override
    @PassToken
    @DESRespondSecret(validated = true)
    public CommonResult visitorQrcode(String visitorId) {
        if (visitorId == null) {
            return CommonResult.fail("访客code不能为空");
        }

        SmartVisitor visitor = smartVisitorService.querySmartVisitorByCode(visitorId);
        if (visitor == null) {
            return CommonResult.fail("访客数据无效，无法生成二维码");
        }

        if (visitor.getVisitorTime().after(new Date())) {
            return CommonResult.fail("未到来访时间，无法生成二维码");
        }

        if (visitor.getVisitorDeadline().before(new Date())) {
            return CommonResult.fail("访客时间已失效，无法生成二维码");
        }

        QrcodeImageVo qiv = new QrcodeImageVo();
        qiv.setId(visitor.getId());
        qiv.setType(eQrcodeType.Visitor.getValue());
        qiv.setPhone(visitor.getUserPhone());
        qiv.setTimestamp(TimeExchange.getTime());

        String qrStr = JSON.toJSONString(qiv);

        BaseImageVo result = new BaseImageVo();
        result.setQrcode(qrStr);

        return CommonResult.ok(result);
    }

}

