package com.lanyuanxiaoyao.service.command.provider;

import com.lanyuanxiaoyao.service.forest.service.InfoService;
import java.util.List;
import java.util.Optional;
import org.springframework.core.MethodParameter;
import org.springframework.shell.CompletionContext;
import org.springframework.shell.CompletionProposal;
import org.springframework.stereotype.Component;

/**
 * Flink Job Id 待选项
 *
 * @author ZhangJiacheng
 * @date 2022-05-30
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Component
public class FlinkJobIdProvider extends BaseValueProvider {
    private final InfoService infoService;

    public FlinkJobIdProvider(InfoService infoService) {
        this.infoService = infoService;
    }

    @Override
    public List<CompletionProposal> complete(MethodParameter parameter, CompletionContext completionContext, String[] hints) {
        Optional<String> field = getField(completionContext.getWords(), "--table");
        return field.map(id -> infoService.simpleTableMetas(field.get())
                        .collect(info -> new CompletionProposal(info.getFlinkJobId().toString()).description(info.getSchema()))
                        .toList())
                .orElseGet(() -> infoService.allFlinkJobId()
                        .collect(String::valueOf)
                        .collect(CompletionProposal::new)
                        .toList());
    }
}
