package com.YuyeTech.HeartRate.game.food.controller;

import com.YuyeTech.HeartRate.VO.ResultVO;
import com.YuyeTech.HeartRate.common.controller.*;
import com.YuyeTech.HeartRate.common.pojo.PageInfo;
import com.YuyeTech.HeartRate.enums.DeviceEnum;
import com.YuyeTech.HeartRate.game.bluetoothinfo.projo.BluetoothInfo;
import com.YuyeTech.HeartRate.game.food.pojo.Food;
import com.YuyeTech.HeartRate.game.food.vo.FoodSimpleVo;
import com.YuyeTech.HeartRate.game.food.vo.FoodVo;
import com.YuyeTech.HeartRate.game.food.service.FoodService;
import com.YuyeTech.HeartRate.game.foodRecommend.service.FoodRecommendService;
import com.YuyeTech.HeartRate.game.foodRecommend.vo.FoodRecommendVo;
import com.YuyeTech.HeartRate.utils.CopyUtil;
import com.YuyeTech.HeartRate.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/food/")
public class FoodController extends CommonController<FoodVo, Food, Integer> {
    @Autowired
    private FoodService foodService;
    @Autowired
    private FoodRecommendService foodRecommendService;


    /**
     * 直接查询服务器缓存数据，数据量不够时候
     * 直接查询数据库数据，数据量不够时候
     * 直接查询薄荷网数据，同步更新到缓存和数据库中
     * @param keyword
     * @param page
     * @return
     */
    @GetMapping(value = "/searchFromKeyword")
    public ResultVO searchFromKeyword(@RequestParam(value = "keyword") String keyword,
                                    @RequestParam(value = "page" ,defaultValue = "1") Integer page){

        String url = "/food/search?keyword="+keyword+"&page="+page;
        return  ResultVOUtil.success(foodService.getListFromUrl(url));
    }

    /**
     * 直接查询服务器缓存数据，数据量不够时候
     * 直接查询数据库数据，数据量不够时候
     * 直接查询薄荷网数据，同步更新到缓存和数据库中
     * @param group
     * @param page
     * @return
     */
    @GetMapping(value = "/searchFromGroup")
    public ResultVO searchFromGroup(@RequestParam(value = "group") Integer group,
                                      @RequestParam(value = "page" ,defaultValue = "1") Integer page){

        String url = "/food/group/"+group +"&page="+page;
        return  ResultVOUtil.success(foodService.getListFromUrl(url));
    }

    /**
     * 按照类型获取推荐数据
     * @param type
     * @return
     */
    @GetMapping(value = "/byType")
    public ResultVO onGetByType(@RequestParam(value = "type") Integer type){
        FoodRecommendVo foodRecommendVo = new FoodRecommendVo();
        foodRecommendVo.setType(type);
        //推荐列表
        List<FoodRecommendVo> foodRecommendVos = foodRecommendService.listVo(foodRecommendVo);
        List<Integer> ids = foodRecommendVos.stream().map(e->e.getFoodId()).collect(Collectors.toList());
        //根据id列表获取信息
        List<Food> foodList = foodService.getListByIds(ids);
        List<FoodSimpleVo> foodSimpleVos = CopyUtil.copyList(foodList, FoodSimpleVo.class);
        return  ResultVOUtil.success(foodSimpleVos);
    }

}
