package com.cirle.scientific.controller.courseware;


import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Lists;
import com.cirle.scientific.dto.QkjCourseWareDto;
import com.cirle.scientific.dto.QkjRecommendDTO;
import com.cirle.scientific.po.QkjCourseWare;
import com.cirle.scientific.pojo.BizCodeEnum;
import com.cirle.scientific.pojo.Result;
import com.cirle.scientific.service.QkjCourseWareService;
import com.cirle.scientific.service.RecommendService;
import com.cirle.scientific.utils.DeviceUtil;
import com.cirle.scientific.utils.UserLoginUtil;
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.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/courseware")
@Api("课件相关接口")
@Slf4j
public class CourseWareController {

    @Autowired
    QkjCourseWareService courseWareService;

    @Autowired
    RecommendService recommendService;

    @GetMapping("/index")
    @ApiOperation(value = "去往课件列表页")
    public String index(HttpServletRequest request,
            @ApiParam(name = "from",value = "请求类型 web：网站 H5：手机端") @RequestParam(name = "from",required = true) String from, Model model
    ){
        String refererUrl = request.getHeader("Referer");
        if(refererUrl!=null && refererUrl.contains("login")){
            model.addAttribute("fromlogin","1");
        }else{
            model.addAttribute("fromlogin","0");
        }
        if (StringUtils.isNotBlank(from) && from.equals("H5")) {
            return "html5/courseware/courseware";
        } else {
            return "web/courseware/courseware";
        }
    }

    @GetMapping("/details")
    @ApiOperation(value = "去往课件详情页")
    public String detail(
            HttpServletRequest request,
            @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
    ) {
        request.setAttribute("id", id);
        if (StringUtils.isNotBlank(from) && from.equals("H5")) {
            return "html5/courseware/courseware-details";
        } else {
            return "web/courseware/courseware-details";
        }
    }

