package com.red.circle.other.app.inner.endpoint.user.region;

import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.other.inner.model.cmd.user.SysRegionCmd;
import com.red.circle.other.inner.model.cmd.user.SysRegionConfigQryCmd;
import com.red.circle.other.inner.model.dto.user.reigon.RegionConfigDTO;
import com.red.circle.other.app.inner.service.user.region.RegionConfigClientService;
import com.red.circle.other.inner.endpoint.user.region.api.RegionConfigClientApi;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.red.circle.tool.core.text.StringUtils;
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 zongpubin on 2023/8/14
 */
@Validated
@RestController
@RequestMapping(value = RegionConfigClientApi.API_PREFIX, produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class RegionConfigClientEndpoint implements RegionConfigClientApi {

  private final RegionConfigClientService regionConfigClientService;

  @Override
  public ResultResponse<RegionConfigDTO> getRegionById(String regionId) {
    return ResultResponse.success(regionConfigClientService.getRegionById(regionId));
  }

  @Override
  public ResultResponse<RegionConfigDTO> getRegionByUserId(Long userId) {
    return ResultResponse.success(regionConfigClientService.getRegionByUserId(userId));
  }

  @Override
  public ResultResponse<Map<String, String>> getMetadata(String regionId) {
    return ResultResponse.success(regionConfigClientService.getMetadata(regionId));
  }

  @Override
  public ResultResponse<Map<String, String>> mapRegionNameBySysOrigin(String sysOrigin) {
    return ResultResponse.success(regionConfigClientService.mapRegionNameBySysOrigin(sysOrigin));
  }

  @Override
  public ResultResponse<Map<String, String>> mapRegionNameByRegionIds(Set<String> regionIds) {
    return ResultResponse.success(regionConfigClientService.mapRegionNameByRegionIds(regionIds));
  }

  @Override
  public ResultResponse<String> getRegionName(String regionId) {
    return ResultResponse.success(regionConfigClientService.getRegionName(regionId));
  }

  @Override
  public ResultResponse<Void> add(SysRegionCmd config) {
    regionConfigClientService.add(config);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<Void> update(SysRegionCmd config) {
    regionConfigClientService.update(config);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<Void> resetWithdrawal(Long regionId) {
    regionConfigClientService.resetWithdrawal(regionId);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<List<RegionConfigDTO>> listByCondition(SysRegionConfigQryCmd query) {
    List<RegionConfigDTO> regionConfigDTOS = regionConfigClientService.listByCondition(query);
    for (RegionConfigDTO regionConfigDTO : regionConfigDTOS) {
      if(StringUtils.isNotBlank(regionConfigDTO.getWithdrawalWays())){
        regionConfigDTO.setWithdrawalWaysList(StringUtils.splitAsList(regionConfigDTO.getWithdrawalWays(), ","));
      }
    }
    return ResultResponse.success(regionConfigDTOS);
  }

  @Override
  public ResultResponse<Map<Long, String>> mapRegionIdByUserIds(Set<Long> userIdSet) {
    return ResultResponse.success(regionConfigClientService.mapRegionIdByUserIds(userIdSet));
  }
}
