package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
@PermitAll
@RestController
@RequestMapping(value = "bohe")
public class BoHeController extends BaseController {

    @Autowired
    private IBoheUserService boheUserService;
    @Autowired
    private IBhCategoriesService bhCategoriesService;

    @Autowired
    private IBhFoodService bhFoodService;
    @Autowired
    private IBhCaloriesService bhCaloriesService;
    @Autowired
    private IBhMoveService bhMoveService;
    @Autowired
    private IBhLoveFoodService loveFoodService;

    @Autowired
    private IBhALoveService aLoveService;
    @Autowired
    private IBhArticeService articeService;

    @PermitAll
    @PostMapping("/boheLogin")
    public AjaxResult boheLogin(@RequestBody BoheUser boheUser) {
        System.out.println(boheUser);
        BoheUser boheUser1 = new BoheUser();
        boheUser1.setUsername(boheUser.getUsername());
        List<BoheUser> boheUsers =  boheUserService.selectBoheUserList(boheUser1);
        if(boheUsers.size()==0){
            return AjaxResult.error(" 账号不正确 ");
        }
        boheUser1 = boheUsers.get(0);
        if(boheUser1.getPassword().equals(boheUser.getPassword())){
            return success(boheUser1);
        }
        return AjaxResult.error("账号或密码错误！");

    }

