package com.izouma.jmrh.service.stat;

import com.izouma.jmrh.domain.stat.Category;
import com.izouma.jmrh.domain.stat.CityData;
import com.izouma.jmrh.domain.stat.Threshold;
import com.izouma.jmrh.repo.stat.CategoryRepo;
import com.izouma.jmrh.repo.stat.ThresholdRepo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.sql.Array;
import java.util.List;

@Service
@AllArgsConstructor
public class ThresholdService {

    private ThresholdRepo thresholdRepo;
    private CategoryRepo categoryRepo;

    public void init() {
        thresholdRepo.deleteAll();
        List<Category> categories = categoryRepo.findAll();

        categories.forEach(category -> {
            thresholdRepo.save(Threshold.builder()
                    .categoryId(category.getId())
                    .maxValue(getRandom(800, 500))
                    .minValue(getRandom(30, 100))
                    .increaseValue(getRandom(50, 100))
                    .decreaseValue(getRandom(50, 100))
                    .build());
        });
    }


    public Threshold getOneThreshold() {
        List<Threshold> thresholds = thresholdRepo.findAll();
        int _index = getRandom(0, thresholds.size());

        return thresholds.get(_index);

    }

    public int getRandom(int max, int min) {
        return (int) Math.floor((Math.random() * (max - min) + min));
    }

}
