package com.red.circle.game.fruit.application.endpoint.impl;

import com.google.common.collect.Lists;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.game.fruit.application.convertor.GameFruitTaskAppConvertor;
import com.red.circle.game.fruit.application.endpoint.GameFruitTaskClientService;
import com.red.circle.game.fruit.infrastructure.database.entity.GameFruitPrize;
import com.red.circle.game.fruit.infrastructure.database.entity.GameFruitRule;
import com.red.circle.game.fruit.infrastructure.database.entity.GameFruitTaskAwardConfig;
import com.red.circle.game.fruit.infrastructure.database.repository.GameFruitBountyConfigService;
import com.red.circle.game.fruit.infrastructure.database.repository.GameFruitPrizeService;
import com.red.circle.game.fruit.infrastructure.database.repository.GameFruitRuleService;
import com.red.circle.game.fruit.infrastructure.database.repository.GameFruitTaskAwardConfigService;
import com.red.circle.game.fruit.inner.model.cmd.GameFruitBountyConfigQryCmd;
import com.red.circle.game.fruit.inner.model.cmd.GameFruitTaskConfigQryCmd;
import com.red.circle.game.fruit.inner.model.dto.GameFruitAwardPictureDTO;
import com.red.circle.game.fruit.inner.model.dto.GameFruitBountyConfigDTO;
import com.red.circle.game.fruit.inner.model.dto.GameFruitTaskConfigDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 摩天轮游戏任务相关
 *
 * @author zongpubin on 2023/12/13
 */
@Service
@RequiredArgsConstructor
public class GameFruitTaskClientServiceImpl implements GameFruitTaskClientService {

  private final GameFruitRuleService gameFruitRuleService;
  private final GameFruitPrizeService gameFruitPrizeService;
  private final GameFruitTaskAppConvertor gameFruitTaskAppConvertor;
  private final GameFruitBountyConfigService gameFruitBountyConfigService;
  private final GameFruitTaskAwardConfigService gameFruitTaskAwardConfigService;

  @Override
  public PageResult<GameFruitBountyConfigDTO> getGameFruitBountyConfig(
      GameFruitBountyConfigQryCmd query) {
    return gameFruitBountyConfigService.getGameFruitBountyConfig(
        query).convert(gameFruitTaskAppConvertor::toGameFruitBountyConfigDTO);
  }

  @Override
  public void addGameFruitBountyConfig(GameFruitBountyConfigDTO param) {
    if (Objects.nonNull(param.getId())) {
      gameFruitBountyConfigService.updateSelectiveById(
          gameFruitTaskAppConvertor.toGameFruitBountyConfig(param));
      return;
    }
    gameFruitBountyConfigService.save(gameFruitTaskAppConvertor.toGameFruitBountyConfig(param));
  }

  @Override
  public void deleteGameFruitBountyConfig(Long id) {
    gameFruitBountyConfigService.removeById(id);
  }

  @Override
  public PageResult<GameFruitTaskConfigDTO> getGameFruitTaskConfig(
      GameFruitTaskConfigQryCmd query) {
    PageResult<GameFruitTaskAwardConfig> pageResult = gameFruitTaskAwardConfigService.getGameFruitTaskConfig(
        query);

    if (CollectionUtils.isEmpty(pageResult.getRecords())) {
      return pageResult.convert(gameFruitTaskAppConvertor::toGameFruitTaskConfigDTO);
    }

    return pageResult.convert(awardConfig -> {
      GameFruitTaskConfigDTO taskConfigDTO = gameFruitTaskAppConvertor.toGameFruitTaskConfigDTO(
          awardConfig);
      if (Objects.nonNull(awardConfig.getGameAwardId())) {
        taskConfigDTO.setGameAwardId(StringUtils.splitAsList(awardConfig.getGameAwardId(), ","));
      }
      return taskConfigDTO;

    });
  }

  @Override
  public void addGameFruitTaskConfig(GameFruitTaskConfigDTO param) {
    List<String> awardPhoto = Lists.newArrayList();
    GameFruitTaskAwardConfig config = gameFruitTaskAppConvertor.toGameFruitTaskAwardConfig(
        param);
    if (CollectionUtils.isNotEmpty(param.getGameAwardId())) {
      if (param.getGameAwardId().contains("1") || param.getGameAwardId().contains("2")) {
        GameFruitRule gameFruitRule = gameFruitRuleService.getRule(param.getSysOrigin());
        awardPhoto.add(gameFruitRule.getVegetableImg());
        awardPhoto.add(gameFruitRule.getPizzaImg());
      } else {
        GameFruitPrize fruitPrize = gameFruitPrizeService.getById(param.getGameAwardId().get(0));
        if (Objects.nonNull(fruitPrize)) {
          awardPhoto.add(fruitPrize.getPrizeImg());
        }
      }

    }

    config.setAwardPhoto(StringUtils.join(awardPhoto, ","));
    config.setGameAwardId(StringUtils.join(param.getGameAwardId(), ","));
    if (Objects.nonNull(param.getId())) {
      gameFruitTaskAwardConfigService.updateSelectiveById(config);
      return;
    }
    gameFruitTaskAwardConfigService.save(config);
  }

  @Override
  public void deleteGameFruitTaskConfig(Long id) {
    gameFruitTaskAwardConfigService.removeById(id);
  }

  @Override
  public List<GameFruitAwardPictureDTO> getGameFruitImages(String sysOrigin) {
    List<GameFruitAwardPictureDTO> images = Lists.newArrayList();
    GameFruitRule gameFruitRule = gameFruitRuleService.getRule(sysOrigin);
    List<GameFruitPrize> gameFruitPrizes = gameFruitPrizeService.listPrize(sysOrigin);

    if (Objects.nonNull(gameFruitRule)) {
      images.add(new GameFruitAwardPictureDTO()
          .setImg(gameFruitRule.getPizzaImg())
          .setId(16643751L)
      );
      images.add(new GameFruitAwardPictureDTO()
          .setImg(gameFruitRule.getVegetableImg())
          .setId(16643752L)
      );
    }

    if (CollectionUtils.isNotEmpty(gameFruitPrizes)) {
      for (GameFruitPrize gameFruitPrize : gameFruitPrizes) {
        images.add(new GameFruitAwardPictureDTO()
            .setId(gameFruitPrize.getId())
            .setImg(gameFruitPrize.getPrizeImg())
        );
      }
    }
    return images;
  }
}
