package com.red.circle.other.app.inner.service.user.user.impl;

import com.red.circle.external.inner.endpoint.message.OfficialNoticeClient;
import com.red.circle.external.inner.model.cmd.message.notice.official.NoticeExtTemplateCustomizeCmd;
import com.red.circle.external.inner.model.enums.message.OfficialNoticeTypeEnum;
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.infra.database.mongo.entity.user.profile.UserSpecialId;
import com.red.circle.other.infra.database.mongo.service.user.profile.UserSpecialIdService;
import com.red.circle.other.infra.database.rds.entity.user.user.UserBeautifulNumberApply;
import com.red.circle.other.infra.database.rds.service.user.user.UserBeautifulNumberApplyService;
import com.red.circle.other.infra.database.rds.service.user.user.UserBeautifulNumberService;
import com.red.circle.other.app.inner.convertor.user.UserProfileInnerConvertor;
import com.red.circle.other.inner.model.cmd.user.BeautifulNumberQryCmd;
import com.red.circle.other.inner.model.cmd.user.UserBeautifulNumberApplyQryCmd;
import com.red.circle.other.inner.model.cmd.user.UserBeautifulNumberCmd;
import com.red.circle.other.inner.model.dto.user.BeautifulNumberDTO;
import com.red.circle.other.inner.model.dto.user.UserBeautifulNumberApplyDTO;
import com.red.circle.other.app.inner.service.user.user.UserBeautifulNumberClientService;
import com.red.circle.tool.core.date.TimestampUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 用户申请靓号记录 服务类.
 * </p>
 *
 * @author pengshigang
 * @since 2021-08-18
 */
@Service
@RequiredArgsConstructor
public class UserBeautifulNumberClientServiceImpl implements
    UserBeautifulNumberClientService {

  private final UserProfileInnerConvertor userProfileInnerConvertor;
  private final UserBeautifulNumberService userBeautifulNumberService;
  private final UserBeautifulNumberApplyService userBeautifulNumberApplyService;
  private final UserSpecialIdService userSpecialIdService;
  private final OfficialNoticeClient officialNoticeClient;

  @Override
  public PageResult<UserBeautifulNumberApplyDTO> pageBeautifulNumberApply(
      UserBeautifulNumberApplyQryCmd query) {
    return userBeautifulNumberApplyService.page(query)
        .convert(userProfileInnerConvertor::toUserBeautifulNumberApplyDTO);
  }

  @Override
  public void handleApply(Long id, Integer state, String remark) {
    ResponseAssert.isTrue(CommonErrorCode.TYPE_IS_NOT_IN_SCOPE, checkState(state));
    userBeautifulNumberApplyService.handleApply(id, state);

    UserBeautifulNumberApply beautifulNumberApply = userBeautifulNumberApplyService.getById(id);
    String content;
    if (state == 1) {
      UserSpecialId userSpecialId = userSpecialIdService.getByUserId(beautifulNumberApply.getApplyUser());
      if (Objects.nonNull(userSpecialId)) {
          userSpecialIdService.remove(userSpecialId.getId());
      }

      // 创建新靓号
      UserSpecialId specialIdCmd = new UserSpecialId();
      specialIdCmd.setSysOrigin(beautifulNumberApply.getSysOrigin());
      specialIdCmd.setAccount(beautifulNumberApply.getApplyAccount());
      specialIdCmd.setUserId(beautifulNumberApply.getApplyUser());
      // 设置365 * 3天后过期
      specialIdCmd.setExpiredTime(TimestampUtils.dateTimePlusDays(TimestampUtils.now(),365 * 3));
      userSpecialIdService.add(specialIdCmd);


      content = String.format("Your application for exclusive lD 【%s】has been approved and has been applied to both you user lD and room lD.",
              beautifulNumberApply.getApplyAccount());
    } else {
      content = String.format("Your application for exclusive lD 【%s】wasn't approved for reason 【%s】",
              beautifulNumberApply.getApplyAccount(),
              remark);
    }

    officialNoticeClient.send(
            NoticeExtTemplateCustomizeCmd.builder()
                    .toAccount(beautifulNumberApply.getApplyUser())
                    .noticeType(OfficialNoticeTypeEnum.USER_SPECIAL_AUDIT_RESULTS)
                    .content(content)
                    .build()
    );

  }

  private boolean checkState(Integer state) {
    return Objects.equals(state, 1) || Objects.equals(state, 2);
  }


  @Override
  public void updateBeautifulNumber(UserBeautifulNumberCmd cmd) {
    userBeautifulNumberService.updateBeautifulNumber(cmd);
  }

  @Override
  public void addBeautifulNumber(UserBeautifulNumberCmd cmd) {
    userBeautifulNumberService.addBeautifulNumber(cmd);
  }


  @Override
  public PageResult<BeautifulNumberDTO> pageBeautifulNumber(BeautifulNumberQryCmd query) {
    return userBeautifulNumberService.getBeautifulNumber(query)
        .convert(userProfileInnerConvertor::toBeautifulNumberDTO);
  }

  @Override
  public void deleteBeautifulNumber(Long id) {
    userBeautifulNumberService.removeById(id);
  }
}
