package com.red.circle.other.app.inner.service.material.props.impl;

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.inner.convertor.material.EmojiInnerConvertor;
import com.red.circle.other.app.inner.service.material.props.EmojiConfigClientService;
import com.red.circle.other.infra.database.cache.service.other.EmojiCacheService;
import com.red.circle.other.infra.database.rds.entity.emoji.EmojiConfig;
import com.red.circle.other.infra.database.rds.service.emoji.EmojiConfigService;
import com.red.circle.other.infra.database.rds.service.emoji.EmojiGroupService;
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 java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 表情管理 服务类.
 *
 * @author pengliang on 2023/6/10
 */
@Service
@RequiredArgsConstructor
public class EmojiConfigClientServiceImpl implements EmojiConfigClientService {

  private final EmojiCacheService emojiCacheService;
  private final EmojiGroupService emojiGroupService;
  private final EmojiConfigService emojiConfigService;
  private final EmojiInnerConvertor emojiInnerConvertor;

  @Override
  public List<EmojiConfigDTO> listEmojiByGroupId(Long groupId) {
    return emojiInnerConvertor.toListEmojiConfig(
        emojiConfigService.listEmojiByGroupId(groupId)
    );
  }

  @Override
  public Map<Long, List<EmojiConfigDTO>> mapByGroupIds(Set<Long> groupIds) {
    return emojiInnerConvertor.toMapEmojiConfig(
        emojiConfigService.mapByGroupIds(groupIds)
    );
  }

  @Override
  public List<EmojiConfigDTO> listByIds(Set<Long> ids) {
    return emojiInnerConvertor.toListEmojiConfig(
        emojiConfigService.listByIds(ids)
    );
  }

  @Override
  public PageResult<EmojiGroupDTO> pageEmojiGroup(SysEmojiGroupQryCmd query) {
    return emojiGroupService.pageEmojiGroup(query)
        .convert(emojiInnerConvertor::toEmojiGroupDTO);
  }

  @Override
  public void groupAddOrUpdate(SysEmojiGroupParamCmd param) {
    emojiCacheService.removeConfig();

    if (Objects.isNull(param.getId())) {
      emojiGroupService.save(emojiInnerConvertor.toEmojiGroup(param));
      return;
    }
    emojiGroupService.updateSelectiveById(emojiInnerConvertor.toEmojiGroup(param));
  }

  @Override
  public List<EmojiGroupDTO> groupListBySysOrigin(String sysOrigin) {
    return emojiInnerConvertor.toListEmojiGroupDTO(
        emojiGroupService.groupListBySysOrigin(sysOrigin));
  }

  @Override
  public void switchGroupShelfStatus(Long id, Boolean status) {
    emojiCacheService.removeConfig();
    emojiGroupService.switchGroupShelfStatus(id, status);
  }

  @Override
  public PageResult<EmojiConfigDTO> pageEmojiConfig(SysEmojiConfigQryCmd query) {
    return emojiConfigService.pageEmojiConfig(query)
        .convert(emojiInnerConvertor::toEmojiConfigDTO);
  }

  @Override
  public void emojiConfigAddOrUpdate(SysEmojiConfigParamCmd param) {
    emojiCacheService.removeConfig();
    if (Objects.nonNull(param.getId())) {
      emojiConfigService.updateSelectiveById(emojiInnerConvertor.toEmojiConfig(param));
      return;
    }

    emojiConfigService.save(emojiInnerConvertor.toEmojiConfig(param));
  }

  @Override
  public void offShelf(Long id) {

    EmojiConfig data = emojiConfigService.getById(id);

    ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_RECORD_INFO, data);

    data.setShelfStatus(!data.getShelfStatus());

    emojiConfigService.updateSelectiveById(data);

    emojiCacheService.removeConfig();

  }

  @Override
  public Map<Long, EmojiGroupDTO> mapEmojiGroupByIds(Set<Long> ids) {
    return emojiInnerConvertor.toMapEmojiGroupDTO(emojiGroupService.mapByIds(ids));
  }

}
