package org.tio.sitexxx.service.service.jpush.honor.messaging;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import org.tio.sitexxx.service.service.jpush.honor.message.Message;
import org.tio.sitexxx.service.service.jpush.honor.response.SendResponse;
import org.tio.sitexxx.service.service.jpush.huawei.util.ValidatorUtils;
import org.tio.sitexxx.service.service.jpush.honor.exception.HonorMesssagingException;

/**
 * <pre>
 *    @author : vagrancy
 *    e-mail : 18050829067@163.com
 *    time   : 2025-02-21 10:34
 *    desc   :
 * </pre>
 */
public class HonorMessaging {
    static final String INTERNAL_ERROR = "internal error";

    static final String UNKNOWN_ERROR = "unknown error";

    static final String KNOWN_ERROR = "known error";

    private final HonorApp app;
    private final Supplier<? extends HonorMessageClient> messagingClient;

    private HonorMessaging(Builder builder) {
        this.app = builder.app;
        this.messagingClient = Suppliers.memoize(builder.messagingClient);
    }

    /**
     * Gets the {@link HonorMessaging} instance for the specified {@link HonorApp}.
     *
     * @return The {@link HonorMessaging} instance for the specified {@link HonorApp}.
     */
    public static synchronized HonorMessaging getInstance(HonorApp app) {
        HonorMessagingService service = ImplHonorTrampolines.getService(app, SERVICE_ID, HonorMessagingService.class);
        if (service == null) {
            service = ImplHonorTrampolines.addService(app, new HonorMessagingService(app));
        }
        return service.getInstance();
    }

    private static HonorMessaging fromApp(final HonorApp app) {
        return HonorMessaging.builder()
                .setApp(app)
                .setMessagingClient(() -> HonorMessageClientImpl.fromApp(app))
                .build();
    }

    HonorMessageClient getMessagingClient() {
        return messagingClient.get();
    }

    /**
     * Sends message {@link Message}
     *
     * <p>If the {@code validateOnly} option is set to true, the message will not be actually sent. Instead
     * HCM performs all the necessary validations, and emulates the send operation.
     *
     * @param message      message {@link Message} to be sent.
     * @return {@link SendResponse}.
     * @throws HonorMesssagingException exception.
     */
    public SendResponse sendMessage(Message message) throws HonorMesssagingException {
        ValidatorUtils.checkArgument(message != null, "message must not be null");
        final HonorMessageClient messagingClient = getMessagingClient();
        return messagingClient.send(message, ImplHonorTrampolines.getAccessToken(app));
    }

    /**
     * HuaweiMessagingService
     */
    private static final String SERVICE_ID = HonorMessaging.class.getName();

    private static class HonorMessagingService extends HonorService<HonorMessaging> {

        HonorMessagingService(HonorApp app) {
            super(SERVICE_ID, HonorMessaging.fromApp(app));
        }

        @Override
        public void destroy() {

        }
    }

    /**
     * Builder for constructing {@link HonorMessaging}.
     */
    static Builder builder() {
        return new Builder();
    }

    static class Builder {
        private HonorApp app;
        private Supplier<? extends HonorMessageClient> messagingClient;

        private Builder() {
        }

        public Builder setApp(HonorApp app) {
            this.app = app;
            return this;
        }

        public Builder setMessagingClient(Supplier<? extends HonorMessageClient> messagingClient) {
            this.messagingClient = messagingClient;
            return this;
        }

        public HonorMessaging build() {
            return new HonorMessaging(this);
        }
    }

}
