package com.its.op.service.its.dashboard;

import com.its.op.dao.mapper.its.dashboard.DashboardMapper;
import com.its.op.dto.its.dashboard.CmtrGradCountSttsDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class DashboardService {

    private final DashboardMapper repo;

    protected void checkGradeRate(List<CmtrGradCountSttsDto> result) {
        result.forEach(obj -> {
            float total = obj.getLtc0() + obj.getLtc1() + obj.getLtc2() + obj.getLtc3();
            obj.setTotal((int)total);
            if (total == 0.0) {
                obj.setRateLtc1(0.0);
                obj.setRateLtc2(0.0);
                obj.setRateLtc3(0.0);
                obj.setRateLtc0(0.0);
            }
            else {
                double ltc0 = Double.parseDouble(String.format("%.1f", obj.getLtc0() / total * 100.0));
                double ltc1 = Double.parseDouble(String.format("%.1f", obj.getLtc1() / total * 100.0));
                double ltc2 = Double.parseDouble(String.format("%.1f", obj.getLtc2() / total * 100.0));
                double ltc3 = Double.parseDouble(String.format("%.1f", obj.getLtc3() / total * 100.0));

                double fSum = ltc0 + ltc1 + ltc2 + ltc3;
                double[] rates = { ltc0, ltc1, ltc2, ltc3 };
                if (fSum > 100.0) {
                    for (int ii = 0; ii < 4; ii++) {
                        if (rates[ii] > 0.0) {
                            rates[ii] -= (fSum - 100.0);
                            break;
                        }
                    }
                }
                if (fSum < 100.0) {
                    for (int ii = 0; ii < 4; ii++) {
                        if (rates[ii] > 0.0) {
                            rates[ii] += (100.0 - fSum);
                            break;
                        }
                    }
                }
                obj.setRateLtc0(rates[0]);
                obj.setRateLtc1(rates[1]);
                obj.setRateLtc2(rates[2]);
                obj.setRateLtc3(rates[3]);
            }
        });
    }

    /**
     * 소틍등급상태 링크
     * @return
     */
    public List<CmtrGradCountSttsDto> findAllCmtrGradCountSttsLink() {
        List<CmtrGradCountSttsDto> result = this.repo.findAllCmtrGradCountSttsLink();
        checkGradeRate(result);
        return result;
    }

    /**
     * 소틍등급상태 정보제공구간
     * @return
     */
    public List<CmtrGradCountSttsDto> findAllCmtrGradCountSttsIfsc() {
        List<CmtrGradCountSttsDto> result = this.repo.findAllCmtrGradCountSttsIfsc();
        checkGradeRate(result);
        return result;
    }

    /**
     * 소틍등급상태 도로
     * @return
     */
    public List<CmtrGradCountSttsDto> findAllCmtrGradCountSttsRoad() {
        List<CmtrGradCountSttsDto> result = this.repo.findAllCmtrGradCountSttsRoad();
        checkGradeRate(result);
        return result;
    }
}
