package com.its.vms.service;

import com.its.app.utils.Elapsed;
import com.its.vms.dao.mapper.VmsFontMapper;
import com.its.vms.dto.TbVmsFontColrDto;
import com.its.vms.dto.TbVmsFontNameDto;
import com.its.vms.entity.TbVmsFontColr;
import com.its.vms.entity.TbVmsFontName;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Service
@RequiredArgsConstructor
public class VmsFontService {

    private final VmsFontMapper mapper;

    private final ConcurrentHashMap<Integer, TbVmsFontNameDto> nameMap = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<Integer, TbVmsFontColrDto> colrMap = new ConcurrentHashMap<>();

    public TbVmsFontNameDto findFontName(Integer vmsFontNameCd) {
        return this.nameMap.get(vmsFontNameCd);
    }
    public TbVmsFontColrDto findFontColor(Integer vmsFontColrCd) {
        return this.colrMap.get(vmsFontColrCd);
    }

    public Color getFontColor(Integer colrCd) {
        if (colrCd == 0) return Color.BLACK;
        if (colrCd == 1) return Color.RED;
        if (colrCd == 2) return Color.GREEN;
        if (colrCd == 3) return Color.YELLOW;
        TbVmsFontColrDto obj = this.colrMap.get(colrCd);
        if (obj != null) {
            return obj.getVmsFontColor();
        }
        if (colrCd == 4) return Color.ORANGE;
        if (colrCd == 5) return new Color(16776960);    // aqua
        return Color.BLACK;
    }

    public String getFontName(Integer vmsFontNameCd) {
        TbVmsFontNameDto obj = findFontName(vmsFontNameCd);
        if (obj == null) {
            return "맑은 고딕";
        }
        return obj.getVmsFontNameNm();
    }
    public void loadDb() {
        loadFontNameInfo();
        loadFontColorInfo();
    }

    public void loadFontNameInfo() {
        Elapsed elapsed = new Elapsed();
        try {
            List<TbVmsFontName> result  = this.mapper.selectFontName();
            log.info("VmsFontService.loadFontNameInfo: {} EA", result.size());
            for (TbVmsFontName data : result) {
                TbVmsFontNameDto obj = data.toDto();
                this.nameMap.put(obj.getVmsFontNameCd(), obj);
            }
        }
        catch (Exception e) {
            log.error("VmsFontService.loadFontNameInfo: {}.", e.getMessage());
        }
        log.info("VmsFontService.loadFontNameInfo: {} ms.", elapsed.milliSeconds());
    }

    public void loadFontColorInfo() {
        Elapsed elapsed = new Elapsed();
        try {
            List<TbVmsFontColr> result  = this.mapper.selectFontColr();
            log.info("VmsFontService.loadFontColorInfo: {} EA", result.size());
            for (TbVmsFontColr dto : result) {
                TbVmsFontColrDto obj = dto.toDto();
                this.colrMap.put(obj.getVmsFontColrCd(), obj);
            }
        }
        catch (Exception e) {
            log.error("VmsFontService.loadFontColorInfo: {}.", e.toString());
        }
        log.info("VmsFontService.loadFontColorInfo: {} ms.", elapsed.milliSeconds());
    }

}
