package com.cirle.scientific.controller.meeting;


import com.cirle.scientific.dto.QkjPhotoWallResDto;
import com.cirle.scientific.dto.QkjRecommendDTO;
import com.cirle.scientific.po.QkjConference;
import com.cirle.scientific.pojo.BizCodeEnum;
import com.cirle.scientific.pojo.Result;
import com.cirle.scientific.service.MeetingService;
import com.cirle.scientific.service.QkjPhotoWallService;
import com.cirle.scientific.service.RecommendService;
import com.cirle.scientific.utils.DeviceUtil;
import com.cirle.scientific.utils.IpUtil;
import com.cirle.scientific.utils.page.ResultPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

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

@Controller
@RequestMapping("/meeting")
@Api("会议相关接口")
@Slf4j
public class MeetingController {

    private QkjPhotoWallService qkjPhotoWallDto;

    @Autowired
    private MeetingService meetingService;

    @Autowired
    private RecommendService recommendService;

    @Autowired
    public void setQkjPhotoWallDto(QkjPhotoWallService qkjPhotoWallDto) {
        this.qkjPhotoWallDto = qkjPhotoWallDto;
    }

    @GetMapping("/index")
    @ApiOperation(value = "去往会议首页")
    public String courseIndex( @ApiParam(name = "from",value = "请求类型 web：网站 H5：手机端") @RequestParam(name = "from",required = true) String from){
        if (StringUtils.isNotBlank(from) && from.equals("H5")) {
            return "html5/meeting/meeting-index";
        } else {
            return "web/meeting/meeting-index";
        }
    }

    @GetMapping("/details")
    @ApiOperation(value = "去往会议详情页")
    public String details(
            HttpServletRequest request,
            @ApiParam(name = "from",value = "请求类型 web：网站 H5：手机端") @RequestParam(name = "from",required = true) String from,
            @ApiParam(name = "conferId",value = "分类ID") @RequestParam(name = "conferId",required = true) Long conferId, Model model
    ){
        String refererUrl = request.getHeader("Referer");
        if(refererUrl!=null && refererUrl.contains("login")){
            model.addAttribute("fromlogin","1");
        }else{
            model.addAttribute("fromlogin","0");
        }
        QkjConference info = meetingService.getInfoById(conferId);
        request.setAttribute("conferId",conferId);
        if (StringUtils.isNotBlank(from) && from.equals("H5")) {
            if(info.getType() == 1){
                //系列会议页面
                return "html5/meeting/meeting-prefecture-list";
            }else{
                //普通会议页面
                return "html5/meeting/meeting-details";
            }
        } else {
            if(info.getType() == 1){
                //系列会议页面
                return "web/meeting/meeting-prefecture-list";
            }else{
                //普通会议页面
                return "web/meeting/meeting-details";
            }
        }
    }

    @GetMapping("/addUser")
    @ApiOperation(value = "添加用户")
    public String addUser( @ApiParam(name = "from",value = "请求类型 web：网站 H5：手机端") @RequestParam(name = "from",required = true) String from,
                           @ApiParam(name = "conferId",value = "分类ID") @RequestParam(name = "conferId",required = true) Long conferId,
                           HttpServletRequest request){
        request.setAttribute("conferId",conferId);
        if (StringUtils.isNotBlank(from) && from.equals("H5")) {
            return "html5/meeting/meeting-add-user";
        } else {
            return "web/meeting/meeting-add-user";
        }
    }

    @GetMapping("/photoWall")
    @ApiOperation(value = "照片墙")
    public String photoWall( @ApiParam(name = "from",value = "请求类型 web：网站 H5：手机端") @RequestParam(name = "from",required = true) String from,
                           @ApiParam(name = "id",value = "会议ID") @RequestParam(name = "id",required = true) Long id,
                           HttpServletRequest request){
        request.setAttribute("id",id);
        if (StringUtils.isNotBlank(from) && from.equals("H5")) {
            return "html5/meeting/photo-wall";
        } else {
            return "web/meeting/photo-wall";
        }
    }

