package com.red.circle.other.app.command.room;

import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.dto.cmd.room.SwitchUseThemeCmd;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.mongo.entity.live.RoomProfile;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.infra.database.rds.entity.props.PropsCommodityStore;
import com.red.circle.other.infra.database.rds.entity.props.PropsSourceRecord;
import com.red.circle.other.infra.database.rds.entity.props.RoomThemeUserBackpack;
import com.red.circle.other.infra.database.rds.entity.props.RoomThemeUserCustomize;
import com.red.circle.other.infra.database.rds.service.props.PropsCommodityStoreService;
import com.red.circle.other.infra.database.rds.service.props.PropsSourceRecordService;
import com.red.circle.other.infra.database.rds.service.props.RoomThemeUserBackpackService;
import com.red.circle.other.infra.database.rds.service.props.RoomThemeUserCustomizeService;
import com.red.circle.other.inner.asserts.RoomErrorCode;
import com.red.circle.other.inner.enums.material.PropsCommodityType;
import com.red.circle.other.inner.enums.material.PropsCurrencyType;
import com.red.circle.tool.core.text.StringUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 房间主题切换使用
 *
 * @author pengliang on 2021/1/23
 */
@Component
@RequiredArgsConstructor
public class RoomThemeSwitchUseCmdExe {

  private final UserProfileGateway userProfileGateway;
  private final PropsSourceRecordService propsSourceRecordService;
  private final RoomProfileManagerService roomProfileManagerService;
  private final PropsCommodityStoreService propsCommodityStoreService;
  private final RoomThemeUserBackpackService roomThemeUserBackpackService;
  private final RoomThemeUserCustomizeService roomThemeUserCustomizeService;

  public void execute(SwitchUseThemeCmd cmd) {
      Long reqUserId = cmd.getReqUserId();
      RoomProfile roomProfile = roomProfileManagerService.getProfileByUserId(reqUserId);
    ResponseAssert.notNull(RoomErrorCode.ROOM_NOT_EXISTS, roomProfile);
    ResponseAssert.isTrue(CommonErrorCode.INSUFFICIENT_PERMISSION,
        cmd.reqIsSelf(roomProfile.getUserId()));

    userProfileGateway.removeCacheAll(reqUserId);
    if (Objects.equals(cmd.getUnload(), Boolean.TRUE)) {
      ResponseAssert.isTrue(CommonErrorCode.UPDATE_FAILURE,
          roomThemeUserBackpackService.cleanUseTheme(reqUserId));
      return;
    }

    RoomThemeUserBackpack backpack = roomThemeUserBackpackService.getNotExpireTimeByUserAndThemeId(
            reqUserId, cmd.getThemeId());

    // 免费
    if (Objects.isNull(backpack)) {
      PropsCommodityStore store = getThemeStoreBySourceId(cmd);
      ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_MAPPING_INFO, store);

      if (StringUtils.isBlank(store.getCurrencyTypes())
          || !store.getCurrencyTypes().contains(PropsCurrencyType.FREE.name())) {
        return;
      }

      PropsSourceRecord sourceRecord = propsSourceRecordService.getPropsById(store.getSourceId());
      ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_MAPPING_INFO, sourceRecord);

      ResponseAssert.isTrue(CommonErrorCode.UPDATE_FAILURE,
          roomThemeUserBackpackService.switchUseTheme(reqUserId,
              cmd.getThemeId(),
              Boolean.TRUE));
      return;
    }

    // 主题商城
    if (Objects.equals(backpack.getThemeType(), 0)) {
      PropsSourceRecord sourceRecord = propsSourceRecordService.getPropsById(backpack.getThemeId());
      ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_MAPPING_INFO, sourceRecord);

      ResponseAssert.isTrue(CommonErrorCode.UPDATE_FAILURE,
          roomThemeUserBackpackService.switchUseTheme
              (reqUserId, cmd.getThemeId(), Boolean.FALSE));
      return;
    }

    // 自定义
    if (Objects.equals(backpack.getThemeType(), 1)) {

      RoomThemeUserCustomize customize = getUserCustomizeThemeById(cmd);
      ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_MAPPING_INFO, customize);

      ResponseAssert.isTrue(CommonErrorCode.UPDATE_FAILURE,
          roomThemeUserBackpackService.switchUseTheme
              (reqUserId, cmd.getThemeId(), Boolean.FALSE));
    }
  }

  private RoomThemeUserCustomize getUserCustomizeThemeById(SwitchUseThemeCmd cmd) {
    return roomThemeUserCustomizeService.getById(cmd.getThemeId());
  }

  private PropsCommodityStore getThemeStoreBySourceId(SwitchUseThemeCmd cmd) {
    return propsCommodityStoreService.getBySourceId(SysOriginPlatformEnum.LIKEI, cmd.getThemeId(),
        PropsCommodityType.THEME.name());
  }

}
