package controllers

import (
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/userstore"
	"github.com/astaxie/beego/server/web"
)

type FoodRecipeController struct {
	web.Controller
}

// @Title 创建菜谱
// @Description 创建菜谱
// @Param	token			header		string	true	"认证token"
// @Param	name			formData	string	true	"菜谱名"
// @Param	description		formData	string	true	"菜谱描述"
// @Param	img				formData	string	true	"图片""
// @Param	timeInMinute	formData	int		true	"大约需要时间（分钟）"
// @Param	recipeItems		formData	string	true	"菜谱配料"
// @Param	recipeSteps		formData	string	true	"菜谱步骤"
// @Param	authorID		formData	string	false	"三方用户ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /CreateFoodRecipe [post]
func (c *FoodRecipeController) CreateFoodRecipe() {
	c.callCreateFoodRecipe(func(params *tFoodrecipeCreateFoodRecipeParams) (retVal interface{}, errCode string, err error) {
		var (
			foodRecipe *model.FoodRecipe
			itemList   []*userstore.FoodRecipeItemParam
			stepList   []*userstore.FoodRecipeStepParam
		)
		if err = jxutils.Strings2Objs(params.RecipeItems, &itemList, params.RecipeSteps, &stepList); err == nil {
			foodRecipe = &model.FoodRecipe{
				Name:         params.Name,
				Description:  params.Description,
				Img:          params.Img,
				TimeInMinute: params.TimeInMinute,
			}
			err = userstore.CreateFoodRecipe(params.Ctx, foodRecipe, itemList, stepList, params.AuthorID)
		}
		return retVal, "", err
	})
}

// @Title 编辑菜谱
// @Description 编辑菜谱
// @Param	token			header		string	true	"认证token"
// @Param	recipeID		formData	int		true	"菜谱ID"
// @Param	name			formData	string	false	"菜谱名"
// @Param	description		formData	string	false	"菜谱描述"
// @Param	img				formData	string	false	"图片""
// @Param	timeInMinute	formData	int		false	"大约需要时间（分钟）"
// @Param	recipeItems		formData	string	false	"菜谱配料"
// @Param	recipeSteps		formData	string	false	"菜谱步骤"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /UpdateFoodRecipe [put]
func (c *FoodRecipeController) UpdateFoodRecipe() {
	c.callUpdateFoodRecipe(func(params *tFoodrecipeUpdateFoodRecipeParams) (retVal interface{}, errCode string, err error) {
		var (
			itemList []*userstore.FoodRecipeItemParam
			stepList []*userstore.FoodRecipeStepParam
		)
		if err = jxutils.Strings2Objs(params.RecipeItems, &itemList, params.RecipeSteps, &stepList); err == nil {
			err = userstore.UpdateFoodRecipe(params.Ctx, params.RecipeID, params.MapData, itemList, stepList)
		}
		return retVal, "", err
	})
}

// @Title 查询菜谱列表
// @Description 查询菜谱列表
// @Param	token			header		string	false	"认证token"
// @Param	keyword			query		string	false	"关键字"
// @Param	authorID		query		string	false	"创建者ID"
// @Param	skuIDs			query		string	false	"skuID列表"
// @Param	offset			query		int		false	"菜谱列表起始序号（以0开始，缺省为0）"
// @Param	pageSize		query		int		false	"菜谱列表页大小（缺省为50，-1表示全部）"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /QueryFoodRecipes [get]
func (c *FoodRecipeController) QueryFoodRecipes() {
	c.callQueryFoodRecipes(func(params *tFoodrecipeQueryFoodRecipesParams) (retVal interface{}, errCode string, err error) {
		var skuIDs []int
		if err = jxutils.Strings2Objs(params.SkuIDs, &skuIDs); err == nil {
			retVal, err = userstore.QueryFoodRecipes(params.Ctx, params.Keyword, params.AuthorID, skuIDs, params.Offset, params.PageSize)
		}
		return retVal, "", err
	})
}

// @Title 得到我的推荐菜谱列表
// @Description 得到我的推荐菜谱列表
// @Param	token			header		string	false	"认证token"
// @Param	keyword			query		string	false	"关键字"
// @Param	offset			query		int		false	"菜谱列表起始序号（以0开始，缺省为0）"
// @Param	pageSize		query		int		false	"菜谱列表页大小（缺省为50，-1表示全部）"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetRecommendFoodRecipes [get]
func (c *FoodRecipeController) GetRecommendFoodRecipes() {
	c.callGetRecommendFoodRecipes(func(params *tFoodrecipeGetRecommendFoodRecipesParams) (retVal interface{}, errCode string, err error) {
		retVal, err = userstore.GetRecommendFoodRecipes(params.Ctx, params.Keyword, params.Offset, params.PageSize)
		return retVal, "", err
	})
}

// @Title 得到菜谱详情
// @Description 得到菜谱详情
// @Param	token			header		string	false	"认证token"
// @Param	recipeID		query		int		true	"菜谱ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetRecipeDetail [get]
func (c *FoodRecipeController) GetRecipeDetail() {
	c.callGetRecipeDetail(func(params *tFoodrecipeGetRecipeDetailParams) (retVal interface{}, errCode string, err error) {
		retVal, err = userstore.GetRecipeDetail(params.Ctx, params.RecipeID)
		return retVal, "", err
	})
}

// @Title 对菜谱投票
// @Description 对菜谱投票
// @Param	token			header		string	true	"认证token"
// @Param	recipeID		formData	int		true	"菜谱ID"
// @Param	voteType		formData	int		true	"-1：踩，0：取消之前的投票，1：赞"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /VoteFoodRecipe [post]
func (c *FoodRecipeController) VoteFoodRecipe() {
	c.callVoteFoodRecipe(func(params *tFoodrecipeVoteFoodRecipeParams) (retVal interface{}, errCode string, err error) {
		err = userstore.VoteFoodRecipe(params.Ctx, params.RecipeID, params.VoteType)
		return retVal, "", err
	})
}

// @Title 删除菜谱
// @Description 删除菜谱
// @Param	token			header		string	true	"认证token"
// @Param	recipeIDs		query		string	true	"菜谱ID列表"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /DeleteRecipes [delete]
func (c *FoodRecipeController) DeleteRecipes() {
	c.callDeleteRecipes(func(params *tFoodrecipeDeleteRecipesParams) (retVal interface{}, errCode string, err error) {
		var recipeIDs []int
		if err = jxutils.Strings2Objs(params.RecipeIDs, &recipeIDs); err == nil {
			retVal, err = userstore.DeleteRecipes(params.Ctx, recipeIDs)
		}
		return retVal, "", err
	})
}
