package com.atmob.mobile_use_statistics.bean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeviceUsageInfo {
    private long totalUsedTime;
    private long lastUsedStartTime;
    private long lastUsedEndTime;
    private long lastUsedTime;
    private int interactiveCount;

    private final List<Event> events;

    public DeviceUsageInfo(List<Event> events) {
        this.events = events;
        initializeUsageData();
    }

    private void initializeUsageData() {
        if (events == null || events.isEmpty()) {
            return;
        }
        for (Event event : events) {
            long startTime = event.getStartTime();
            long endTime = event.getEndTime();
            long usedTime = event.getUsedTime();

            totalUsedTime += usedTime;

            if (lastUsedStartTime == 0 || startTime > lastUsedStartTime) {
                lastUsedStartTime = startTime;
            }
            if (endTime > lastUsedEndTime) {
                lastUsedEndTime = endTime;
                lastUsedTime = endTime - startTime;
            }
        }
        interactiveCount = events.size();
    }

    public long getTotalUsedTime() {
        return totalUsedTime;
    }

    public long getLastUsedStartTime() {
        return lastUsedStartTime;
    }

    public long getLastUsedEndTime() {
        return lastUsedEndTime;
    }

    public long getLastUsedTime() {
        return lastUsedTime;
    }

    public int getInteractiveCount() {
        return interactiveCount;
    }

    public List<Event> getEvents() {
        return events;
    }

    @Override
    public String toString() {
        return "DeviceUsageInfo{" +
                "totalUsedTime=" + totalUsedTime +
                ", lastUsedStartTime=" + lastUsedStartTime +
                ", lastUsedEndTime=" + lastUsedEndTime +
                ", lastUsedTime=" + lastUsedTime +
                ", interactiveCount=" + interactiveCount +
                ", events=" + events +
                '}';
    }

    public static class Event {
        private final long startTime;
        private final long endTime;
        private final long usedTime;

        public Event(long startTime, long endTime) {
            this.startTime = startTime;
            this.endTime = endTime;
            this.usedTime = endTime - startTime;
        }

        public long getStartTime() {
            return startTime;
        }

        public long getEndTime() {
            return endTime;
        }

        public long getUsedTime() {
            return usedTime;
        }

        @Override
        public String toString() {
            return "Event{" +
                    "startTime=" + startTime +
                    ", endTime=" + endTime +
                    ", usedTime=" + usedTime +
                    '}';
        }

        public Map<String, Object> toMap() {
            Map<String, Object> map = new HashMap<>();
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            return map;
        }
    }
}
