package com.xunhong.erp.turbo.admin.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.xunhong.erp.turbo.api.infra.api.CategoryServiceI;
import com.xunhong.erp.turbo.api.infra.dto.cmd.CategoryCreateCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.CategoryDestroyCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.CategoryUpdateCmd;
import com.xunhong.erp.turbo.api.infra.dto.enums.CategoryTypeEnum;
import com.xunhong.erp.turbo.api.infra.dto.qry.CategoryShowQry;
import com.xunhong.erp.turbo.api.infra.dto.qry.CategoryTreeQry;
import com.xunhong.erp.turbo.api.infra.dto.vo.CategoryVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "MaterialCategory", description = "素材分类")
@RestController("operationMaterialCategoryController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class MaterialCategoryController {
	@DubboReference(version = "1.0.0")
	private final CategoryServiceI categoryService;

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.OPERATION_MATERIAL_CATEGORY_VIEW}, mode = SaMode.OR)
	@GetMapping("/treeMaterialCategory")
	@Operation(summary = "素材分类树", method = "GET")
	public MultiResponse<Tree<Long>> treeMaterialCategory(@ModelAttribute @Validated CategoryTreeQry categoryTreeQry) {
		List<CategoryTypeEnum> typeList = new ArrayList<>();
		typeList.add(CategoryTypeEnum.FILE_IMAGE);
		typeList.add(CategoryTypeEnum.FILE_VIDEO);
		categoryTreeQry.setTypeList(typeList);

		List<CategoryVO> categoryVOList = categoryService.list(categoryTreeQry);
		TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
		treeNodeConfig.setWeightKey("sort");
		treeNodeConfig.setIdKey("categoryId");
		treeNodeConfig.setParentIdKey("pid");
		treeNodeConfig.setNameKey("name");
		treeNodeConfig.setChildrenKey("children");
		treeNodeConfig.setDeep(5);

		List<Tree<Long>> trees = TreeUtil.build(categoryVOList, null, treeNodeConfig, (category, tree) -> {
			tree.setId(category.getCategoryId());
			tree.setParentId(category.getPid());
			tree.setWeight(category.getSort());
			tree.setName(category.getName());
			tree.putExtra("type", category.getType());
		});

		return MultiResponse.of(trees);
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.OPERATION_MATERIAL_CATEGORY_CREATE}, mode = SaMode.OR)
	@PostMapping("createMaterialCategory")
	@Operation(summary = "创建素材分类", method = "POST")
	public SingleResponse<CategoryVO> createMaterialCategory(@RequestBody @Validated CategoryCreateCmd categoryCreateCmd) {
		return SingleResponse.of(categoryService.create(categoryCreateCmd));
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.OPERATION_MATERIAL_CATEGORY_UPDATE}, mode = SaMode.OR)
	@RequestMapping(value = "updateMaterialCategory", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "更新素材分类", method = "PATCH")
	public SingleResponse<CategoryVO> updateMaterialCategory(@RequestBody @Validated CategoryUpdateCmd categoryUpdateCmd) {
		return SingleResponse.of(categoryService.update(categoryUpdateCmd));
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.OPERATION_MATERIAL_CATEGORY_VIEW}, mode = SaMode.OR)
	@GetMapping("/showMaterialCategory")
	@Operation(summary = "素材分类", method = "GET")
	public SingleResponse<CategoryVO> showMaterialCategory(@ModelAttribute @Validated CategoryShowQry categoryShowQry) {
		return SingleResponse.of(categoryService.show(categoryShowQry));
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.OPERATION_MATERIAL_CATEGORY_DELETE}, mode = SaMode.OR)
	@DeleteMapping("/destroyMaterialCategory")
	@Operation(summary = "删除素材分类", method = "DELETE")
	public Response destroyMaterialCategory(@RequestBody @Validated CategoryDestroyCmd categoryDestroyCmd) {
		categoryService.destroy(categoryDestroyCmd);
		return Response.buildSuccess();
	}

}