    @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("/getlistData")
    @ApiOperation(value = "获取课件列表")
    @ResponseBody
    public Result getArtListData(
            @ApiParam(name = "memCard",value = "memCard") @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "cataId",value = "分类id") @RequestParam(name = "cataId",required = false) Integer cataId,
            @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 courseWareService.getListData(memCard, cataId, page, size);
    }

    @PostMapping("/getPageData")
    @ApiOperation(value = "获取课件列表分页")
    @ResponseBody
    public ResultPage getPageData(
            HttpServletRequest request,
            @ApiParam(name = "memCard",value = "memCard") @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "cataId",value = "分类id") @RequestParam(name = "cataId",required = false) Integer cataId,
            @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
    ){
        if (request.getSession().getAttribute("UserCard") != null) {
            memCard = (String) request.getSession().getAttribute("UserCard");
        }
        return courseWareService.getPageData(memCard, cataId, page, size);
    }

    @GetMapping("/downFilebyWeb")
    @ApiOperation(value = "web下载课件")
    public String downFilebyWeb(
            HttpServletResponse response, HttpServletRequest request,
            @ApiParam(name = "id",value = "课件ID") @RequestParam(name = "id",required = true) Long id
    ){
        if(request.getSession().getAttribute("UserCard")==null){
            return "web/user/login";
        }
        String memCard = (String)request.getSession().getAttribute("UserCard");
        courseWareService.downFile(response, memCard, id);
        return null;
    }

    @GetMapping("/downFile")
    @ApiOperation(value = "下载课件")
    public String downFile(
            HttpServletResponse response,
            @ApiParam(name = "memCard",value = "memCard") @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "id",value = "课件ID") @RequestParam(name = "id",required = true) Long id
    ){
        if(StringUtils.isBlank(memCard)){
            return "html5/user/login";
        }
        courseWareService.downFile(response, memCard, id);
        return null;
    }
    //自传课件相关接口
    @GetMapping("/getlistData/forme")
    @ApiOperation(value = "获取自传课件列表")
    @ResponseBody
    public ResultPage getArtListDataForMe(
            HttpServletRequest request,
            @ApiParam(name = "memCard", value = "memCard") @RequestAttribute(value = "memCard", required = false) String memCard,
            @ApiParam(name = "belongBrandid", value = "品牌ID") @RequestParam(name = "belongBrandid", required = false) Integer belongBrandid,
            @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
    ) {
        if (StringUtils.isBlank(memCard)) {//pc从session去 m从参数取
            memCard = UserLoginUtil.isUserLogin(request);
        }
        return courseWareService.getListDataForMe(memCard, belongBrandid, page, size);
    }

    /**
     * QkjCourseWare
     *
     * @param resources
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation("新增课件")
    @ResponseBody
    public Result createQkjCourseware(
            HttpServletRequest request,
            @RequestAttribute(value = "memCard", required = false) String memCard,
            QkjCourseWareDto resources) {
        if (StringUtils.isBlank(memCard)) {//pc从session去 m从参数取
            memCard = UserLoginUtil.isUserLogin(request);
        }
        resources.setUploader(memCard);
        return courseWareService.add(resources);
    }

    @PostMapping(value = "/delete")
    @ApiOperation("删除自传课件")
    @ResponseBody
    public Result delete(@ApiParam(name = "courseWareId", value = "课件ID") @RequestParam(name = "courseWareId", required = false) Long courseWareId) {

        log.info("delete join courseWareId:{}", courseWareId);
        if (Objects.isNull(courseWareId)) {
            return Result.failed(BizCodeEnum.PARAM_NULL);
        }
        return courseWareService.deleteAll(Lists.newArrayList(courseWareId));
    }

    @GetMapping("/downFile/ppt/web")
    @ApiOperation(value = "下载课件")
    public String downFilePptWeb(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(name = "memCard",value = "memCard") @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "id",value = "课件ID") @RequestParam(name = "id",required = true) Long id
    ){
        if (StringUtils.isBlank(memCard)) {//pc从session去 m从参数取
            memCard = UserLoginUtil.isUserLogin(request);
        }
        if(StringUtils.isBlank(memCard)){
            return "web/user/login";
        }
        courseWareService.downFileForPPt(response, memCard, id);
        return null;
    }

    @GetMapping("/downFile/ppt")
    @ApiOperation(value = "下载课件")
    public String downFilePpt(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(name = "memCard",value = "memCard") @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "id",value = "课件ID") @RequestParam(name = "id",required = true) Long id
    ){
        if (StringUtils.isBlank(memCard)) {//pc从session去 m从参数取
            memCard = UserLoginUtil.isUserLogin(request);
        }
        if(StringUtils.isBlank(memCard)){
            return "html5/user/login";
        }
        courseWareService.downFileForPPt(response, memCard, id);
        return null;
    }

    @GetMapping("/downFile/ppt/batch/web")
    @ApiOperation(value = "批量下载课件")
    public String downFilePptBatchWeb(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(name = "memCard",value = "memCard") @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "ids",value = "课件ID集合") @RequestBody List<Integer> ids
    ){
        if (StringUtils.isBlank(memCard)) {//pc从session去 m从参数取
            memCard = UserLoginUtil.isUserLogin(request);
        }
        if(StringUtils.isBlank(memCard)){
            return "web/user/login";
        }
        courseWareService.downFileForPPtBatch(response, ids);
        return null;
    }
    @GetMapping("/downFile/ppt/batch")
    @ApiOperation(value = "下载课件")
    public String downFilePptBatch(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(name = "memCard",value = "memCard") @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "ids",value = "课件ID集合")  @RequestBody List<Integer> ids
    ){
        if (StringUtils.isBlank(memCard)) {//pc从session去 m从参数取
            memCard = UserLoginUtil.isUserLogin(request);
        }
        if(StringUtils.isBlank(memCard)){
            return "html5/user/login";
        }
        courseWareService.downFileForPPtBatch(response, ids);
        return null;
    }




}
