package com.red.circle.other.app.service.task;


import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.app.command.task.CheckInExe;
import com.red.circle.other.app.command.task.query.CheckInLotteryQryExe;
import com.red.circle.other.app.command.task.query.CheckInRewardQryExe;
import com.red.circle.other.app.common.props.SingleGroupRewardPoolCommon;
import com.red.circle.other.app.dto.clientobject.activity.ActivityResourceCO;
import com.red.circle.other.app.dto.clientobject.task.CheckInRewardListCO;
import com.red.circle.other.app.dto.cmd.task.CheckInRewardCmd;
import com.red.circle.other.infra.database.cache.service.other.CheckInCacheService;
import com.red.circle.other.inner.enums.activity.PropsActivityTypeEnum;
import com.red.circle.other.inner.model.dto.activity.props.ActivityRewardPropsDTO;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 打卡服务实现.
 *
 * @author pengliang on 2020/11/27
 */
@Service
@RequiredArgsConstructor
public class CheckInServiceImpl implements CheckInService {

  private final CheckInExe checkInExe;
  private final CheckInRewardQryExe checkInRewardQryExe;
  private final CheckInCacheService checkInCacheService;
  private final CheckInLotteryQryExe checkInLotteryQryExe;
  private final SingleGroupRewardPoolCommon singleGroupRewardPoolCommon;


  @Override
  public List<ActivityRewardPropsDTO> listRewardPool(AppExtCommand cmd) {
    return singleGroupRewardPoolCommon
        .listRewardProps(cmd.getReqSysOrigin().getOrigin(), PropsActivityTypeEnum.DAILY_REGISTER);
  }

  @Override
  public Boolean checkToday(AppExtCommand cmd) {
    return checkInCacheService.checkDailyCheckIn(cmd.getReqUserId());
  }

  @Override
  public ActivityRewardPropsDTO lottery(AppExtCommand cmd) {
    return checkInLotteryQryExe.execute(cmd);
  }

  @Override
  public CheckInRewardListCO listReward(AppExtCommand cmd) {
    CheckInRewardListCO checkInRewardListCO = checkInRewardQryExe.execute(cmd);
    List<ActivityResourceCO> rewards  = checkInRewardListCO.getRewards();
    rewards.forEach(p->{
      if (p.getPropsGroup() != null){
        List<ActivityRewardPropsDTO> activityRewardProps = p.getPropsGroup().getActivityRewardProps();
        activityRewardProps.forEach(q->{
          if ("GOLD".equals(q.getType())){
            q.setCover("https://dev.file.momooline.com/yolo_gold_icon.png");
            q.setQuantity(q.getAmount().intValue());
          }
        });
        p.getPropsGroup().setActivityRewardProps(activityRewardProps);
      }
    });
    return checkInRewardListCO;
  }

  @Override
  public Integer checkIn2Fun(CheckInRewardCmd cmd) {
    return checkInExe.execute(cmd);
  }


}
