package ru.ulstu.commit.model;

import ru.ulstu.odin.model.annotation.OdinCaption;
import ru.ulstu.odin.model.annotation.OdinDate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

public class CommitListDto {
    private final static String DELIMITER = ";";
    @OdinCaption("Дата")
    @OdinDate(type = OdinDate.OdinDateType.DATETIME)
    private final Date date;
    @OdinCaption("Пользователь")
    private final String userName;
    @OdinCaption("Сообщение")
    private final String message;

    public CommitListDto(String data) {
        List<String> datas = Arrays.stream(data.split(DELIMITER))
                .filter(d -> d != null && !d.isEmpty())
                .collect(Collectors.toList());
        if (datas.size() > 2) {
            this.userName = datas.get(0);
            this.message = datas.get(2);
            SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy Z", Locale.US);
            try {
                this.date = format.parse(datas.get(1));
            } catch (ParseException e) {
                throw new RuntimeException("wrong commits date");
            }
            return;
        }
        throw new RuntimeException("wrong commits data");
    }

    public String getMessage() {
        return message;
    }

    public Date getDate() {
        return date;
    }

    public String getUserName() {
        return userName;
    }
}
