package com.cirle.scientific.controller.index;

import com.cirle.scientific.dto.HomeDataDTO;
import com.cirle.scientific.dto.QkjPositionsDTO;
import com.cirle.scientific.pojo.Result;
import com.cirle.scientific.service.HomeService;
import com.cirle.scientific.service.RecommendService;
import com.cirle.scientific.utils.DeviceUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping("/web/index")
@Api("首页相关测试")
public class PcIndexController {

    @Autowired
    private RecommendService recommendService;

    @Autowired
    private HomeService homeService;


    @PostMapping("/information")
    @ApiOperation("首页资讯接口")
    @ResponseBody
    public Result<List<QkjPositionsDTO>> getInformation(
            @RequestAttribute(value = "memCard",required = false) String memCard, HttpServletRequest request) {
        // 默认展示pc端内容  0全部不显示 、1全部显示 、2m端显示、3pc端显示
        Integer status = 3;
        if(DeviceUtil.isMobileDevice(request)) {
            status = 2;
        }
        List<QkjPositionsDTO> qkjPositionsDTOList = new ArrayList<>();
        //long array[] = {8,9,10};
        long array[] = {27,9,10};
        for (long l : array) {
            qkjPositionsDTOList.add(recommendService.findByPosId(l,status));
        }
        return Result.success(qkjPositionsDTOList);
    }

    @PostMapping("/homeData")
    @ApiOperation("Web首页接口数据")
    @ResponseBody
    public Result<HomeDataDTO> homeData(
            @RequestAttribute(value = "memCard",required = false) String memCard,HttpServletRequest request) {
        // 默认展示pc端内容  0全部不显示 、1全部显示 、2m端显示、3pc端显示
        Integer status = 3;
        if(DeviceUtil.isMobileDevice(request)) {
            status = 2;
        }
        Result<HomeDataDTO> homeData = homeService.getHomeData(memCard, status);
        List<QkjPositionsDTO> children = homeData.getData().getSeriesRecommends().getChildren();
        for (Iterator<QkjPositionsDTO> iterator=children.iterator(); iterator.hasNext();) {
            QkjPositionsDTO next = iterator.next();
            if (next.getPosName().contains("资讯")){
                iterator.remove();
            }
        }
        return homeData;
    }
}
