;; https://explorer.stacks.co/txid/0x80eb693e5e2a9928094792080b7f6d69d66ea9cc881bc465e8d9c5c621bd4d07?chain=mainnet
(impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)


;; Non Fungible Token, using sip-009
(define-non-fungible-token Candy uint)

;; constants
(define-constant err-no-more-nfts u300)
(define-constant err-invalid-user u500)
(define-constant mint-limit u2400)
(define-constant contract-owner tx-sender)

;; data maps and vars
(define-data-var last-id uint u0)
(define-data-var total-price uint u0)
(define-data-var artist-address principal 'SP19H1D5DV317DR2196ADZNN4P83H9KP5GA1PPEW5)
(define-data-var ipfs-root (string-ascii 256) "https://gateway.pinata.cloud/ipfs/QmTihwJveE2mrEcJHEhQ9ckZeWXK2vYznkJfBttVVyP6Nb/")

;; private functions
;;
(define-private (mint (new-owner principal))
    (let ((next-id (+ u1 (var-get last-id))))
      (match (nft-mint? Candy next-id new-owner)
        success
          (begin
            (var-set last-id next-id)
            (ok true))
        error (err error))))

;; public functions
;;
(define-public (claim)
  (mint tx-sender))

(define-public (tip-artist (amount uint))
    (stx-transfer? amount tx-sender (var-get artist-address))
)

(define-public (tip-dev (amount uint))
   (stx-transfer? amount tx-sender contract-owner)
)

(define-public (set-artist-address (address principal))
  (if (is-eq tx-sender contract-owner)
    (begin 
      (var-set artist-address address)
      (ok true)
    )
    (err err-invalid-user)))

(define-public (set-price (price uint))
  (if (is-eq tx-sender contract-owner)
    (begin 
      (var-set total-price price)
      (ok true)
    )
    (err err-invalid-user)))

(define-public (set-ipfs-root (new-ipfs-root (string-ascii 256)))
  (if (is-eq tx-sender contract-owner)
    (begin 
      (var-set ipfs-root new-ipfs-root)
      (ok true)
    )
    (err err-invalid-user)))

(define-public (transfer (token-id uint) (sender principal) (recipient principal))
  (if (and
        (is-eq tx-sender sender))
      (match (nft-transfer? Candy token-id sender recipient)
        success (ok success)
        error (err error))
      (err err-invalid-user)))

;; read-only functions
(define-read-only (get-owner (token-id uint))
  (ok (nft-get-owner? Candy token-id)))

(define-read-only (get-last-token-id)
  (ok (var-get last-id)))

(define-read-only (get-token-uri (token-id uint))
    (ok (some  (concat (var-get ipfs-root) "$TOKEN_ID"))))