package org.springblade.modules.martial.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.modules.martial.pojo.entity.MartialCompetitionAttachment;
import org.springblade.modules.martial.pojo.entity.MartialCompetitionRulesChapter;
import org.springblade.modules.martial.pojo.entity.MartialCompetitionRulesContent;
import org.springblade.modules.martial.pojo.vo.MartialCompetitionRulesVO;
import org.springblade.modules.martial.service.IMartialCompetitionRulesService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 赛事规程 控制器
 *
 * @author BladeX
 */
@RestController
@AllArgsConstructor
@RequestMapping("/martial/competition/rules")
@Tag(name = "赛事规程管理", description = "赛事规程管理接口")
public class MartialCompetitionRulesController extends BladeController {

	private final IMartialCompetitionRulesService rulesService;

	/**
	 * 获取赛事规程（小程序端）
	 */
	@GetMapping("")
	@Operation(summary = "获取赛事规程", description = "小程序端获取规程信息")
	public R<MartialCompetitionRulesVO> getRules(@RequestParam Long competitionId) {
		MartialCompetitionRulesVO rules = rulesService.getRulesByCompetitionId(competitionId);
		return R.data(rules);
	}

	// ==================== 附件管理 ====================

	/**
	 * 获取附件列表
	 */
	@GetMapping("/attachment/list")
	@Operation(summary = "获取附件列表", description = "管理端获取附件列表")
	public R<List<MartialCompetitionAttachment>> getAttachmentList(@RequestParam Long competitionId) {
		List<MartialCompetitionAttachment> list = rulesService.getAttachmentList(competitionId);
		return R.data(list);
	}

	/**
	 * 保存附件
	 */
	@PostMapping("/attachment/save")
	@Operation(summary = "保存附件", description = "新增或修改附件")
	public R saveAttachment(@RequestBody MartialCompetitionAttachment attachment) {
		return R.status(rulesService.saveAttachment(attachment));
	}

	/**
	 * 删除附件
	 */
	@PostMapping("/attachment/remove")
	@Operation(summary = "删除附件", description = "传入附件ID")
	public R removeAttachment(@RequestParam Long id) {
		return R.status(rulesService.removeAttachment(id));
	}

	// ==================== 章节管理 ====================

	/**
	 * 获取章节列表
	 */
	@GetMapping("/chapter/list")
	@Operation(summary = "获取章节列表", description = "管理端获取章节列表")
	public R<List<MartialCompetitionRulesChapter>> getChapterList(@RequestParam Long competitionId) {
		List<MartialCompetitionRulesChapter> list = rulesService.getChapterList(competitionId);
		return R.data(list);
	}

	/**
	 * 保存章节
	 */
	@PostMapping("/chapter/save")
	@Operation(summary = "保存章节", description = "新增或修改章节")
	public R saveChapter(@RequestBody MartialCompetitionRulesChapter chapter) {
		return R.status(rulesService.saveChapter(chapter));
	}

	/**
	 * 删除章节
	 */
	@PostMapping("/chapter/remove")
	@Operation(summary = "删除章节", description = "传入章节ID")
	public R removeChapter(@RequestParam Long id) {
		return R.status(rulesService.removeChapter(id));
	}

	// ==================== 章节内容管理 ====================

	/**
	 * 获取章节内容列表
	 */
	@GetMapping("/content/list")
	@Operation(summary = "获取章节内容列表", description = "管理端获取章节内容")
	public R<List<MartialCompetitionRulesContent>> getContentList(@RequestParam Long chapterId) {
		List<MartialCompetitionRulesContent> list = rulesService.getContentList(chapterId);
		return R.data(list);
	}

	/**
	 * 保存章节内容
	 */
	@PostMapping("/content/save")
	@Operation(summary = "保存章节内容", description = "新增或修改章节内容")
	public R saveContent(@RequestBody MartialCompetitionRulesContent content) {
		return R.status(rulesService.saveContent(content));
	}

	/**
	 * 批量保存章节内容
	 */
	@PostMapping("/content/batch-save")
	@Operation(summary = "批量保存章节内容", description = "批量保存章节内容")
	public R batchSaveContents(@RequestBody Map<String, Object> params) {
		Long chapterId = Long.valueOf(params.get("chapterId").toString());
		@SuppressWarnings("unchecked")
		List<String> contents = (List<String>) params.get("contents");
		return R.status(rulesService.batchSaveContents(chapterId, contents));
	}

	/**
	 * 删除章节内容
	 */
	@PostMapping("/content/remove")
	@Operation(summary = "删除章节内容", description = "传入内容ID")
	public R removeContent(@RequestParam Long id) {
		return R.status(rulesService.removeContent(id));
	}

}
