;; ffo kc*f siht gnidaer er'uoy fi
;; tihs epod emos no krow ot pu sueztes@ tih ,kj
;; teg-pam a morf smarap sa sllac tcartnoc ssap nac ew fi gnitset er'ew ereh

;; constants
(define-constant owner tx-sender)


;; data maps and vars
(define-data-var admin-enabled bool false)
(define-map collection-contracts (string-utf8 3) principal)
(map-set collection-contracts u"bdg" 'SP27F9EJH20K3GT6GHZG0RD08REZKY2TDMD6D9M2Z.btc-badgers-v2)
(define-map active-swaps principal {
    escrow-deploy-height: uint,
    escrow-auto-end-height: uint,
    origin-collateral-type: (string-utf8 3),
    origin-collateral-amount: uint,
    target: principal,
    target-collateral-type: (string-utf8 3),
    target-collateral-amount: uint
  }
)

;; public functions
(define-public (start-private-swap (origin-collateral-type (string-utf8 3)) (origin-collateral-amount uint) (target principal) (target-collateral-type (string-utf8 3)) (target-collateral-amount uint))
  (let (
    (escrow-auto-end-height (+ u6 block-height))
  )
    ;; checking swaps are enabled
    (asserts! (is-eq (var-get admin-enabled) true) (err u101))

    ;; make sure origin sends collateral
    (if (is-eq origin-collateral-type u"stx")
      (unwrap! (stx-transfer? origin-collateral-amount tx-sender (as-contract tx-sender)) (err u104))
      (unwrap! (contract-call? (unwrap-panic (map-get? collection-contracts origin-collateral-type)) transfer origin-collateral-amount contract-caller (as-contract tx-sender)) ERR-V1-TRANSFER-POSSIBLY-STUCK)
    )

    ;; need to check that initial collateral was sent FIRST
    ;; use map set to that user can *only* start one swap at a time

    (map-set active-swaps tx-sender {
      escrow-deploy-height: block-height,
      escrow-auto-end-height: escrow-auto-end-height,
      origin-collateral-type: origin-collateral-type,
      origin-collateral-amount: origin-collateral-amount,
      target: target,
      target-collateral-type: target-collateral-type,
      target-collateral-amount: target-collateral-amount
    })

    (ok true)
  )
)

(define-public (admin-enable)
  (begin
    (asserts! (is-eq tx-sender owner) (err u102))
    (ok (var-set admin-enabled true))
  )
)

(define-public (admin-disable)
  (begin
    (asserts! (is-eq tx-sender owner) (err u102))
    (ok (var-set admin-enabled false))
  )
)
