package com.storlead.thirdparty.biz.service;

import com.storlead.thirdparty.api.dto.ThirdPartyTextMessageDTO;
import com.storlead.thirdparty.api.dto.ThirdPartyUserSyncDTO;
import com.storlead.thirdparty.api.enums.ThirdPartyChannel;
import com.storlead.thirdparty.api.service.ThirdPartyDirectorySyncService;
import com.storlead.thirdparty.api.service.ThirdPartyNotificationService;
import com.storlead.thirdparty.api.service.ThirdPartyUserSyncService;
import com.storlead.thirdparty.biz.config.ThirdPartyBizAutoConfiguration;
import com.storlead.thirdparty.spi.ThirdPartyDirectorySyncClient;
import com.storlead.thirdparty.spi.ThirdPartyMessageClient;
import com.storlead.thirdparty.spi.ThirdPartyUserSyncClient;

import java.util.EnumMap;
import java.util.List;
import java.util.Map;

/** 由 {@link ThirdPartyBizAutoConfiguration} 注册为 Spring Bean */
public class DefaultThirdPartyService implements ThirdPartyNotificationService, ThirdPartyDirectorySyncService, ThirdPartyUserSyncService {
    private final Map<ThirdPartyChannel, ThirdPartyMessageClient> messageClients = new EnumMap<>(ThirdPartyChannel.class);
    private final Map<ThirdPartyChannel, ThirdPartyDirectorySyncClient> syncClients = new EnumMap<>(ThirdPartyChannel.class);
    private final Map<ThirdPartyChannel, ThirdPartyUserSyncClient> userSyncClients = new EnumMap<>(ThirdPartyChannel.class);

    public DefaultThirdPartyService(List<ThirdPartyMessageClient> messageClientList,
                                    List<ThirdPartyDirectorySyncClient> syncClientList,
                                    List<ThirdPartyUserSyncClient> userSyncClientList) {
        for (ThirdPartyMessageClient client : messageClientList) {
            messageClients.put(client.channel(), client);
        }
        for (ThirdPartyDirectorySyncClient client : syncClientList) {
            syncClients.put(client.channel(), client);
        }
        for (ThirdPartyUserSyncClient client : userSyncClientList) {
            userSyncClients.put(client.channel(), client);
        }
    }

    @Override
    public boolean sendText(ThirdPartyChannel channel, ThirdPartyTextMessageDTO message) {
        return getMessageClient(channel).sendText(message);
    }

    @Override
    public String syncOrganization(ThirdPartyChannel channel) {
        return getSyncClient(channel).syncOrganization();
    }

    @Override
    public String syncUsers(ThirdPartyChannel channel) {
        return getSyncClient(channel).syncUsers();
    }

    @Override
    public List<ThirdPartyUserSyncDTO> syncUserOpenId(ThirdPartyChannel channel, List<ThirdPartyUserSyncDTO> users) {
        return getUserSyncClient(channel).syncUserOpenId(users);
    }

    private ThirdPartyMessageClient getMessageClient(ThirdPartyChannel channel) {
        ThirdPartyMessageClient client = messageClients.get(channel);
        if (client == null) {
            throw new IllegalArgumentException("Unsupported third-party message channel: " + channel);
        }
        return client;
    }

    private ThirdPartyDirectorySyncClient getSyncClient(ThirdPartyChannel channel) {
        ThirdPartyDirectorySyncClient client = syncClients.get(channel);
        if (client == null) {
            throw new IllegalArgumentException("Unsupported third-party sync channel: " + channel);
        }
        return client;
    }

    private ThirdPartyUserSyncClient getUserSyncClient(ThirdPartyChannel channel) {
        ThirdPartyUserSyncClient client = userSyncClients.get(channel);
        if (client == null) {
            throw new IllegalArgumentException("Unsupported third-party user sync channel: " + channel);
        }
        return client;
    }
}
