package com.red.circle.console.adapter.admin.dashboard;


import com.red.circle.console.infra.database.mongo.admin.entity.BackDashboard;
import com.red.circle.console.infra.database.mongo.admin.entity.BackDashboardNavigation;
import com.red.circle.console.infra.database.mongo.admin.service.BackDashboardService;
import com.red.circle.framework.core.dto.CommonCommand;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 控制面板.
 *
 * @author pengliang on 2022/7/12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/dashboard", produces = MediaType.APPLICATION_JSON_VALUE)
public class DashboardRestController extends BaseController {

  private final BackDashboardService backDashboardService;

  /**
   * 获取用户dashboard 信息.
   */
  @GetMapping
  public BackDashboard getDashboard(CommonCommand cmd) {
    return backDashboardService.getById(cmd.requiredReqUserId());
  }

  /**
   * 添加预览 dashboard 信息.
   */
  @PostMapping("/previews")
  public void addRecentPreviews(@RequestBody BackDashboardNavigation navigation,
      CommonCommand cmd) {
    navigation.setCreateTime(TimestampUtils.now());
    backDashboardService.addRecentPreviews(cmd.requiredReqUserId(), navigation);
  }

  /**
   * 删除预览 dashboard 信息.
   */
  @DeleteMapping("/previews")
  public void delRecentPreviews(@RequestParam String id, CommonCommand cmd) {
    backDashboardService.removeRecentPreviews(cmd.requiredReqUserId(), id);
  }

  /**
   * 添加自定义 dashboard 信息.
   */
  @PostMapping("/custom")
  public void addCustomNavigations(@RequestBody BackDashboardNavigation navigation,
      CommonCommand cmd) {
    navigation.setId(IdWorkerUtils.getIdStr());
    navigation.setCreateTime(TimestampUtils.now());
    backDashboardService.addCustomNavigations(cmd.requiredReqUserId(), navigation);
  }

  /**
   * 删除自定义 dashboard 信息.
   */
  @DeleteMapping("/custom")
  public void delCustomNavigations(@RequestParam String id, CommonCommand cmd) {
    backDashboardService.removeCustomNavigations(cmd.requiredReqUserId(), id);
  }

  /**
   * 修改主题.
   */
  @PostMapping("/theme")
  public void updateTheme(@RequestParam String theme, CommonCommand cmd) {
    backDashboardService.updateTheme(cmd.requiredReqUserId(), theme);
  }

}
