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

import com.utic.center.common.utils.TimeUtils;
import com.utic.center.utic.ptis.server.config.ApplicationConfig;
import com.utic.center.utic.ptis.server.dto.BatchJobInfoDto;
import com.utic.center.utic.ptis.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;

import java.util.concurrent.TimeUnit;

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

    private final ApplicationConfig config;
    
    private String elapsedTimeStr(long elapsed) {
        long hours = TimeUnit.HOURS.convert(elapsed, TimeUnit.MILLISECONDS);
        long minutes = TimeUnit.MINUTES.convert(elapsed, TimeUnit.MILLISECONDS);
        long seconds = TimeUnit.SECONDS.convert(elapsed, TimeUnit.MILLISECONDS);
        long miliSeconds = TimeUnit.MILLISECONDS.convert(elapsed, TimeUnit.MILLISECONDS) % 1000L;
//        long microSeconds = TimeUnit.MICROSECONDS.convert(elapsed, TimeUnit.MILLISECONDS) % 1000L;
//        long nanoSeconds = TimeUnit.NANOSECONDS.convert(elapsed, TimeUnit.MILLISECONDS) % 1000L;
        if (hours > 0L) {
            return String.format("%,d hr. %2d min. %2d sec. %3d ms.", hours, minutes, seconds, miliSeconds);
        } else if (minutes > 0L) {
            return String.format("%2d min. %2d sec. %3d ms.", minutes, seconds, miliSeconds);
        } else if (seconds > 0L) {
            return String.format("%2d sec. %3d ms.", seconds, miliSeconds);
        } else {
            return String.format("%3d ms.", miliSeconds);
        }
    }
    
    @GetMapping(value = "/info", produces = {"application/json; charset=utf8"})
    public String infoString() {
        String sep = System.lineSeparator();
        String heading = "---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";

        StringBuilder sb = new StringBuilder();
        sb.append(heading).append(sep);
        sb.append("UTIC Center PTIS ETLP Server Running Information").append(sep);
        sb.append(heading).append(sep);
        sb.append(String.format("Process Id: %s, %s, %s, %s", this.config.getProcessId(), this.config.getProcessName(), this.config.getBootingTime(), TimeUtils.now())).append(sep);
        sb.append(heading).append(sep);
        sb.append(String.format("%s %-20.20s %-20.20s %-31.31s %-31.31s %-31.31s %s",
                " SEQ", "Start", "End", "TRAF(LastRegDate(CURR/TOT))", "EVNT(LastRegDate(CURR/TOT))", "INCD(LastRegDate(CURR/TOT)", "Elapsed Time")).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++;
            String startTime = jobInfo.getStartTime();
            String endTime = jobInfo.getEndTime();
            String elapsedTime = elapsedTimeStr(jobInfo.getElapsedTime());
            String traf = String.format("%d/%d", jobInfo.getTraf(), jobInfo.getTotTraf());
            String evnt = String.format("%d/%d", jobInfo.getEvnt(), jobInfo.getTotEvnt());
            String incd = String.format("%d/%d", jobInfo.getIncd(), jobInfo.getTotIncd());
            sb.append(String.format("%4d %-20.20s %-20.20s %-14.14s %-16.16s %-14.14s %-16.16s %-14.14s %-16.16s %s",
                    total,
                    startTime, endTime,
                    jobInfo.getTrafDate(), traf,
                    jobInfo.getEvntDate(), evnt,
                    jobInfo.getIncdDate(), incd,
                    elapsedTime)).append(sep);
        }
        sb.append(heading).append(sep);
        return sb.toString();
    }

}
