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

import com.yolo.keyboard.dal.dataobject.character.CharacterI18nDO;
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.*;

import com.yolo.keyboard.controller.admin.character.vo.*;
import com.yolo.keyboard.dal.dataobject.character.KeyboardCharacterDO;
import com.yolo.keyboard.service.character.KeyboardCharacterService;

@Tag(name = "管理后台 - 键盘人设")
@RestController
@RequestMapping("/keyboard/character")
@Validated
public class KeyboardCharacterController {

    @Resource
    private KeyboardCharacterService characterService;

    @PostMapping("/create")
    @Operation(summary = "创建键盘人设")
    @PreAuthorize("@ss.hasPermission('keyboard:character:create')")
    public CommonResult<Long> createCharacter(@Valid @RequestBody KeyboardCharacterSaveReqVO createReqVO) {
        return success(characterService.createCharacter(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新键盘人设")
    @PreAuthorize("@ss.hasPermission('keyboard:character:update')")
    public CommonResult<Boolean> updateCharacter(@Valid @RequestBody KeyboardCharacterSaveReqVO updateReqVO) {
        characterService.updateCharacter(updateReqVO);
        return success(true);
    }

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

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除键盘人设")
                @PreAuthorize("@ss.hasPermission('keyboard:character:delete')")
    public CommonResult<Boolean> deleteCharacterList(@RequestParam("ids") List<Long> ids) {
        characterService.deleteCharacterListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得键盘人设")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:character:query')")
    public CommonResult<KeyboardCharacterRespVO> getCharacter(@RequestParam("id") Long id) {
        KeyboardCharacterDO character = characterService.getCharacter(id);
        return success(BeanUtils.toBean(character, KeyboardCharacterRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得键盘人设分页")
    @PreAuthorize("@ss.hasPermission('keyboard:character:query')")
    public CommonResult<PageResult<KeyboardCharacterRespVO>> getCharacterPage(@Valid KeyboardCharacterPageReqVO pageReqVO) {
        PageResult<KeyboardCharacterDO> pageResult = characterService.getCharacterPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardCharacterRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出键盘人设 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:character:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCharacterExcel(@Valid KeyboardCharacterPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardCharacterDO> list = characterService.getCharacterPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "键盘人设.xls", "数据", KeyboardCharacterRespVO.class,
                        BeanUtils.toBean(list, KeyboardCharacterRespVO.class));
    }
    // ==================== 子表（键盘人设国际化内容） ====================

    @GetMapping("/character-i18n/page")
    @Operation(summary = "获得键盘人设国际化内容分页")
    @Parameter(name = "characterId", description = "角色主表id")
    @PreAuthorize("@ss.hasPermission('keyboard:character:query')")
    public CommonResult<PageResult<CharacterI18nDO>> getCharacterI18nPage(PageParam pageReqVO,
                                                                          @RequestParam("characterId") Long characterId) {
        return success(characterService.getCharacterI18nPage(pageReqVO, characterId));
    }

    @PostMapping("/character-i18n/create")
    @Operation(summary = "创建键盘人设国际化内容")
    @PreAuthorize("@ss.hasPermission('keyboard:character:create')")
    public CommonResult<Long> createCharacterI18n(@Valid @RequestBody CharacterI18nDO characterI18n) {
        return success(characterService.createCharacterI18n(characterI18n));
    }

    @PutMapping("/character-i18n/update")
    @Operation(summary = "更新键盘人设国际化内容")
    @PreAuthorize("@ss.hasPermission('keyboard:character:update')")
    public CommonResult<Boolean> updateCharacterI18n(@Valid @RequestBody CharacterI18nDO characterI18n) {
        characterService.updateCharacterI18n(characterI18n);
        return success(true);
    }

    @DeleteMapping("/character-i18n/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除键盘人设国际化内容")
    @PreAuthorize("@ss.hasPermission('keyboard:character:delete')")
    public CommonResult<Boolean> deleteCharacterI18n(@RequestParam("id") Long id) {
        characterService.deleteCharacterI18n(id);
        return success(true);
    }

    @DeleteMapping("/character-i18n/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除键盘人设国际化内容")
    @PreAuthorize("@ss.hasPermission('keyboard:character:delete')")
    public CommonResult<Boolean> deleteCharacterI18nList(@RequestParam("ids") List<Long> ids) {
        characterService.deleteCharacterI18nListByIds(ids);
        return success(true);
    }

    @GetMapping("/character-i18n/get")
    @Operation(summary = "获得键盘人设国际化内容")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:character:query')")
    public CommonResult<CharacterI18nDO> getCharacterI18n(@RequestParam("id") Long id) {
        return success(characterService.getCharacterI18n(id));
    }

}