(define (hours)   (car (gettime)))
(define (mins)    (car (cdr (gettime))))
(define (year)    (car (getdate)))
(define (month)   (car (cdr (getdate))))
(define (day)     (car (cdr (cdr (getdate)))))
(define (lat x)   (car x))
(define (lon x)   (car (cdr x)))

(define (eastof x)  (> (lon (gps)) (lon x)))
(define (westof x)  (< (lon (gps)) (lon x)))
(define (northof x) (> (lat (gps)) (lat x)))
(define (southof x) (< (lat (gps)) (lat x)))

; DD  - Decimal Degree format
; DMS - Degree/Minute/Second format
; DMM - Degree/Decimal Minute format
;
; defined in DMM-ish style (call it DMMt).
; Most significant digts are degree, remaining are in minute, so:
;
; q = floor(DMMt/100)
; DD = q + ((DMMt - (100*q))/60)
;
; for example:
;
; x = 4229.532
; q = floor(x/100)
; q + ((x - (100*q))/60)
;
; --> 42.49220
;

(define bound0 '(4227.139 -7641.424))
(define bound1 '(4222.689 -7616.695))


(define city_bounds
  '(
    (and (= (routenum) 1) (westeof bound0))
    (and (= (routenum) 2) (eastof bound1))
  )
)

; ---

(define (dec_local_balance)
  (let ((balance (string->number (lookaside_param))))
    (cond
      ( (< balance 1)
        "0"
      )
      ( else
        (let ((foo (number->string (- balance 1))))
          (set_lookaside foo)
          foo
        )
      )
    )
  )
)

; ----

(define (rule_NRIDE)
  ;;accept all
  (let ((rides_left (dec_local_balance)))
    (tell_rider (string-append rides_left " Rides left"))
    (accept_fare_no_passback
      (string-append rides_left " Rides left")
      (string-append rides_left " Rides left")
      0
    )
  )
)

(define (rule_NDAY)
  ;;accept all
  (let ((tokens (strtok (param) " "))) 
    (cond
      ( (pair? (cdr tokens))
        (tell_rider (string-append "Expires " (car (cdr tokens))) )
        (accept_fare
          (string-append "Expires " (car (cdr tokens)))
          (string-append "Expires " (car (cdr tokens)))
          0
        )
      )
      ( else
        (tell_rider (string-append (car tokens) " Day pass"))
        (accept_fare
          (string-append (car tokens) " Day pass")
          (string-append (car tokens) " Day pass")
          0
        )
      )
    )
  )
)

(define (rule_CASH-ADULT)
  (accept_fare_no_passback "Adult Cash Fare" "Adult Cash Fare" 150)
  (drop_vault)
)

(define (rule_CASH-YOUTH)
  (accept_fare_no_passback "Youth Cash Fare" "Youth Cash Fare" 75)
  (drop_vault)
)

(define (rule_CASH-HALF)
  (accept_fare_no_passback "ADA/Senior Cash" "ADA/Senior Cash" 75)
  (drop_vault)
)

(define (rule_MISC-INTERLINE)
  (accept_fare_no_passback "Interline" "Interline" 0)
)

(define (rule_MISC-TRANSFER)
  (accept_fare_no_passback "Transfer" "Transfer" 0)
)

(define (rule_MISC-BIKE)
  (accept_fare_no_passback "Bike" "Bike" 0)
)

(define (rule_MISC-WHEELCHAIR)
  (accept_fare_no_passback "Wheelchair" "Wheelchair" 0)
)

(define (rule_MISC-EVENT)
  (accept_fare_no_passback "Special Event" "Special Event" 0)
)

(define (rule_OVR-CITY)
  (accept_fare_no_passback "City Override" "City Override" 0)
)

(define (rule_OVR-CARD)
  (accept_fare_no_passback "ORG-Card Override" "ORG-Card Override" 0)
)

(define (bulk_generic rule times)
  (cond
    ((= times 0) 0)
    (else
      (rule)
      (bulk_generic rule (- times 1))
    )
  )
)

(define (rule_BULK-CASH-ADULT)
  (bulk_generic rule_CASH-ADULT (string->number (param)))
)

(define (rule_BULK-CASH-YOUTH)
  (bulk_generic rule_CASH-YOUTH (string->number (param)))
)

(define (rule_BULK-CASH-HALF)
  (bulk_generic rule_CASH-HALF (string->number (param)))
)

(define (rule_BULK-MISC-EVENT)
  (bulk_generic rule_MISC-EVENT (string->number (param)))
)
(define (rule_BULK-MISC-INTERLINE)
  (bulk_generic rule_MISC-INTERLINE (string->number (param)))
)

(define (rule_BULK-MISC-TRANSFER)
  (bulk_generic rule_MISC-TRANSFER (string->number (param)))
)

(define (rule_BULK-OVR-CITY)
  (bulk_generic rule_OVR-CITY (string->number (param)))
)

(define (rule_BULK-OVR-CARD)
  (bulk_generic rule_OVR-CARD (string->number (param)))
)

;------------------ blanket reject (used for domain card rejections as well)
      
(define (rule_REJECT)
  (tell_rider "No Passes on Card")
  (reject_fare "No Passes on Card" "No Passes on Card" 0)
)
