;; magic-ape-school
(impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)

(define-non-fungible-token magic-ape-school uint)

;; Constants
(define-constant err-mint u100)
(define-constant err-mint-not-active u200)
(define-constant err-no-more-nfts u300)
(define-constant err-invalid-user u500)
(define-constant CONTRACT-OWNER tx-sender)

(define-data-var commission-address principal 'SPNWZ5V2TPWGQGVDR6T7B6RQ4XMGZ4PXTEE0VQ0S)
(define-data-var artist-address principal 'SPDGER90F6BQCKN0R34SYJPKQJ3C8YCEES5E8CQ0)

;; Internal variables
(define-data-var mint-limit uint u2500)
(define-data-var commission uint u1000)
(define-data-var last-id uint u201)
(define-data-var mint-price uint u50000000)
(define-data-var mint-price-mia uint u10000)
(define-data-var mint-price-nyc uint u5400)
(define-data-var the-mint principal tx-sender)
(define-data-var ipfs-full (string-ascii 84) "ipfs://QmZBfZm1L24qDG7h1xXSkRaeq6GNYKcVs3CDm5s5PMjXhk/magic_ape_school_metadata.json")
(define-data-var ipfs-root (string-ascii 80) "ipfs://QmZBfZm1L24qDG7h1xXSkRaeq6GNYKcVs3CDm5s5PMjXhk/")
(define-data-var ipfs-change-enabled bool true)
(define-data-var mint-status bool true)

;;STX Minter
(define-private (mint-many (orders (list 10 bool)) (currency (string-ascii 3)))
  (let (
      (last-nft-id (var-get last-id))
      (enabled (asserts! (<= last-nft-id (var-get mint-limit)) (err err-no-more-nfts)))
      (id-reached (fold mint-many-iter orders last-nft-id))
      (price (* (var-get mint-price) (- id-reached last-nft-id)))
      (price-mia (* (var-get mint-price-mia) (- id-reached last-nft-id)))
      (price-nyc (* (var-get mint-price-nyc) (- id-reached last-nft-id)))
      (payout (- price (/ (* price (var-get commission)) u10000)))
      (payout-mia (- price-mia (/ (* price-mia (var-get commission)) u10000)))
      (payout-nyc (- price-nyc (/ (* price-nyc (var-get commission)) u10000)))
      (commission1 (/ (* price (/ (var-get commission) u2)) u10000))
      (commission2 (/ (* price (/ (var-get commission) u2)) u10000))
      (commission1-mia (/ (* price-mia (/ (var-get commission) u2)) u10000))
      (commission2-mia (/ (* price-mia (/ (var-get commission) u2)) u10000))
      (commission1-nyc (/ (* price-nyc (/ (var-get commission) u2)) u10000))
      (commission2-nyc (/ (* price-nyc (/ (var-get commission) u2)) u10000))
    )
      (begin
        (var-set last-id id-reached)
        (if (is-eq currency "stx")
            (begin
                (print "mint in stx")
                (try! (stx-transfer? commission1 tx-sender CONTRACT-OWNER))
                (try! (stx-transfer? commission2 tx-sender (var-get commission-address)))
                (try! (stx-transfer? payout tx-sender (var-get artist-address)))
            )
            (if (is-eq currency "mia")
                (begin
                    (print "mint in mia")
                    (try! (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer payout-mia tx-sender (var-get artist-address) (some 0x00)))
                    (try! (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer commission1-mia tx-sender CONTRACT-OWNER (some 0x00)))
                    (try! (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer commission2-mia tx-sender (var-get commission-address) (some 0x00)))
                )
                (begin
                    (print "mint in nyc")
                    (try! (contract-call? 'SP2H8PY27SEZ03MWRKS5XABZYQN17ETGQS3527SA5.newyorkcitycoin-token transfer payout-nyc tx-sender (var-get artist-address) (some 0x00)))
                    (try! (contract-call? 'SP2H8PY27SEZ03MWRKS5XABZYQN17ETGQS3527SA5.newyorkcitycoin-token transfer commission1-nyc tx-sender CONTRACT-OWNER (some 0x00)))
                    (try! (contract-call? 'SP2H8PY27SEZ03MWRKS5XABZYQN17ETGQS3527SA5.newyorkcitycoin-token transfer commission2-nyc tx-sender (var-get commission-address) (some 0x00)))
                )
            )
        )
      )
    (ok id-reached)
  )
)

(define-private (mint-many-iter (ignore bool) (next-id uint))
  (if (<= next-id (var-get mint-limit))
    (begin
      (unwrap! (nft-mint? magic-ape-school next-id tx-sender) next-id)
      (+ next-id u1)
    )
    next-id
  )
)

(define-public (mint (orders (list 10 bool)) (currency (string-ascii 3)))
  (begin
    (asserts! (is-eq (var-get mint-status) true) (err err-mint-not-active))
    (asserts! (or (is-eq contract-caller (var-get the-mint)) (is-eq tx-sender CONTRACT-OWNER)) (err err-invalid-user))
    (mint-many orders currency)
  )
)

;; Allows contract owner to change commission address if need be
(define-public (set-commission-address (value principal))
  (if (is-eq tx-sender CONTRACT-OWNER)
    (ok (var-set commission-address value))
    (err err-invalid-user)
  )
)

;; Allows contract owner to change artist address if need be
(define-public (set-artist-address (value principal))
  (if (is-eq tx-sender CONTRACT-OWNER)
    (ok (var-set artist-address value))
    (err err-invalid-user)
  )
)

(define-public (set-commission (amount uint))
  (begin
    (asserts! (is-eq tx-sender CONTRACT-OWNER) (err err-invalid-user))
    (ok (var-set commission amount))
  )
)

(define-public (set-price (price uint))
  (begin
    (asserts! (is-eq tx-sender CONTRACT-OWNER) (err err-invalid-user))
    (ok (var-set mint-price price))
  )
)

(define-public (set-mia-price (price uint))
  (begin
    (asserts! (is-eq tx-sender CONTRACT-OWNER) (err err-invalid-user))
    (ok (var-set mint-price-mia price))
  )
)

(define-public (set-nyc-price (price uint))
  (begin
    (asserts! (is-eq tx-sender CONTRACT-OWNER) (err err-invalid-user))
    (ok (var-set mint-price-nyc price))
  )
)

(define-public (set-ipfs-full (new-ipfs-full (string-ascii 84)))
  (begin
    (asserts! (and (var-get ipfs-change-enabled) (is-eq tx-sender CONTRACT-OWNER)) (err err-invalid-user))
    (ok (var-set ipfs-full new-ipfs-full))
  )
)

(define-public (set-ipfs-root (new-ipfs-root (string-ascii 80)))
  (begin
    (asserts! (and (var-get ipfs-change-enabled) (is-eq tx-sender CONTRACT-OWNER)) (err err-invalid-user))
    (ok (var-set ipfs-root new-ipfs-root))
  )
)

(define-public (freeze-ipfs-root)
  (begin
    (asserts! (and (var-get ipfs-change-enabled) (is-eq tx-sender CONTRACT-OWNER)) (err err-invalid-user))
    (ok (var-set ipfs-change-enabled false))
  )
)

(define-public (set-mint-status (status bool))
  (begin
    (asserts! (is-eq tx-sender CONTRACT-OWNER) (err err-invalid-user))
    (ok (var-set mint-status status))
  )
)

(define-public (set-mint-addr (mint-addr principal) (sender principal))
  (begin
    (asserts! (is-eq sender CONTRACT-OWNER) (err err-invalid-user))
    (asserts! (is-eq tx-sender (var-get the-mint)) (err err-invalid-user))
    (ok (var-set the-mint mint-addr))
  )
)

(define-public (transfer (token-id uint) (sender principal) (recipient principal))
  (begin
    (asserts! (is-eq tx-sender sender) (err err-invalid-user))
    (nft-transfer? magic-ape-school token-id sender recipient)
  )
)

;; read-only functions
(define-read-only (get-mint-price)
  (ok (var-get mint-price)))

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

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

(define-read-only (get-owner (token-id uint))
  (ok (nft-get-owner? magic-ape-school token-id)))

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

(define-read-only (get-token-uri (token-id uint))
  (if (< token-id u5001)
    (ok (some (concat (concat (var-get ipfs-root) (unwrap-panic (contract-call? .conversion lookup token-id))) ".json")))
    (ok (some (concat (concat (var-get ipfs-root) (unwrap-panic (contract-call? .conversion-v2 lookup (- token-id u5001)))) ".json")))
    )
)

(begin
    (try! (nft-mint? magic-ape-school u1 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u2 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u3 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u4 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u5 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u6 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u7 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u8 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u9 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u10 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u11 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u12 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u13 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u14 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u15 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u16 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u17 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u18 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u19 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u20 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u21 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u22 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u23 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u24 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u25 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u26 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u27 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u28 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u29 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u30 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u31 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u32 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u33 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u34 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u35 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u36 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u37 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u38 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u39 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u40 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u41 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u42 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u43 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u44 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u45 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u46 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u47 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u48 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u49 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u50 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u51 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u52 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u53 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u54 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u55 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u56 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u57 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u58 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u59 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u60 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u61 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u62 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u63 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u64 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u65 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u66 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u67 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u68 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u69 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u70 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u71 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u72 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u73 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u74 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u75 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u76 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u77 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u78 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u79 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u80 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u81 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u82 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u83 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u84 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u85 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u86 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u87 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u88 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u89 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u90 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u91 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u92 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u93 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u94 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u95 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u96 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u97 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u98 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u99 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u100 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u101 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u102 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u103 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u104 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u105 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u106 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u107 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u108 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u109 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u110 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u111 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u112 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u113 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u114 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u115 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u116 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u117 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u118 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u119 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u120 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u121 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u122 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u123 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u124 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u125 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u126 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u127 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u128 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u129 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u130 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u131 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u132 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u133 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u134 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u135 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u136 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u137 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u138 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u139 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u140 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u141 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u142 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u143 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u144 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u145 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u146 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u147 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u148 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u149 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u150 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u151 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u152 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u153 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u154 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u155 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u156 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u157 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u158 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u159 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u160 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u161 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u162 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u163 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u164 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u165 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u166 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u167 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u168 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u169 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u170 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u171 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u172 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u173 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u174 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u175 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u176 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u177 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u178 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u179 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u180 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u181 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u182 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u183 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u184 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u185 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u186 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u187 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u188 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u189 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u190 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u191 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u192 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u193 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u194 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u195 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u196 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u197 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u198 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u199 (var-get artist-address)))
    (try! (nft-mint? magic-ape-school u200 (var-get artist-address)))
    (ok true)
)