package com.red.circle.other.app.inner.endpoint.tmp;

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.framework.core.dto.ReqSysOrigin;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.other.app.dto.clientobject.room.RoomMikeNumCO;
import com.red.circle.other.app.dto.clientobject.room.RoomMikeTypeCO;
import com.red.circle.other.app.dto.cmd.room.RoomMikeNumCmd;
import com.red.circle.other.app.dto.cmd.room.RoomMikeTypeCmd;
import com.red.circle.other.app.service.room.RoomMikeService;
import com.red.circle.other.inner.endpoint.tmp.api.RoomMikeClientRestApi;
import com.red.circle.other.inner.tmp.CommonOtherRestApiCmd;
import com.red.circle.other.inner.tmp.RoomMikeNumResApiCmd;
import com.red.circle.other.inner.tmp.RoomMikeNumTmpCO;
import com.red.circle.other.inner.tmp.RoomMikeTypeRestApiCmd;
import com.red.circle.other.inner.tmp.RoomMikeTypeTmpCO;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.math.BigDecimal;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author pengliang on 2024/1/8
 */
@Validated
@RestController
@RequestMapping(value = RoomMikeClientRestApi.API_PREFIX, produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class RoomMikeClientRestEndpoint implements RoomMikeClientRestApi {

  private final RoomMikeService roomMikeService;

  @Override
  public ResultResponse<BigDecimal> purchasingRoomMikeNum(RoomMikeNumResApiCmd cmd) {
    RoomMikeNumCmd newCmd = new RoomMikeNumCmd();
    newCmd.setReqSysOrigin(ReqSysOrigin.of(cmd.getSysOrigin()));
    newCmd.setReqUserId(cmd.getReqUserId());
    newCmd.setMikeNum(cmd.getMikeNum());
    newCmd.setMikeNumType(cmd.getMikeNumType());
    return ResultResponse.success(roomMikeService.purchasingRoomMikeNum(newCmd));
  }

  @Override
  public ResultResponse<List<RoomMikeNumTmpCO>> listRoomMikeNum(CommonOtherRestApiCmd cmd) {
    AppExtCommand newCmd = new AppExtCommand();
    newCmd.setReqSysOrigin(ReqSysOrigin.of(cmd.getSysOrigin()));
    newCmd.setReqUserId(cmd.getReqUserId());

    List<RoomMikeNumCO> mikeNums = roomMikeService.listRoomMikeNum(newCmd);

    if (CollectionUtils.isEmpty(mikeNums)) {
      return ResultResponse.success();
    }

    return ResultResponse.success(
        mikeNums.stream().map(item -> new RoomMikeNumTmpCO()
            .setId(item.getId())
            .setUserId(item.getUserId())
            .setMikeNum(item.getMikeNum())
            .setMikeNumType(item.getMikeNumType())
            .setSysOrigin(item.getSysOrigin())).toList()
    );
  }

  @Override
  public ResultResponse<BigDecimal> purchasingRoomMikeType(RoomMikeTypeRestApiCmd cmd) {
    RoomMikeTypeCmd newCmd = new RoomMikeTypeCmd();
    newCmd.setReqSysOrigin(ReqSysOrigin.of(cmd.getSysOrigin()));
    newCmd.setReqUserId(cmd.getReqUserId());
    newCmd.setMikeType(cmd.getMikeType());
    newCmd.setDuration(cmd.getDuration());
    return ResultResponse.success(
        roomMikeService.purchasingRoomMikeType(newCmd)
    );
  }

  @Override
  public ResultResponse<List<RoomMikeTypeTmpCO>> listRoomMikeType(CommonOtherRestApiCmd cmd) {
    AppExtCommand newCmd = new AppExtCommand();
    newCmd.setReqSysOrigin(ReqSysOrigin.of(cmd.getSysOrigin()));
    newCmd.setReqUserId(cmd.getReqUserId());
    List<RoomMikeTypeCO> roomMikeTypes = roomMikeService.listRoomMikeType(newCmd);

    if (CollectionUtils.isEmpty(roomMikeTypes)) {
      return ResultResponse.success();
    }

    return ResultResponse.success(
        roomMikeTypes.stream().map(item -> new RoomMikeTypeTmpCO()
            .setId(item.getId())
            .setUserId(item.getUserId())
            .setMikeType(item.getMikeType())
            .setSysOrigin(item.getSysOrigin())
            .setExpireType(item.getExpireType())
            .setExpireTime(item.getExpireTime())
        ).toList()
    );
  }
}
