package com.atmob.mobile_use_statistics.bean;

import java.util.List;

public class AppUsageInfo {
    private final String packageName;
    private final String appName;
    private final List<Event> appUsageEvents;

    private long totalUsedTime;
    private long lastUsedStartTime;
    private long lastUsedEndTime;
    private long lastUsedTime;
    private int launchCount;

    public AppUsageInfo(String packageName, String appName, List<Event> appUsageEvents) {
        this.packageName = packageName;
        this.appName = appName;
        this.appUsageEvents = appUsageEvents;
        initializeUsageData();
    }

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

            totalUsedTime += usedTime;

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

    public String getPackageName() {
        return packageName;
    }

    public String getAppName() {
        return appName;
    }

    public long getTotalUsedTime() {
        return totalUsedTime;
    }

    public long getLastUsedStartTime() {
        return lastUsedStartTime;
    }

    public long getLastUsedEndTime() {
        return lastUsedEndTime;
    }

    public long getLastUsedTime() {
        return lastUsedTime;
    }

    public List<Event> getAppUsageEvents() {
        return appUsageEvents;
    }

    public int getLaunchCount() {
        return launchCount;
    }

    @Override
    public String toString() {
        return "AppUsageInfo{" +
                "packageName='" + packageName + '\'' +
                ", appName='" + appName + '\'' +
                ", appUsageEvents=" + appUsageEvents +
                ", totalUsedTime=" + totalUsedTime +
                ", lastUsedStartTime=" + lastUsedStartTime +
                ", lastUsedEndTime=" + lastUsedEndTime +
                ", lastUsedTime=" + lastUsedTime +
                ", launchCount=" + launchCount +
                '}';
    }

    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 +
                    '}';
        }
    }
}
