package com.corewing.app.modules.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.corewing.app.common.Result;
import com.corewing.app.common.annotation.CommonLog;
import com.corewing.app.entity.Tutorial;
import com.corewing.app.entity.TutorialCategory;
import com.corewing.app.service.TutorialCategoryService;
import com.corewing.app.service.TutorialService;
import com.corewing.app.util.I18nUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 教程接口
 */
@Api(tags = "教程接口")
@RequestMapping("/tutorial")
@Controller
@Slf4j
public class AppTutorialController {

    private final TutorialService tutorialService;
    private final TutorialCategoryService tutorialCategoryService;

    public AppTutorialController(TutorialService tutorialService, TutorialCategoryService tutorialCategoryService) {
        this.tutorialService = tutorialService;
        this.tutorialCategoryService = tutorialCategoryService;
    }

    /**
     * 跳转到界面查看教程详情
     * @param tutorialId    教程id
     * @param model         数据模型
     * @return  详情页
     */
    @CommonLog("跳转到界面查看教程详情")
    @ApiOperation("跳转到界面查看教程详情")
    @GetMapping("/viewDetail/{tutorialId}")
    public String viewDetail(@PathVariable Long tutorialId, ModelMap model) {
        Tutorial tutorial = tutorialService.getById(tutorialId);
        model.put("tutorial", tutorial);
        return "tutorial/viewDetail";
    }


    /**
     * 添加查看次数
     * @param tutorialId 教程id
     * @return
     */
    @CommonLog("添加查看次数")
    @ApiOperation("添加查看次数")
    @GetMapping("/addViewCount")
    @ResponseBody
    public Result<String> addViewCount(@RequestParam Long tutorialId) {
        Tutorial tutorial = tutorialService.getById(tutorialId);
        if(tutorial == null) {
            return Result.error();
        }
        tutorial.setViewCount(tutorial.getViewCount() + 1);
        tutorialService.updateById(tutorial);
        return Result.success();
    }

    /**
     * 分类查询列表
     *
     * @param firstStatus  置首状态（选填）
     *
     */
    @CommonLog("分类查询集合")
    @ApiOperation("分类查询集合")
    @GetMapping("/category")
    @ResponseBody
    public Result<List<TutorialCategory>> category(
            @RequestParam(required = false, defaultValue = "0") Integer firstStatus
    ) {
        log.info("当前语言环境：{}", I18nUtil.getCurrentLocale().getLanguage());
        LambdaQueryWrapper<TutorialCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(firstStatus != 0, TutorialCategory::getFirstStatus, firstStatus);
        wrapper.eq(TutorialCategory::getLang, I18nUtil.getCurrentLocale().getLanguage());
        List<TutorialCategory> list = tutorialCategoryService.list(wrapper);
        return Result.success(list);
    }

    /**
     * 分页查询教程列表
     *
     * @param current      当前页码
     * @param size         每页数量
     * @param categoryId   分类ID（选填）
     * @param tutorialTitle  教程标题（选填）
     *
     */
    @CommonLog("分页查询教程列表")
    @ApiOperation("分页查询教程列表")
    @GetMapping("/page")
    @ResponseBody
    public Result<IPage<Tutorial>> getPageList(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false, defaultValue = "0") Long categoryId,
            @RequestParam(required = false) String tutorialTitle) {
        try {
            Page<Tutorial> page = new Page<>(current, size);
            Page<Tutorial> pageResult = tutorialService.pageList(page, categoryId, tutorialTitle, I18nUtil.getCurrentLocale().getLanguage());
            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}
