package vn.azteam.tpf.service.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public final class DateTimeUtil {

//    private static final String[] DAY_OF_WEEKS = {"Chủ nhật","Thứ hai","Thứ ba","Thứ tư","Thứ năm","Thứ sáu","Thứ bảy"};
    private static final String[] DAY_OF_WEEKS = {"CN","T2","T3","T4","T5","T6","T7"};

    private DateTimeUtil() {
    }

    /**
     * Generate vietnammese format date from Date String pattern "yyyy-MM-dd".
     *
     * @return the generated date
     */
    public static String convertVietnameseDateFromDateString(String dateInput) {
        String pattern = "dd/MM/yyyy";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        List<String> listFullFormatDate = new ArrayList<>();
        try {
            Date thisDate = new SimpleDateFormat("yyyy-MM-dd").parse(dateInput);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(thisDate);
            Integer DAY_OF_WEEK = calendar.get(Calendar.DAY_OF_WEEK)-1;
            SimpleDateFormat simpleFormat = new SimpleDateFormat(pattern);
            String fullDateStr = simpleDateFormat.format(thisDate);
            listFullFormatDate.add(DAY_OF_WEEKS[DAY_OF_WEEK].toString());
            listFullFormatDate.add(fullDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return listFullFormatDate.size() > 0 ? String.join(" ",listFullFormatDate) : "";
    }

    public static String convertInstantToDateStringByPattern(Instant dateInput, String pattern) {
        pattern = !pattern.isEmpty() ? pattern : "yyyy-MM-dd HH:mm:ss";
        ZoneId zoneId = ZoneId.systemDefault();
        if(dateInput != null){
            DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(pattern)
                .withZone(zoneId);
            return DATE_TIME_FORMATTER.format(dateInput);
        }
        return null;
    }

    public static Instant convertDateFromDateString(String dateInput, String pattern) {
        Instant result = Instant.parse(dateInput);
        return result;
    }

}
