package com.inspect.guideline.runner;

import com.inspect.guideline.constant.PatrolGuidelineCollection;
import com.inspect.guideline.domain.PatrolGuideline;
import com.inspect.guideline.domain.PatrolGuidelineRule;
import com.inspect.guideline.service.IPatrolGuidelineRuleService;
import com.inspect.guideline.service.IPatrolGuidelineService;

import java.util.List;
import java.util.Objects;
import javax.annotation.PostConstruct;

import org.springframework.stereotype.Component;

@Component
public class PatrolGuidelineRunner {
    private final IPatrolGuidelineService patrolGuidelineService;
    private final IPatrolGuidelineRuleService patrolGuidelineRuleService;

    public PatrolGuidelineRunner(IPatrolGuidelineService patrolGuidelineService, IPatrolGuidelineRuleService patrolGuidelineRuleService) {
        this.patrolGuidelineService = patrolGuidelineService;
        this.patrolGuidelineRuleService = patrolGuidelineRuleService;
    }

    @PostConstruct
    public void init() {
        List<PatrolGuideline> patrolGuidelines = this.patrolGuidelineService.selectPatrolGuidelineList(new PatrolGuideline());
        List<PatrolGuidelineRule> patrolGuidelineRules = this.patrolGuidelineRuleService.selectPatrolGuidelineRuleList(new PatrolGuidelineRule());

        for (PatrolGuideline patrolGuideline : patrolGuidelines) {
            PatrolGuidelineCollection.INSTANCE.put(patrolGuideline.getGuidelineId(), patrolGuideline);

            for (PatrolGuidelineRule patrolGuidelineRule : patrolGuidelineRules) {
                if (Objects.equals(patrolGuidelineRule.getGuidelineId(), patrolGuideline.getGuidelineId())) {
                    patrolGuideline.getPatrolGuidelineRules().add(patrolGuidelineRule);
                }
            }
        }

    }
}