    /**
     * 新增薄荷用户管理
     */
    @PermitAll
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BoheUser boheUser) {
        BoheUser old = new BoheUser();
        old.setUsername(boheUser.getUsername());

        if (boheUserService.selectBoheUserList(old).size() != 0) {
            return AjaxResult.error("用户名已被注册");
        }

        int i = boheUserService.insertBoheUser(boheUser);
        if(i == 1){
            return AjaxResult.success(boheUser);
        }

        return AjaxResult.error("注册失败");

    }


    /**
     *  添加摄入和运动消耗
     * @param calories
     * @return
     */
    @PermitAll
    @PostMapping(value = "addCalory")
    public AjaxResult addCalory(@RequestBody BhCalories calories){


        if(calories.getUid() != null && calories.getUid() > 0){
            BhCalories old = new BhCalories();

            old.setcId(calories.getcId());
            old.setCreateTime(calories.getCreateTime());
            old.setUid(calories.getUid());
            old.setcType(calories.getcType());
            //检测一下今天是否 同样的食物并且同一类型（早中晚餐）
            List<BhCalories> oldList = bhCaloriesService.selectBhCaloriesList(old);
            if(oldList.size() > 0){
                // 数组结果大于0说明添加过，那就取第一个更新一下 摄入热量 和 食物重量
                oldList.get(0).setcValue(calories.getcValue() + oldList.get(0).getcValue());
                oldList.get(0).setcNum(calories.getcNum() + oldList.get(0).getcNum());
                return toAjax(bhCaloriesService.updateBhCalories(oldList.get(0)));
            }
            return toAjax(bhCaloriesService.insertBhCalories(calories));


        }

        return error(String.valueOf(calories));


    }


    /**
     *  查询分类列表
     * @param
     * @return
     */
    @PermitAll
    @PostMapping(value = "categorisList")
    public AjaxResult categorisList(){
        return AjaxResult.success(bhCategoriesService.selectBhCategoriesList(new BhCategories()));
    }

    /**
     *  查询分类下所有食物
     * @param
     * @return
     */
    @PermitAll
    @PostMapping(value = "byCategorisIdFood")
    public AjaxResult byCategorisIdFood(@RequestBody BhFood food){

        if(food.getPid() == null){
            return AjaxResult.error();
        }
        return AjaxResult.success(bhFoodService.selectBhFoodList(food));
    }


    /**
     *  查询所有 运动项目
     * @param
     * @return
     */
    @PermitAll
    @PostMapping(value = "movesList")
    public AjaxResult movesList(){

        return AjaxResult.success(bhMoveService.selectBhMoveList(null));
    }

    /**
     *  查询所有 运动项目
     * @param
     * @return
     */
    @PermitAll
    @PostMapping(value = "calories")
    public AjaxResult calories(@RequestBody BhCalories bhCalories) throws ParseException {
//        LocalDate date = LocalDate.now();
//
//        // 格式化日期
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        String formattedDate = date.format(formatter);
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        bhCalories.setCreateTime(dateFormat.parse(formattedDate));
        return AjaxResult.success(bhCaloriesService.selectBhCaloriesList(bhCalories));
    }



    /**
     *  查询分类及所属食物列表
     * @param
     * @return
     */
    @PermitAll
    @PostMapping(value = "categorisAndFood")
    public AjaxResult categorisAndFood(){


        List<BhCategories> categories = bhCategoriesService.selectBhCategoriesList(new BhCategories());
        for (BhCategories item: categories
             ) {
            BhFood bhFood = new BhFood();
            bhFood.setPid(item.getId());
            item.setProducts(bhFoodService.selectBhFoodList(bhFood));
            bhFood = null;
        }

        return AjaxResult.success(categories);
    }

    /**
     * 删除 记录的 食物 或者 运动
     * @param bhCalories
     * @return
     * @throws ParseException
     */
    @PermitAll
    @PostMapping(value = "delCalories")
    public AjaxResult delCalories(@RequestBody BhCalories bhCalories) throws ParseException {
        System.out.println(bhCalories);
        if(bhCalories.getcType() == null || bhCalories.getcId() == null || bhCalories.getUid() == null || bhCalories.getcNum() == null || bhCalories.getCreateTime() == null){
            return AjaxResult.error();
        }
        return AjaxResult.success(bhCaloriesService.deleteBhCaloriesByOne(bhCalories));
    }

    /**
     * 查询所有收藏食物
     * @param loveFood
     * @return
     * @throws ParseException
     */
    @PermitAll
    @PostMapping(value = "loveFoods")
    public AjaxResult loveFoods(@RequestBody BhLoveFood loveFood) throws ParseException {


        return AjaxResult.success(loveFoodService.selectBhLoveList(loveFood.getUid()));
    }

    /**
     * 添加收藏食物
     * @param loveFood
     * @return
     * @throws ParseException
     */
    @PermitAll
    @PostMapping(value = "addloveFood")
    public AjaxResult addloveFood(@RequestBody BhLoveFood loveFood)  {
        return AjaxResult.success(loveFoodService.insertBhLoveFood(loveFood));
    }

    /**
     * 删除 收藏的食物
     * @param loveFood
     * @return
     */
    @PermitAll
    @PostMapping(value = "deleteBhLove")
    public AjaxResult deleteBhLove(@RequestBody BhLoveFood loveFood)  {
        return AjaxResult.success(loveFoodService.deleteBhLove(loveFood));
    }


    /**
     * 检测是否收藏当前食物 1是 0否
     * @param loveFood
     * @return
     */
    @PermitAll
    @PostMapping(value = "loveStatus")
    public AjaxResult loveStatus(@RequestBody BhLoveFood loveFood)  {
        return AjaxResult.success(loveFoodService.selectBhLoveFoodList(loveFood).size() > 0 ? 1 : 0);
    }




    /**
     * 获取当前用户信息
     * @param userInfo
     * @return
     */
    @PermitAll
    @PostMapping(value = "userInfo")
    public AjaxResult userInfo(@RequestBody BoheUser user)  {
        return AjaxResult.success(boheUserService.selectBoheUserById(user.getId()));
    }


    /**
     * 获取当前用户信息
     * @param updateUserInfo
     * @return
     */
    @PermitAll
    @PostMapping(value = "updateUserInfo")
    public AjaxResult updateUserInfo(@RequestBody BoheUser user)  {
        return AjaxResult.success(boheUserService.updateBoheUser(user));
    }


    /**
     * 查询社区列表
     */
    @PostMapping("/Articelist")
    public List<BhArtice> Articelist(BhArtice bhArtice)
    {
        List<BhArtice> list = articeService.selectBhArticeList(bhArtice);
        for (BhArtice item: list
             ) {
            BhALove love = new BhALove();
            love.setUid(bhArtice.getUserid());
            love.setAid(item.getId());
            item.setStatus(aLoveService.selectBhALoveList(love).size() > 0);
        }
        return list;
    }


    /**
     * 新增社区
     */
    @PostMapping("/bhArticeadd")
    public AjaxResult bhArticeadd(@RequestBody BhArtice bhArtice)
    {
        return toAjax(articeService.insertBhArtice(bhArtice));
    }


    /**
     * 新增点赞记录
     */
    @PostMapping("/bhALoveadd")
    public AjaxResult bhALoveadd(@RequestBody BhALove bhALove)
    {
        System.out.println(bhALove);

        try {
            aLoveService.insertBhALove(bhALove);
        }catch (Exception e){
            aLoveService.deleteBhALoveByUid(bhALove);
        }


        BhArtice a = articeService.selectBhArticeById(bhALove.getAid());
        BhALove love = new BhALove();
        love.setUid(bhALove.getUid());
        love.setAid(a.getId());
        a.setStatus(aLoveService.selectBhALoveList(love).size() > 0);
        return AjaxResult.success(a);
    }


}
