using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using nft.framework;
using System.Xml;
using nft.util;
using System.Diagnostics;
using nft.core.schedule;

namespace nft.test.test {
    class ClockTest {
        [TestEntry]
        public static void InitCalender() {
            try {
                GlobalModules.Initialize();
            }catch(Exception ex){
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
            }
        }

        [TestEntry]
        public static void TestClock() {
            Clock clock = Calendar.ActiveCalendar.CreateNewClock(new Date());
            clock.RegisterDailyHandler(new ClockEventCallback("Daily").Callback, new TimeOfDay(12, 34));
            clock.RegisterWeeklyHandler(new ClockEventCallback("Weekly_A").Callback, DayOfWeek.Friday, new TimeOfDay(23, 45));
            clock.RegisterYearlyHandler(new ClockEventCallback("Yearly2/2").Callback, Time.Create(new Date(0,2,2)));
            clock.RegisterYearlyHandler(new ClockEventCallback("Yearly3/3").Callback, Time.Create(new Date(0,3,3)));
            clock.RegisterWeeklyHandler(new ClockEventCallback("Weekly_B").Callback, DayOfWeek.Monday, new TimeOfDay(0, 0));
            clock.RegisterYearlyHandler(new ClockEventCallback("Yearly4/4").Callback, Time.Create(new Date(0, 4, 4), new TimeOfDay(4,4)));

            long l = Calendar.ActiveCalendar.TimeTickConverter.HourlyTicks;
            long lmax = Time.Create(new Date(2, 5, 1)).Ticks;
            while (lmax > 0) {
                clock.Tick(l);
                lmax -= l;
            }

        }

        class ClockEventCallback {
            String msg;
            int n;
            public ClockEventCallback(String m) {
                msg = m;
                n = 0;
            }

            public bool Callback(Clock clock) {
                Debug.Write(clock);
                Debug.Write(" " + msg);
                Debug.WriteLine(" " + n++);
                return n < 10;
            }
        }
    }
}
