package com.resaneh24.util;

import android.util.Log;
import java.util.List;

/**
 * 12/6/2016
 *
 * @author Mahdi Taherian
 */

public class TimeTable extends StandardEntity {
    public List<Session> Sessions;
    public List<Session> Exceptions;
    public String HumanReadable;

    public boolean isOpen(long time) {
        return isOpen(this, time);
    }

    public static boolean isOpen(TimeTable timeTable, long time) {
        if (timeTable == null || timeTable.Sessions == null || timeTable.Sessions.isEmpty()) {
            return true;
        }
        Session session = timeTable.findCurrentSession(time);
        return session != null;
    }

    public long nextChange(long time) {
        recursiveCount = 0;
        Session currentSession = findCurrentSession(time);
        if (currentSession != null) {
            long nearestException = findNearest(Exceptions, null, time);

            long remaining = currentSession.remaining(time);
            return Math.min(remaining, nearestException);
        }
        return findNearest(Sessions, Exceptions, time);
    }

    private int recursiveCount = 0;

    private long findNearest(List<Session> Sessions, List<Session> Exceptions, long time) {
        long nearestTime = Long.MAX_VALUE;
        if (Sessions != null) {
            for (Session session : Sessions) {
                if (!session.isInPeriod(time)) {
                    continue;
                }

                long s;
                long t;
                if (session.Cycle <= 0) {
                    s = session.Start;
                    t = time;
                } else {
                    s = session.Start % session.Cycle;
                    t = time % session.Cycle;
                }

//                long end = s + session.Duration;
                long dif = s - t;
                if (dif < 0) {
                    dif += session.Cycle;
                }
                if (dif > 0) {
                    if (Exceptions != null && !Exceptions.isEmpty()) {
                        long future = time + dif;
                        for (Session ex : Exceptions) {
                            if (isInSession(ex, future)) {
                                if (recursiveCount++ > Sessions.size() * Exceptions.size()) {
                                    Log.w("TimeTable", "Problem in recursive calculation.");
                                } else {
                                    dif = Math.min(findNearest(Sessions, Exceptions, time + ex.remaining(future)), dif);
                                }
                            }
                        }
                    }
                } else {
                    throw new RuntimeException("Negative time difference: ");
//                    Log.w("TimeTable", "Negative time difference: " + dif);
//                    long r = session.remaining(time);
//                    dif = r;
                }
                nearestTime = Math.min(nearestTime, dif);
            }
        }
        return nearestTime;
    }

    private static boolean isInSession(Session session, long time) {
        if (!session.isInPeriod(time)) {
            return false;
        } else {
            return session.remaining(time) > 0;
        }
    }

    public Session findCurrentSession(long time) {
        if (Exceptions != null) {
            for (Session session : Exceptions) {
                if (isInSession(session, time)) {
                    return null;
                }
            }
        }

        for (Session session : Sessions) {
            if (isInSession(session, time)) {
                return session;
            }
        }
        return null;
    }


    public static class Session extends StandardEntity {
        public long Start;
        public long End;
        public long Duration;
        public long Cycle;

        public long remaining(long time) {
            if (isInPeriod(time)) {
                return -1;
            }
            long s;
            long t;
            if (Cycle <= 0) {
                s = Start;
                t = time;
            } else {
                s = Start % Cycle;
                t = time % Cycle;
            }
            if (t < s) {
                return -1;
            }
            long e = s + Duration;
            long r = e - t;
            return r > 0 ? r : 0;
        }

        public boolean isInPeriod(long time) {
            return time >= Start && (End <= 0 || time < End);
        }
    }

@@ -121,17 +121,16 @@
 ian%0A */%0A
-%0A
 public c
@@ -3919,24 +3919,25 @@
         if (
+!
 isInPeriod(t
@@ -4425,32 +4425,264 @@
 od(long time) %7B%0A
+            long e;%0A            if (Cycle == 0 && End == 0) %7B%0A                e = Start + Duration;%0A            %7D else if (End == 0) %7B%0A                e = Long.MAX_VALUE;%0A            %7D else %7B%0A                e = End;%0A            %7D%0A%0A
             retu
@@ -4704,29 +4704,16 @@
 t &&
- (End %3C= 0 %7C%7C
  time %3C 
 End)
@@ -4712,12 +4712,9 @@
 e %3C 
-End)
+e
 ;%0A  
@@ -4711,24 +4711,25 @@
 me %3C e;%0A        %7D%0A    %7D%0A
+%7D

Fix issue with zero End and Cycle