package com.eactive.ext.kjb.statistics.mapping;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import com.eactive.eai.data.mapper.BaseMapperConfig;
import com.eactive.eai.rms.data.entity.onl.kjb.statistics.ApiStatsDay;
import com.eactive.eai.rms.data.entity.onl.kjb.statistics.ApiStatsHour;
import com.eactive.eai.rms.data.entity.onl.kjb.statistics.ApiStatsMinute;
import com.eactive.eai.rms.data.entity.onl.kjb.statistics.ApiStatsMonth;
import com.eactive.eai.rms.data.entity.onl.kjb.statistics.ApiStatsYear;
import com.eactive.ext.kjb.statistics.ui.ApiStatsUI;

@Mapper(config = BaseMapperConfig.class)
public interface ApiStatsUIMapper {

	DateTimeFormatter MINUTE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
	DateTimeFormatter HOUR_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00");
	DateTimeFormatter DAY_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

	@Mapping(source = "statTime", target = "statTime", qualifiedByName = "formatMinuteTime")
	ApiStatsUI toVo(ApiStatsMinute entity);

	@Mapping(source = "statTime", target = "statTime", qualifiedByName = "formatHourTime")
	ApiStatsUI toVo(ApiStatsHour entity);

	@Mapping(source = "statTime", target = "statTime", qualifiedByName = "formatDayTime")
	ApiStatsUI toVo(ApiStatsDay entity);

	@Mapping(source = "statTime", target = "statTime", qualifiedByName = "formatMonthTime")
	ApiStatsUI toVo(ApiStatsMonth entity);

	@Mapping(source = "statTime", target = "statTime", qualifiedByName = "formatYearTime")
	ApiStatsUI toVo(ApiStatsYear entity);

	@Named("formatMinuteTime")
	default String formatMinuteTime(LocalDateTime dateTime) {
		if (dateTime == null) {
			return null;
		}
		return dateTime.format(MINUTE_FORMATTER);
	}

	@Named("formatHourTime")
	default String formatHourTime(LocalDateTime dateTime) {
		if (dateTime == null) {
			return null;
		}
		return dateTime.format(HOUR_FORMATTER);
	}

	@Named("formatDayTime")
	default String formatDayTime(LocalDate date) {
		if (date == null) {
			return null;
		}
		return date.format(DAY_FORMATTER);
	}

	@Named("formatMonthTime")
	default String formatMonthTime(String yyyymm) {
		if (yyyymm == null || yyyymm.length() != 6) {
			return yyyymm;
		}
		return yyyymm.substring(0, 4) + "-" + yyyymm.substring(4, 6);
	}

	@Named("formatYearTime")
	default String formatYearTime(String yyyy) {
		return yyyy;
	}
}
