package com.utic.center.utic.traf.server.controller;

import com.utic.center.common.utils.LogUtils;
import com.utic.center.common.utils.TimeUtils;
import com.utic.center.utic.traf.server.config.ApplicationConfig;
import com.utic.center.utic.traf.server.dto.BatchJobInfoDto;
import com.utic.center.utic.traf.server.repository.ApplicationRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("")
public class UticTrafServerRestController {

    private final ApplicationConfig config;

    @GetMapping(value = "/info", produces = {"application/json; charset=utf8"})
    public String stringInfo() {
        String sep = System.lineSeparator();
        String heading = "----------------------------------------------------------------------------------------------------------------------------------------------------------";

        StringBuilder sb = new StringBuilder();
        sb.append(heading).append(sep);
        sb.append("UTIC Center ITS Traffic Processing Server").append(sep);
        sb.append(heading).append(sep);
        sb.append(String.format("Process Id: %s, %s, %s", this.config.getProcessId(), this.config.getBootingTime(), TimeUtils.now())).append(sep);
        sb.append(heading).append(sep);
        sb.append(String.format("%s %-20.20s %-20.20s    TOTAL RCV_TRAF  COLLECT   FUSION  MISSING UTIADMIN  TRAFFIC     DWDB  PREPARE    AFTER",
                " SEQ", "Start", "End")).append(sep);
        sb.append(heading).append(sep);
        int total = 0;

        int jobSize = ApplicationRepository.batchJobInfoList.size();
        for (int ii = jobSize-1; ii >= 0; ii--) {
            BatchJobInfoDto jobInfo = ApplicationRepository.batchJobInfoList.get(ii);
            total++;

            sb.append(String.format("%4d %-20.20s %-20.20s %s %s %s %s %s %s %s %s %s %s",
                    total,
                    jobInfo.getStartTime(), jobInfo.getEndTime(),
                    LogUtils.numberLog(jobInfo.getElapsedTime()),
                    LogUtils.numberLog(jobInfo.getAsnEtlpTime()),
                    LogUtils.numberLog(jobInfo.getCollectTime()),
                    LogUtils.numberLog(jobInfo.getFusionTime()),
                    LogUtils.numberLog(jobInfo.getMissingTime()),
                    LogUtils.numberLog(jobInfo.getSaveCenterTime()),
                    LogUtils.numberLog(jobInfo.getSaveTrafficTime()),
                    LogUtils.numberLog(jobInfo.getSaveDwdbTime()),
                    LogUtils.numberLog(jobInfo.getPrepareTime()),
                    LogUtils.numberLog(jobInfo.getPrcsAfterTime())
                    )).append(sep);
        }
        sb.append(heading).append(sep);
        return sb.toString();
    }


}
