package com.red.circle.wallet.app.command.freight.query;

import com.google.common.collect.Sets;
import com.red.circle.common.business.dto.cmd.app.AppAccountCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.other.inner.endpoint.user.region.UserRegionClient;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.tool.core.json.JacksonUtils;
import com.red.circle.wallet.app.dto.clientobject.FreightSearchUserResultCO;
import com.red.circle.wallet.domain.gateway.FreightShipGateway;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 获取代理搜索用户结果.
 *
 * @author pengliang on 2023/4/2
 */
@Log4j2
@Component
@RequiredArgsConstructor
public class FreightSearchUserResultQryExe {

  private final UserRegionClient userRegionClient;
  private final UserProfileClient userProfileClient;
  private final FreightShipGateway freightShipGateway;

  public FreightSearchUserResultCO execute(AppAccountCmd cmd) {
    UserProfileDTO userProfile = ResponseAssert.requiredSuccess(
        userProfileClient.getByAccount(cmd.requireReqSysOrigin(),
            cmd.getAccount())
    );
    ResponseAssert.notNull(UserErrorCode.USER_INFO_NOT_FOUND, userProfile);

    Set<Long> userIds = Sets.newHashSet(cmd.getReqUserId());
    if (Boolean.FALSE.equals(Objects.equals(userProfile.getId(), cmd.getReqUserId()))) {
      userIds.add(userProfile.getId());
    }

    Map<Long, String> regionsCodes = ResponseAssert.requiredSuccess(
        userRegionClient.mapRegionCode(userIds));

    String reqUserRegionCode = regionsCodes.get(cmd.requiredReqUserId());
    String userRegionCode = regionsCodes.get(userProfile.getId());
    // 2023.7.5 菲律宾/2互通
    List<String> phRegions = List.of("PH1", "PH");
    boolean eqPh = equalsRegion(phRegions, reqUserRegionCode)
        && equalsRegion(phRegions, userRegionCode);

    boolean eqRegion = Objects.equals(reqUserRegionCode, userRegionCode) || eqPh;
    // 2023.6.2根据刘占洋需要货运代理收发区域必须精准相同才能交易
    ResponseAssert.isTrue(CommonErrorCode.REGION_NOT_SUPPORTED_ERROR, eqRegion);

    return new FreightSearchUserResultCO()
        .setUserProfile(userProfile)
        .setRatio(freightShipGateway.getFreightGoldToUsdRatio(userProfile.getId()));
  }


  private boolean equalsRegion(List<String> regions, String regionCode) {
    if (CollectionUtils.isEmpty(regions) || StringUtils.isBlank(regionCode)) {
      return Boolean.FALSE;
    }

    for (String region : regions) {
      if (equalsRegion(region, regionCode)) {
        return Boolean.TRUE;
      }
    }
    return Boolean.FALSE;
  }

  private boolean equalsRegion(String regionCodeOne, String regionCodeTwo) {
    return StringUtils.equalsIgnoreCase(regionCodeOne, regionCodeTwo);
  }
}
