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.RedisSettingMap;
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.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.URLEncoder;
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) throws IOException {

        String url = "/food/search?keyword="+keyword+"&page="+page;

        String _encodeKeyword = URLEncoder.encode(keyword, "UTF-8");
        String refer = "/food/search?keyword="+_encodeKeyword+"&page="+page;

        List<FoodSimpleVo> foodSimpleVos = foodService.getListFromUrl(url,refer);
        if(foodSimpleVos.size()>0){
            foodService.addKeywordToRedis(keyword);
        }
        return  ResultVOUtil.success(foodSimpleVos);
    }

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

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

}
