;;; NYC Degens
(impl-trait 'SP2V1F1QA2X5HEMADTY9YGBG6ES3Q1Y95435A329C.nft-trait.nft-trait)

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

;; Constants
(define-constant err-no-more-nfts u300)
(define-constant err-invalid-user u500)
(define-constant err-mint-not-enabled (err u1004))
(define-constant commission-address-master tx-sender)
(define-constant mint-limit u5)

;; Internal variables
(define-data-var last-id uint u0)
(define-data-var commission uint u200)
(define-data-var total-price uint u1000000)
(define-data-var MAIN principal 'SP2V1F1QA2X5HEMADTY9YGBG6ES3Q1Y95435A329C)
(define-data-var artist-address principal 'SP67RWGFXKEYFW3P16M15WR2Z3JV1M17V5FC06Q5)

(define-data-var COMM1 principal 'SP3ZRDNS9NXNVD1P0QNZ6STBC4B6XT0RXHA4F6APV)
(define-data-var COMM2 principal 'SPVDTWPB2AHT63385BT3JENEM68VNFMGWADVDKWD)
(define-data-var COMM3 principal 'SP1P1A6VNW2M05JB7FHG6EBBH1ZAB05XWRZABN1ZG)
(define-data-var ipfs-root (string-ascii 80) "ipfs://QmNMwWZULwpG7AZrLrPwGXMeYspBEAjoamtYSaqxjhTQqz/")
(define-data-var minting-enabled bool true)


;; private functions
(define-private (mint (new-owner principal))
(let ((next-id (+ u1 (var-get last-id)))  
        (count (var-get last-id)))
    (asserts! (is-eq (var-get minting-enabled) true) err-mint-not-enabled)
    (asserts! (< count mint-limit) (err err-no-more-nfts))
    (let
    (
        (total-commission (/ (* (var-get total-price) (var-get commission)) u10000))
        (total-artist (- (var-get total-price) (*  total-commission u3)))
    )
    (if (is-eq tx-sender (var-get artist-address))
        (mint-helper new-owner next-id)
        (if (is-eq tx-sender (var-get MAIN))
            (begin
            (try! (stx-transfer? total-commission tx-sender (var-get COMM1)))
            (try! (stx-transfer? total-commission tx-sender (var-get COMM2)))
            (try! (stx-transfer? total-commission tx-sender (var-get COMM3)))
            (try! (stx-transfer? total-artist tx-sender (var-get artist-address)))
            (mint-helper new-owner next-id))
        )
        
    )
    )   
)
)            

(define-private (mint-helper (new-owner principal) (next-id uint))
    (match (nft-mint? stacksocean-art 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 (gift (address principal))
(mint address)
)

(define-public (claim-three)
(begin
    (try! (mint tx-sender))
    (try! (mint tx-sender))
    (try! (mint tx-sender))
    (ok true)
)
)

(define-public (claim-five)
(begin
    (try! (mint tx-sender))
    (try! (mint tx-sender))
    (try! (mint tx-sender))
    (try! (mint tx-sender))
    (try! (mint tx-sender))
    (ok true)
)
)


(define-public (set-minting-enabled (new-minting-enabled bool))
(if (is-eq tx-sender commission-address-master)
    (begin 
    (var-set minting-enabled new-minting-enabled)
    (ok true)
    )
    (err err-invalid-user)
)
)


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

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

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

(define-public (set-commission-master (new-commission-master uint))
(if (is-eq tx-sender commission-address-master)
    (begin
    (var-set commission-master new-commission-master)
    (ok true)
    )
    (err err-invalid-user)
)
)
    
(define-public (set-commission (new-commission uint))
(if (is-eq tx-sender commission-address-master)
    (begin
    (var-set commission new-commission)
    (ok true)
    )
    (err err-invalid-user)
)
)

(define-public (set-ipfs-root (new-ipfs-root (string-ascii 80)))
(if (is-eq tx-sender commission-address-master)
    (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? stacksocean-art 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? stacksocean-art 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 (concat (var-get ipfs-root) (unwrap-panic (contract-call? .conversion lookup token-id))) ".json")))  
)

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

(define-read-only (get-artist-address)
(ok (var-get artist-address))
)

(define-read-only (get-commission-address)
(ok (var-get commission-address))
)

(define-read-only (get-commission-master)
(ok (var-get commission-master))
)

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

(define-read-only (get-minting-enabled)
(ok (var-get minting-enabled))
)

(define-read-only (get-base-uri)
(ok (var-get ipfs-root))
)