    @GetMapping("/meetSupport")
    @ApiOperation(value = "项目支持")
    public String meetSupport( @ApiParam(name = "from",value = "请求类型 web：网站 H5：手机端") @RequestParam(name = "from",required = true) String from,
                             @ApiParam(name = "id",value = "会议ID") @RequestParam(name = "id",required = true) Long id,
                             HttpServletRequest request){
        request.setAttribute("id",id);
        if (StringUtils.isNotBlank(from) && from.equals("H5")) {
            return "html5/meeting/meeting-support";
        } else {
            return "web/meeting/meeting-support";
        }
    }

    @PostMapping("/getAdInfo")
    @ResponseBody
    @ApiOperation(value = "直播首页获取广告位")
    public Result<List<QkjRecommendDTO>> getAdInfo(HttpServletRequest request) {
        // 默认展示pc端内容  0全部不显示 、1全部显示 、2m端显示、3pc端显示
        Integer status = 3;
        if(DeviceUtil.isMobileDevice(request)) {
            status = 2;
        }
        List<QkjRecommendDTO> recmdTop = recommendService.findByPosIdLimit(19L,status, 1, 10);
        return Result.success(recmdTop);
    }

    @PostMapping("/getConferList")
    @ApiOperation(value = "获取首页直播列表")
    @ResponseBody
    public Result getConferList(
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "page",value = "分页") @RequestParam(name = "page",required = true,defaultValue="1") Integer page,
            @ApiParam(name = "size",value = "每页数量") @RequestParam(name = "size",required = true,defaultValue="10") Integer size
    ){
        return meetingService.getListData(memCard, page, size);
    }

    @GetMapping("/getPhotoWall")
    @ApiOperation(value = "查看照片墙")
    @ResponseBody
    public Result getPhotoWall(@ApiParam(name = "page",value = "分页") @RequestParam(name = "page",required = true,defaultValue="1") Integer page,
                                               @ApiParam(name = "size",value = "每页数量") @RequestParam(name = "size",required = true,defaultValue="10") Integer size,
                                               @ApiParam(name = "id",value = "会议id") @RequestParam(name = "id",required = true) Integer id,
                                               @ApiParam(name = "type",value = "类型") @RequestParam(name = "type",required = true) Integer type){
        Map<String, Object> all = qkjPhotoWallDto.findAll(page, size, id, type);
        return Result.success(all);
    }

    @PostMapping("/getPageData")
    @ApiOperation(value = "获取会议列表web")
    @ResponseBody
    public ResultPage getPageData(
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "page",value = "分页") @RequestParam(name = "page",required = true,defaultValue="1") Integer page,
            @ApiParam(name = "size",value = "每页数量") @RequestParam(name = "size",required = true,defaultValue="10") Integer size,
            HttpServletRequest request
    ){
        boolean isPC= DeviceUtil.isMobileDevice(request);
        return meetingService.getPageData(memCard, page, size,isPC);
    }

    @PostMapping("/getDetailData")
    @ApiOperation(value = "获取会议详情")
    @ResponseBody
    public Result getDetailData(
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "conferId",value = "会议id") @RequestParam(name = "conferId") Long conferId
    ){
        return meetingService.getDetailData(conferId, memCard);
    }

    @PostMapping("/getPeopleList")
    @ApiOperation(value = "获取会议报名人员信息")
    @ResponseBody
    public Result getPeopleList(
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "conferId",value = "会议id") @RequestParam(name = "conferId") Long conferId
    ){
        if(StringUtils.isBlank(memCard)){
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }
        return meetingService.getPeopleList(conferId, memCard);
    }

    @PostMapping("/pursePeopleBm")
    @ApiOperation(value = "判断参会人是否已报名")
    @ResponseBody
    public Result pursePeopleBm(
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "conferId",value = "会议id") @RequestParam(name = "conferId") Long conferId,
            @ApiParam(name = "peopleList",value = "参会人数据") @RequestParam(name = "peopleList") String peopleList
    ){
        if(StringUtils.isBlank(memCard)){
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }
        return meetingService.pursePeopleBm(memCard, conferId, peopleList);
    }

    @PostMapping("/peopleBm")
    @ApiOperation(value = "判断参会人是否已报名")
    @ResponseBody
    public Result peopleBm(
            HttpServletRequest request,
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "conferId",value = "会议id") @RequestParam(name = "conferId") Long conferId,
            @ApiParam(name = "peopleList",value = "参会人数据") @RequestParam(name = "peopleList") String peopleList
    ){
        if(StringUtils.isBlank(memCard)){
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }
        return meetingService.peopleBm(memCard, conferId, peopleList, IpUtil.getIpAddr(request));
    }

    @PostMapping("/pursePeopleBmWeb")
    @ApiOperation(value = "判断参会人是否已报名Web")
    @ResponseBody
    public Result pursePeopleBmWeb(
            HttpServletRequest request,
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "conferId",value = "会议id") @RequestParam(name = "conferId") Long conferId,
            @ApiParam(name = "peopleList",value = "参会人数据") @RequestParam(name = "peopleList") String peopleList
    ){
        if (request.getSession().getAttribute("UserCard") != null) {
            memCard = (String) request.getSession().getAttribute("UserCard");
        }else {
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }

        return meetingService.pursePeopleBm(memCard, conferId, peopleList);
    }
    @PostMapping("/peopleBmWeb")
    @ApiOperation(value = "判断参会人是否已报名Web")
    @ResponseBody
    public Result peopleBmWeb(
            HttpServletRequest request,
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "conferId",value = "会议id") @RequestParam(name = "conferId") Long conferId,
            @ApiParam(name = "peopleList",value = "参会人数据") @RequestParam(name = "peopleList") String peopleList
    ){
        if (request.getSession().getAttribute("UserCard") != null) {
            memCard = (String) request.getSession().getAttribute("UserCard");
        }else {
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }
        return meetingService.peopleBm(memCard, conferId, peopleList, IpUtil.getIpAddr(request));
    }

    @GetMapping("/finish")
    @ApiOperation(value = "报名成功跳转页面")
    public String finish(  HttpServletRequest request,
                           @ApiParam(name = "from",value = "请求类型 web：网站 H5：手机端") @RequestParam(name = "from",required = true) String from,
                           @ApiParam(name = "conferId",value = "分类ID") @RequestParam(name = "conferId",required = true) Long conferId){
        request.setAttribute("conferId",conferId);

        if (StringUtils.isNotBlank(from) && from.equals("H5")) {
            return "html5/meeting/finish";
        } else {
            return "web/meeting/finish";
        }
    }


    @PostMapping("/getQuestionList")
    @ApiOperation(value = "获取问卷信息")
    @ResponseBody
    public Result getQuestionList(
            HttpServletRequest request,
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "conferId",value = "会议id") @RequestParam(name = "conferId") Long conferId
    ){
        if (request.getSession().getAttribute("UserCard") != null) {
            memCard = (String) request.getSession().getAttribute("UserCard");
        }else {
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }
        return meetingService.getQuestionList(conferId, memCard);
    }

    @PostMapping("/getDetailDataWeb")
    @ApiOperation(value = "获取会议详情web")
    @ResponseBody
    public Result getDetailDataWeb(
            HttpServletRequest request,
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "conferId",value = "会议id") @RequestParam(name = "conferId") Long conferId
    ){
        if (request.getSession().getAttribute("UserCard") != null) {
            memCard = (String) request.getSession().getAttribute("UserCard");
        } else {
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }
        return meetingService.getDetailData(conferId, memCard);
    }
}
