package com.sw.mes.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TimeUtil {

    private static final ThreadLocal<SimpleDateFormat> FORMATS = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    private Date start;

    private Date end;

    public static String union(List<String[]> list) throws ParseException {
        if (list == null || list.size() <=0 ) {
            return null;
        }
        List<TimeUtil> timeUtils = parseArray(list);
        for (int i = 0; i < timeUtils.size() - 1; i++) {
            long start = timeUtils.get(i).getStartTime();
            long end = timeUtils.get(i).getEndTime();
            for (int j = i + 1; j < timeUtils.size(); j++) {
                if (end > timeUtils.get(j).getStartTime() && start < timeUtils.get(j).getEndTime()) {
                    return new TimeUtil(start, end).toString();
                }
            }
        }
        return null;
    }

    public static  String checkContinue(List<String[]> list) throws ParseException {

        if (list == null || list.size() <=0 ) {
            return null;
        }
        List<TimeUtil> timeUtils = parseArray(list);

        timeUtils.sort((TimeUtil t1, TimeUtil t2) -> t1.getStart().compareTo(t2.getStart()));

        for (int i = 0; i < timeUtils.size() - 1; i++) {
            TimeUtil temp = timeUtils.get(i);
            if (temp.getStart().before(temp.getEnd()) && !temp.getEnd().equals(timeUtils.get(i + 1).getStart())) {
                return temp.toString();
            }
        }
        //校验最后一个期间
        TimeUtil last = timeUtils.get(timeUtils.size() - 1);
        if (!last.getStart().before(last.getEnd())) {
            return last.toString();
        }
        return null;
    }

    public static List<TimeUtil> parseArray(List<String[]> list) throws ParseException {
        int size = list.size();
        List<TimeUtil> timeUtils = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            String[] str = list.get(i);
            TimeUtil timeUtil = new TimeUtil(str[0], str[1]);
            timeUtils.add(timeUtil);
        }
        return timeUtils;
    }

    public TimeUtil(Date start, Date end) {
        if (start.after(end)) {
            throw new IllegalArgumentException("时间段无效(开始日期需要小于结束日期)");
        }
        this.start = start;
        this.end = end;
    }

    public TimeUtil(Long start, Long end) {
        this(new Date(start), new Date(end));
    }

    public TimeUtil(String start, String end) throws ParseException {
        this(parse(start), parse(end));
    }

    private static Date parse(String str) throws ParseException {
        return FORMATS.get().parse(str);
    }

    private static String format(Date str) {
        return FORMATS.get().format(str);
    }

    public long getStartTime() {
        return start.getTime();
    }

    public long getEndTime() {
        return end.getTime();
    }

    public Date getStart() {
        return start;
    }

    public void setStart(Date start) {
        this.start = start;
    }

    public Date getEnd() {
        return end;
    }

    public void setEnd(Date end) {
        this.end = end;
    }

    @Override
    public String toString() {
        return  "start=" + format(start) + ", end=" + format(end) ;
    }
}
