package com.njcn.system.controller;


import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.system.pojo.param.ThemeParam;
import com.njcn.system.pojo.po.Theme;
import com.njcn.system.service.IThemeService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hongawen
 * @since 2021-12-13
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/theme")
@Api(tags = "主题管理")
@AllArgsConstructor
public class ThemeController extends BaseController {

    private final IThemeService themeService;

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getAllThemes")
    @ApiOperation("主题列表")
    public HttpResult<List<Theme>> getAllThemes(){
        String methodDescribe = getMethodDescribe("getAllThemes");
        List<Theme> list = themeService.getAllThemes();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,list,methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getTheme")
    @ApiOperation("当前主题")
    public HttpResult<Theme> getTheme(){
        String methodDescribe = getMethodDescribe("getTheme");
        Theme theme = themeService.getTheme();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,theme,methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON,operateType = OperateType.ADD)
    @PostMapping("/addTheme")
    @ApiOperation("新增主题")
    public HttpResult<Boolean> addTheme(@Validated ThemeParam theme) {
        String methodDescribe = getMethodDescribe("addTheme");
        boolean result = themeService.addTheme(theme);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON,operateType = OperateType.UPDATE)
    @PutMapping("/updateTheme")
    @ApiOperation("修改主题")
    public HttpResult<Boolean> updateTheme(@Validated ThemeParam.ThemeUpdateParam theme) {
        String methodDescribe = getMethodDescribe("updateTheme");
        boolean result = themeService.updateTheme(theme);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON,operateType = OperateType.DELETE)
    @DeleteMapping("/deleteTheme")
    @ApiOperation("删除主题")
    @ApiImplicitParam(name = "id", value = "主题id", required = true)
    public HttpResult<Boolean> deleteTheme(@RequestParam @Validated String id) {
        String methodDescribe = getMethodDescribe("deleteTheme");
        LogUtil.njcnDebug(log, "{}，删除的主题id为：{}", methodDescribe, id);
        boolean result = themeService.deleteTheme(id);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON,operateType = OperateType.UPDATE)
    @PutMapping("/activateTheme")
    @ApiOperation("激活主题")
    @ApiImplicitParam(name = "id", value = "主题id", required = true)
    public HttpResult<Boolean> activateTheme(@RequestParam @Validated String id) {
        String methodDescribe = getMethodDescribe("activateTheme");
        LogUtil.njcnDebug(log, "{}，启用的主题id为：{}", methodDescribe, id);
        boolean result = themeService.activateTheme(id);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }



}

