package com.cygnusa.hyperm.wrappers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;

public class TimeTaken {
    private String timetaken;
    private String fromtime;
    private String totime;

    public String getTimetaken() {
        return timetaken;
    }

    public void setTimetaken(String timetaken) {
        this.timetaken = timetaken;
    }

    public String getFromtime() {
        return fromtime;
    }

    public void setFromtime(String fromtime) {
        this.fromtime = fromtime;
    }

    public String getTotime() {
        return totime;
    }

    public void setTotime(String totime) {
        this.totime = totime;
    }

    public String getTimeString(boolean isFrom) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            SimpleDateFormat format1 = new SimpleDateFormat("hh:mm:ss a", Locale.getDefault());
            Date max = format.parse(isFrom ? fromtime : totime);
            return format1.format(Objects.requireNonNull(max));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "00:00";
    }

    public String getFromTimeString() {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            SimpleDateFormat format1 = new SimpleDateFormat("hh:mm a", Locale.getDefault());
            Date max = format.parse(fromtime);
            return format1.format(Objects.requireNonNull(max));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "00:00";
    }

    public String getToTimeString() {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            SimpleDateFormat format1 = new SimpleDateFormat("hh:mm a", Locale.getDefault());
            Date max = format.parse(totime);
            return format1.format(Objects.requireNonNull(max));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "00:00";
    }

    public int getTimeTakenSeconds() {
        if (timetaken == null || timetaken.trim().isEmpty()) {
            return 0;
        } else {
            return Integer.parseInt(timetaken);
        }
    }

    public boolean isActive() {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            long m = Objects.requireNonNull(format.parse(totime)).getTime();
            long d = new Date().getTime();
            return ((d - m) / 1000) <= 60;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
