package com.cirle.scientific.controller.popupWindow;


import com.cirle.scientific.pojo.Result;
import com.cirle.scientific.service.CommonService;
import com.cirle.scientific.service.PopuWindowService;
import com.cirle.scientific.service.RecommendService;
import com.cirle.scientific.utils.DeviceUtil;
import com.cirle.scientific.utils.IpUtil;
import com.cirle.scientific.utils.StringUtil;
import com.cirle.scientific.utils.UserLoginUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/popu/window")
@Api("弹窗相关接口")
@Slf4j
public class PopuWIndowController {


    @Autowired
    PopuWindowService popuWindowService;
    @Autowired
    CommonService commonService;
    @Autowired
    RecommendService recommendService;


    @PostMapping("/checkWindow")
    @ResponseBody
    @ApiOperation(value = "校验是否弹窗接口（1：实名认证弹窗 2：医师认证弹窗 3：调查问卷弹窗）")
    public Result checkWindow(HttpServletRequest request,
                              @RequestHeader(value="referer", defaultValue = "0") String referer,
                              @RequestHeader(value="user-agent", defaultValue = "0") String userAgent,
                              @RequestParam(required = false) Long id,
                              @RequestParam(required = false) Integer idType,
                              @ApiParam(name = "memCard", value = "用户唯一标识") @RequestAttribute(name = "memCard", required = false) String memCard
    ) {
        log.info("checkWindow join id:{} idType:{} memCard:{} referer:{} userAgent:{}", id, idType, memCard, referer, userAgent);
        if (StringUtils.isBlank(memCard)) {
            memCard = UserLoginUtil.isUserLogin(request);
        }
//        if (request.getSession().getAttribute("UserCard") != null) {
//            memCard = (String) request.getSession().getAttribute("UserCard");
//        }
        return popuWindowService.checkWindowNew(id, idType, memCard);
    }

    @PostMapping("/checkWindow/question")
    @ResponseBody
    @ApiOperation(value = "校验是否弹调查问卷弹窗）")
    public Result checkWindowQuestion(HttpServletRequest request,
                              @RequestHeader(value="referer", defaultValue = "0") String referer,
                              @RequestHeader(value="user-agent", defaultValue = "0") String userAgent,
                              @RequestParam(required = false) Long id,
                              @RequestParam(required = false) Integer idType,
                              @ApiParam(name = "memCard", value = "用户唯一标识") @RequestAttribute(name = "memCard", required = false) String memCard
    ) {
        log.info("checkWindow join id:{} idType:{} memCard:{} referer:{} userAgent:{}", id, idType, memCard, referer, userAgent);
        if (StringUtils.isBlank(memCard)) {
            memCard = UserLoginUtil.isUserLogin(request);
        }

        return popuWindowService.checkWindowNew(id, idType, memCard);
    }

}
