package com.rj.scheduler;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
 * 为 {@link LBAdminPullScheduler} 集成测试提供可拨 {@link Clock}（{@link MutableClock}）。
 */
@TestConfiguration
public class LBAdminPullSchedulerTestClockConfiguration {

    @Bean
    @Primary
    MutableClock lbAdminPullTestMutableClock() {
        return new MutableClock();
    }

    /** 测试中可切换时刻的 {@link Clock}，用于固定 18:10～23:59 时间窗口。 */
    public static final class MutableClock extends Clock {

        private volatile Clock delegate = Clock.systemDefaultZone();

        public void setTo(ZonedDateTime zdt) {
            delegate = Clock.fixed(zdt.toInstant(), zdt.getZone());
        }

        @Override
        public ZoneId getZone() {
            return delegate.getZone();
        }

        @Override
        public Clock withZone(ZoneId zone) {
            return delegate.withZone(zone);
        }

        @Override
        public Instant instant() {
            return delegate.instant();
        }
    }
}
