(define-constant contract-creator tx-sender)
(define-constant NYCCOIN_TOKEN 'SP2H8PY27SEZ03MWRKS5XABZYQN17ETGQS3527SA5.newyorkcitycoin-token)

(define-constant ERR-UNAUTHORIZED u1)

(define-data-var price uint u15000)
(define-data-var amountOfNYC uint u0)
(define-data-var POOL_ADDRESS principal 'SP1WBNM0A4ZVY1GVZMXDPMV831BF0BMHFJK9ZXNRW)

(define-public (sell-nyc (amount uint))
    (begin
        (asserts! (is-eq contract-caller POOL_ADDRESS) (err ERR_UNAUTHORIZED))
        
        (try! (transfer-nyc amount contract-caller (as-contract tx-sender)))
        
        (var-set amountOfNYC (+ (var-get amountOfNYC) amount))
        (ok true)
    )
)

(define-public (exit-mia (amount uint))
    (begin
        (asserts! (is-eq contract-caller POOL_ADDRESS) (err ERR_UNAUTHORIZED))
        
        (try! (transfer-nyc amount (as-contract tx-sender) contract-caller))
        
        (var-set amountOfNYC (- (var-get amountOfNYC) amount))
        (ok true)
    )
)

(define-public (buy-nyc (amount uint))
    (begin
        (asserts! (not (is-eq contract-caller POOL_ADDRESS)) (ERR_UNAUTHORIZED))
        
        (try! (stx-transfer? (* amount (var-get price)) contract-caller POOL_ADDRESS))

        (try! (transfer-mia amount (as-contract tx-sender) contract-caller))

        (var-set amountOfNYC (- (var-get amountOfNYC) amount))
        (ok true)

    )
)

(define-public (change-price (newPrice uint))
    (begin
        (asserts! (is-eq contract-caller POOL_ADDRESS) (ERR_UNAUTHORIZED))
        
        (var-set price newPrice)
        (ok true)
    )
)

(define-public (set-pool-address (value principal))
    (if
        (is-eq tx-sender cocntract-creator)
            (ok (var-set POOL_ADDRESS (some value)))
        (err ERR-UNAUTHORIZED)
    )
)

(define-read-only (get-price)
    (ok (var-get price))
)

(define-read-only (get-remaining)
    (ok (var-get amountOfNYC))
)

(define-read-only (get-pool-address)
    (ok (var-get POOL_ADDRESS))
)

(define-private (transfer-nyc (amount uint) (from principal) (to principal))
    (contract-call? 'SP2H8PY27SEZ03MWRKS5XABZYQN17ETGQS3527SA5.newyorkcitycoin-token transfer amount from to none)
)

(define-private (get-balance (user principal))
    (contract-call? 'SP2H8PY27SEZ03MWRKS5XABZYQN17ETGQS3527SA5.newyorkcitycoin-token get-balance user)
)