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

import cn.hutool.core.date.DateUtil;
import com.YuyeTech.HeartRate.VO.ResultVO;
import com.YuyeTech.HeartRate.common.controller.*;
import com.YuyeTech.HeartRate.common.pojo.PageInfo;
import com.YuyeTech.HeartRate.enums.FoodEnum;
import com.YuyeTech.HeartRate.game.food.pojo.Food;
import com.YuyeTech.HeartRate.game.food.service.FoodService;
import com.YuyeTech.HeartRate.game.food.vo.FoodSimpleVo;
import com.YuyeTech.HeartRate.game.food.vo.FoodVo;
import com.YuyeTech.HeartRate.game.foodRecommend.pojo.FoodRecommend;
import com.YuyeTech.HeartRate.game.foodRecommend.vo.FoodRecommendVo;
import com.YuyeTech.HeartRate.game.foodRecommend.service.FoodRecommendService;
import com.YuyeTech.HeartRate.utils.CopyUtil;
import com.YuyeTech.HeartRate.utils.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/foodRecommend/")
public class FoodRecommendController extends CommonController<FoodRecommendVo, FoodRecommend, Integer> {
    @Autowired
    private FoodRecommendService foodRecommendService;

    @Autowired
    private FoodService foodService;

    /**
     * 按照类型获取推荐数据
     * @param proType
     * @return
     */
    @GetMapping(value = "/byType")
    public ResultVO onGetByType(@RequestParam(value = "proType") Integer proType){
        FoodRecommendVo foodRecommendVo = new FoodRecommendVo();
        foodRecommendVo.setType(proType);
        //推荐列表
        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);
        Collections.sort(foodSimpleVos,(a, b)->{
            return (int) (a.getKcal() - b.getKcal());
        });
        return  ResultVOUtil.success(foodSimpleVos);
    }

    /**
     *  生成推荐数据
     *  碳水推荐：燕麦、大米、小米、紫薯、糯米、薏米
     *  蛋白质推荐：鸡蛋、鸡胸肉、牛肉、
     *  蔬菜：生菜、黄瓜、番茄、洋葱、胡萝卜、白菜、玉米
     *  油    ：椰子油、橄榄油、菜油、花生油
     * @return
     */
//    @GetMapping(value = "/generate")
    public ResultVO onGenerate() throws IOException{
        List<String> CARBOHYDRATES = new ArrayList<>();
        CARBOHYDRATES.add("燕麦");
        CARBOHYDRATES.add("大米");
        CARBOHYDRATES.add("小米");
        CARBOHYDRATES.add("紫薯");
        CARBOHYDRATES.add("糯米");
        CARBOHYDRATES.add("薏米");
//        CARBOHYDRATES.add("原兹林 紫薯果");
        query(CARBOHYDRATES,FoodEnum.CARBOHYDRATES.getValue());


        List<String> PROTEINS = new ArrayList<>();
        PROTEINS.add("鸡蛋");
        PROTEINS.add("鸡胸肉");
        PROTEINS.add("牛肉");
        query(PROTEINS,FoodEnum.PROTEINS.getValue());

        List<String> VEGETABLES = new ArrayList<>();
        VEGETABLES.add("生菜");
        VEGETABLES.add("黄瓜");
        VEGETABLES.add("番茄");
        VEGETABLES.add("洋葱");
        VEGETABLES.add("胡萝卜");
        VEGETABLES.add("白菜");
        VEGETABLES.add("玉米");
        query(VEGETABLES,FoodEnum.VEGETABLES.getValue());

        List<String> OILS = new ArrayList<>();
        OILS.add("椰子油");
        OILS.add("橄榄油");
        OILS.add("菜油");
        OILS.add("花生油");
        query(OILS,FoodEnum.OILS.getValue());

        return  ResultVOUtil.success();
    }


    public void query(List<String>  keywords,Integer proType) throws IOException {
        for (int i=0;i<keywords.size();i++){
            String url = "/food/search?keyword="+keywords.get(i)+"&page=1";
            String _encodeKeyword = URLEncoder.encode(keywords.get(i), "UTF-8");
            String refer = "/food/search?keyword="+_encodeKeyword+"&page=1";

            List<FoodSimpleVo> foodSimpleVos = foodService.getListFromUrl(url,refer);
            FoodVo addRecommendItem = null;
            for (FoodSimpleVo item : foodSimpleVos) {
                item.setProType(proType);
                FoodVo foodVo = CopyUtil.copy(item, FoodVo.class);

                FoodSimpleVo foodSimpleVo = foodService.getByFoodTitle(foodVo.getTitle());
                if(foodSimpleVo == null){
                    if(addRecommendItem == null){
//                        FoodVo(id=null, title=燕麦片，又叫燕麦, proType=0, type=shiwu, name=yanmaipian, iconUrl=https://s.boohee.cn/house/upload_food/2021/8/18/small_photo_url_b7579d0f5a211460e8d356a846033f77.jpg, kcal=338.0, carbohydrate=0.0, fat=0.0, protein=0.0, cellulose=0.0, describe=热量：338 大卡(每100克), createTime=2023-06-09 18:08:29, updateTime=2023-06-09 18:08:29)
                        addRecommendItem =  foodService.saveReturnVo(foodVo);
                        FoodRecommendVo foodRecommendVo = new FoodRecommendVo();
                        foodRecommendVo.setType(addRecommendItem.getProType());
                        foodRecommendVo.setFoodId(addRecommendItem.getId());
                        foodRecommendVo.setIsShow(1);
                        foodRecommendVo.setShowTime(DateUtil.date());
                        foodRecommendService.saveReturnVo(foodRecommendVo);
                    } else{
                        foodService.saveReturnVo(foodVo);
                    }
                }

            }
        }
    }

}
