package com.red.circle.console.app.service.app.sys;

import com.red.circle.console.app.convertor.sys.EmojiConfigAppConvertor;
import com.red.circle.console.app.dto.clienobject.sys.SysEmojiConfigCO;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.inner.endpoint.material.props.EmojiConfigClient;
import com.red.circle.other.inner.model.cmd.sys.SysEmojiConfigParamCmd;
import com.red.circle.other.inner.model.cmd.sys.SysEmojiConfigQryCmd;
import com.red.circle.other.inner.model.cmd.sys.SysEmojiGroupParamCmd;
import com.red.circle.other.inner.model.cmd.sys.SysEmojiGroupQryCmd;
import com.red.circle.other.inner.model.dto.material.EmojiConfigDTO;
import com.red.circle.other.inner.model.dto.material.EmojiGroupDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 表情管理
 *
 * @author zongpubin on 2023/10/26
 */
@Service
@RequiredArgsConstructor
public class SysEmojiConfigServiceImpl implements SysEmojiConfigService {

  private final EmojiConfigClient emojiConfigClient;
  private final EmojiConfigAppConvertor emojiConfigAppConvertor;

  @Override
  public PageResult<EmojiGroupDTO> pageEmojiGroup(SysEmojiGroupQryCmd query) {
    return ResponseAssert.requiredSuccess(emojiConfigClient.pageEmojiGroup(query));
  }

  @Override
  public void groupAddOrUpdate(SysEmojiGroupParamCmd param) {
    ResponseAssert.requiredSuccess(emojiConfigClient.groupAddOrUpdate(param));
  }

  @Override
  public List<EmojiGroupDTO> groupListBySysOrigin(String sysOrigin) {
    return ResponseAssert.requiredSuccess(emojiConfigClient.groupListBySysOrigin(sysOrigin));
  }

  @Override
  public void switchGroupShelfStatus(Long id, Boolean status) {
    ResponseAssert.requiredSuccess(emojiConfigClient.switchGroupShelfStatus(id, status));
  }

  @Override
  public PageResult<SysEmojiConfigCO> pageEmojiConfig(SysEmojiConfigQryCmd query) {

    PageResult<EmojiConfigDTO> pageResult = ResponseAssert.requiredSuccess(
        emojiConfigClient.pageEmojiConfig(query));

    if (CollectionUtils.isEmpty(pageResult.getRecords())) {
      return pageResult.convert(
          emojiConfigAppConvertor::toSysEmojiConfigCO);
    }

    Map<Long, EmojiGroupDTO> groupMap = ResponseAssert.requiredSuccess(
        emojiConfigClient.mapEmojiGroupByIds(
            pageResult.getRecords().stream().map(EmojiConfigDTO::getGroupId).collect(
                Collectors.toSet())));
    return pageResult.convert(emojiConfigDTO -> {
      SysEmojiConfigCO sysEmojiConfigCO = emojiConfigAppConvertor.toSysEmojiConfigCO(
          emojiConfigDTO);
      sysEmojiConfigCO.setGroup(groupMap.get(emojiConfigDTO.getGroupId()));
      return sysEmojiConfigCO;
    });
  }

  @Override
  public void addOrUpdate(SysEmojiConfigParamCmd param) {
    ResponseAssert.requiredSuccess(emojiConfigClient.emojiConfigAddOrUpdate(param));
  }

  @Override
  public void offShelf(Long id) {
    ResponseAssert.requiredSuccess(emojiConfigClient.offShelf(id));
  }
}
