package com.neusoft.smart.pos.user.controller;

import com.google.code.kaptcha.Producer;
import com.neusoft.smart.pos.dto.user.*;
import com.neusoft.smart.pos.framework.config.RabbitExchangeConfiguration;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.mq.rabbitmq.RabbitMQSender;
import com.neusoft.smart.pos.user.dto.request.LoginRequest;
import com.neusoft.smart.pos.user.dto.request.LogoutRequest;
import com.neusoft.smart.pos.user.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Api(value = "登录接口接口", description = "登录相关的Restful接口。")
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    UserService userService;
    @Autowired
    Producer captchaProducer;
    
    @Autowired
    private RabbitMQSender rabbitMQSender;//TODO

    @PostMapping
    @ApiOperation(value = "用户登录", notes = "根据订单编号查询用户支付状态方法")
    public ResponseData<LoginResponse> login(@Valid @RequestBody LoginRequest loginRequest, @SessionAttribute(value = "captcha", required = false) String captchaId) {
    	LoginResponse response = userService.login(loginRequest.getUsername(), loginRequest.getPassword(), loginRequest.getCaptcha(), captchaId, loginRequest.getSn());
//    	//发送登录通知 TODO
    	LoginInfo info = new LoginInfo();
    	BeanUtils.copyProperties(loginRequest, info);
    	rabbitMQSender.sendMessage(RabbitExchangeConfiguration.FANOUT_EXCHANGE_LOGIN, "", info);
        return new ResponseData<>(response);
    }

    @PostMapping("/thirdPartLogin")
    @ApiOperation(value = "三方用户登录", notes = "使用line等三方账号登录")
    public ResponseData<LoginThirdPartResponse> thirdPartLogin(@Valid @RequestBody LoginThirdPartInfo loginThirdPartInfo) {
        LoginThirdPartResponse response = userService.loginThirdPart(loginThirdPartInfo);
        return new ResponseData<>(response);
    }


    @GetMapping("/queryLastLoginTime")
    @ApiOperation(value = "查询登录时间", notes = "根据设备SN查询上次交接班后的登录时间")
    public ResponseData<QueryLastLoginTimeResponse> queryLastLoginTime(@RequestParam(value = "serialNo", defaultValue = "") String serialNo) {

        return new ResponseData<>(userService.queryLastLoginTime(serialNo));
    }

    @PostMapping("/logout")
    @ApiOperation(value = "用户登出", notes = "根据订单编号查询用户支付状态方法")
    public ResponseData<LogoutResponse> logout(@Valid @RequestBody LogoutRequest logoutRequest) {

        return new ResponseData<>(userService.logout(logoutRequest.getUsername(), logoutRequest.getSn(), logoutRequest.getIsShift()));
    }

    @GetMapping("/captcha")
    @ApiOperation(value = "验证码", notes = "显示验证码图片，每次调用都会刷新")
    public void catpcha(HttpSession session, HttpServletResponse response) throws IOException {
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        String capText = captchaProducer.createText();
        session.setAttribute("captcha", capText);
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }
}
