package com.fufan.mtsaas.aliyun;

import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Random;

@RequiredArgsConstructor
public class RandomVerificationCodeGenerator implements VerificationCodeGenerator {

    private static final int NUMBER = 6;

    private final Random random = new Random();

    private final VerificationCodeGenerator fixedVerificationCodeGenerator = new FixedVerificationCodeGenerator();

    private final ApplicationProperties applicationProperties;

    @Override
    public String generate(String phoneNumber) {
        List<String> fixedVerificationCodePhoneNumbers = applicationProperties.getSms().getFixedVerificationCodePhoneNumbers();
        if (CollectionUtils.isNotEmpty(fixedVerificationCodePhoneNumbers)
                && fixedVerificationCodePhoneNumbers.contains(phoneNumber)) {
            return fixedVerificationCodeGenerator.generate(phoneNumber);
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < NUMBER; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }

}
