package com.yolo.keyboard.controller.admin.themes;

import com.yolo.keyboard.controller.admin.themes.vo.KeyboardThemesPageReqVO;
import com.yolo.keyboard.controller.admin.themes.vo.KeyboardThemesRespVO;
import com.yolo.keyboard.controller.admin.themes.vo.KeyboardThemesSaveReqVO;
import com.yolo.keyboard.dal.dataobject.themes.KeyboardThemesDO;
import com.yolo.keyboard.service.themes.KeyboardThemesService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.pojo.CommonResult;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
import static com.yolo.keyboard.framework.common.pojo.CommonResult.success;

import com.yolo.keyboard.framework.excel.core.util.ExcelUtils;

import com.yolo.keyboard.framework.apilog.core.annotation.ApiAccessLog;
import static com.yolo.keyboard.framework.apilog.core.enums.OperateTypeEnum.*;



@Tag(name = "管理后台 - 键盘皮肤")
@RestController
@RequestMapping("/keyboard/themes")
@Validated
public class KeyboardThemesController {

    @Resource
    private KeyboardThemesService themesService;

    @PostMapping("/create")
    @Operation(summary = "创建键盘皮肤")
    @PreAuthorize("@ss.hasPermission('keyboard:themes:create')")
    public CommonResult<Long> createThemes(@Valid @RequestBody KeyboardThemesSaveReqVO createReqVO) {
        return success(themesService.createThemes(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新键盘皮肤")
    @PreAuthorize("@ss.hasPermission('keyboard:themes:update')")
    public CommonResult<Boolean> updateThemes(@Valid @RequestBody KeyboardThemesSaveReqVO updateReqVO) {
        themesService.updateThemes(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除键盘皮肤")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:themes:delete')")
    public CommonResult<Boolean> deleteThemes(@RequestParam("id") Long id) {
        themesService.deleteThemes(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除键盘皮肤")
                @PreAuthorize("@ss.hasPermission('keyboard:themes:delete')")
    public CommonResult<Boolean> deleteThemesList(@RequestParam("ids") List<Long> ids) {
        themesService.deleteThemesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得键盘皮肤")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:themes:query')")
    public CommonResult<KeyboardThemesRespVO> getThemes(@RequestParam("id") Long id) {
        KeyboardThemesDO themes = themesService.getThemes(id);
        return success(BeanUtils.toBean(themes, KeyboardThemesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得键盘皮肤分页")
    @PreAuthorize("@ss.hasPermission('keyboard:themes:query')")
    public CommonResult<PageResult<KeyboardThemesRespVO>> getThemesPage(@Valid KeyboardThemesPageReqVO pageReqVO) {
        PageResult<KeyboardThemesDO> pageResult = themesService.getThemesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardThemesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出键盘皮肤 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:themes:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportThemesExcel(@Valid KeyboardThemesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardThemesDO> list = themesService.getThemesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "键盘皮肤.xls", "数据", KeyboardThemesRespVO.class,
                        BeanUtils.toBean(list, KeyboardThemesRespVO.class));
    }

}