/*
 * Copyright (C) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.ase.facade;

import android.app.AlarmManager;
import android.app.Service;

import com.google.ase.jsonrpc.RpcReceiver;
import com.google.ase.rpc.Rpc;
import com.google.ase.rpc.RpcDefault;
import com.google.ase.rpc.RpcOptional;
import com.google.ase.rpc.RpcParameter;
import com.google.ase.trigger.AlarmTriggerManager;
import com.google.ase.trigger.TriggerRepository;

/**
 * A facade exposing the functionality of the {@link AlarmManager}.
 * 
 * @author Felix Arends (felix.arends@gmail.com)
 *
 */
public class AlarmManagerFacade implements RpcReceiver {
  final AlarmTriggerManager mAlarmManager;

  public AlarmManagerFacade(Service service, EventFacade eventFacade,
      TriggerRepository triggerRepository) {
    mAlarmManager = new AlarmTriggerManager(service, triggerRepository);
  }

  @Rpc(description = "schedules a script for (inexact) regular execution - saves battery in "
      + "comparison to scheduleRepeating")
  public void scheduleInexactRepeating(
      @RpcParameter(name = "interval", description = "the interval between invocations, in seconds")
      Double interval,
      @RpcParameter(name = "script", description = "the script to execute")
      String script,
      @RpcParameter(name = "wakeup", description = "whether or not to wakeup the device if asleep")
      @RpcDefault("true") 
      Boolean wakeup) {
    mAlarmManager.scheduleInexactRepeating(interval, script, wakeup);
  }

  @Rpc(description = "scheudles a script for (exact) regular execution")
  public void scheduleRepeating(
      @RpcParameter(name = "interval", description = "interval between invocations, in seconds")
      Double interval,
      @RpcParameter(name = "script", description = "script to execute")
      String script,
      @RpcParameter(name = "firstExecutionTime", description = "first time to execute script, in seconds since epoch") @RpcOptional
      Double firstExecutionTime,
      @RpcParameter(name = "wakeup", description = "whether or not to wake up the device if asleep")
      @RpcDefault("true")
      Boolean wakeup) {
    if (firstExecutionTime == null) {
      // If the default value is passed, use the current time.
      firstExecutionTime = currentTime();
    }

    mAlarmManager.scheduleRepeating(interval, script, wakeup);
  }
  
  @Rpc(description = "schedules one-time execution of a script")
  public void scheudleAbsolute(
      @RpcParameter(name = "script", description = "script to execute")
      String script,
      @RpcParameter(name = "time", description = "time of invocation, in seconds since epoch")
      Double time,
      @RpcParameter(name = "wakeup", description = "whether or not to wake up the device if asleep")
      @RpcDefault("true")
      Boolean wakeup) {
    mAlarmManager.schedule(time, script, wakeup);
  }
  
  @Rpc(description = "schedules one-time execution of a script, a given number of seconds from now")
  public void scheduleRelative(
      @RpcParameter(name = "script", description = "script to execute")
      String script,
      @RpcParameter(name = "secondsFromNow", description = "after what time to execute the script")
      Double secondsFromNow,
      @RpcParameter(name = "wakeup", description = "whether or not to wake up the device if asleep")
      @RpcDefault("true")
      Boolean wakeup) {
    mAlarmManager.schedule(currentTime() + secondsFromNow, script, wakeup);
  }

  @Rpc(description = "cancels all scheduled regular executions of a given script")
  public void cancelRepeating(@RpcParameter(name = "script") String script) {
    mAlarmManager.cancelByScriptName(script);
  }

  /** Returns the current time, in seconds since epoch. */
  private Double currentTime() {
    return (1.0d * System.currentTimeMillis()) / 1000;
  }

  @Override
  public void shutdown() {
  }
}

@@ -1131,24 +1131,25 @@
 ail.com)%0D%0A *
+ 
 %0D%0A */%0D%0Apubli
@@ -1717,39 +1717,32 @@
 ns, in seconds%22)
-%0D%0A     
  Double interval
@@ -1811,39 +1811,32 @@
 ipt to execute%22)
-%0D%0A     
  String script,%0D
@@ -1927,39 +1927,32 @@
 vice if asleep%22)
-%0D%0A     
  @RpcDefault(%22tr
@@ -1956,24 +1956,16 @@
 %22true%22) 
-%0D%0A      
 Boolean 
@@ -2257,23 +2257,16 @@
 econds%22)
-%0D%0A     
  Double 
@@ -2339,39 +2339,32 @@
 ipt to execute%22)
-%0D%0A     
  String script,%0D
@@ -2495,23 +2495,16 @@
 Optional
-%0D%0A     
  Double 
@@ -2616,39 +2616,32 @@
 vice if asleep%22)
-%0D%0A     
  @RpcDefault(%22tr
@@ -2636,39 +2636,32 @@
 cDefault(%22true%22)
-%0D%0A     
  Boolean wakeup)
@@ -2880,34 +2880,32 @@
  wakeup);%0D%0A  %7D%0D%0A
-  
 %0D%0A  @Rpc(descrip
@@ -2974,18 +2974,18 @@
 oid sche
-u
 d
+u
 leAbsolu
@@ -3052,39 +3052,32 @@
 ipt to execute%22)
-%0D%0A     
  String script,%0D
@@ -3171,23 +3171,16 @@
  epoch%22)
-%0D%0A     
  Double 
@@ -3278,39 +3278,32 @@
 vice if asleep%22)
-%0D%0A     
  @RpcDefault(%22tr
@@ -3298,39 +3298,32 @@
 cDefault(%22true%22)
-%0D%0A     
  Boolean wakeup)
@@ -3382,18 +3382,16 @@
 ;%0D%0A  %7D%0D%0A
-  
 %0D%0A  @Rpc
@@ -3590,23 +3590,16 @@
 xecute%22)
-%0D%0A     
  String 
@@ -3706,23 +3706,16 @@
 script%22)
-%0D%0A     
  Double 
@@ -3831,23 +3831,16 @@
 asleep%22)
-%0D%0A     
  @RpcDef
@@ -3851,23 +3851,16 @@
 (%22true%22)
-%0D%0A     
  Boolean

Fix typo.