class CloudObject {
    id
    className
    data = {}
    constructor(className) {
        this.className = className
    }
    toPointer() {
        return { "__type": "Pointer", "className": this.className, "objectId": this.id }
    }
    set(json) {
        Object.keys(json).forEach(key => {
            if (["objectId", "id", "createdAt", "updatedAt", "ACL"].indexOf(key) > -1) {
                return
            }
            this.data[key] = json[key]
        })
    }
    get(key) {
        return this.data[key] || null
    }
    async save() {
        let method = "POST"
        let url = "http://dev.fmode.cn:1337/parse/classes/" + this.className
        // 更新
        if (this.id) {
            url += "/" + this.id
            method = "PUT"
        }
        let body = JSON.stringify(this.data)
        let response = await fetch(url, {
            "headers": {
                "content-type": "application/json;charset=UTF-8",
                "x-parse-application-id": "dev"
            },
            "body": body,
            "method": method,
            "mode": "cors",
            "credentials": "omit"
        });
        let result = await response?.json();
        if (result?.error) {
            console.error(result?.error)
        }
        if (result?.objectId) { this.id = result?.objectId }
        return this
    }
    async destory() {
        if (!this.id) return
        let response = await fetch("http://dev.fmode.cn:1337/parse/classes/Doctor/" + this.id, {
            "headers": {
                "x-parse-application-id": "dev"
            },
            "body": null,
            "method": "DELETE",
            "mode": "cors",
            "credentials": "omit"
        });
        let result = await response?.json();
        if (result) {
            this.id = null
        }
        return true
    }
}

class CloudQuery {
    className
    constructor(className) {
        this.className = className
    }

    whereOptions = {}
    greaterThan(key, value) {
        if (!this.whereOptions[key]) this.whereOptions[key] = {}
        this.whereOptions[key]["$gt"] = value
    }
    greaterThanAndEqualTo(key, value) {
        if (!this.whereOptions[key]) this.whereOptions[key] = {}
        this.whereOptions[key]["$gte"] = value
    }
    lessThan(key, value) {
        if (!this.whereOptions[key]) this.whereOptions[key] = {}
        this.whereOptions[key]["$lt"] = value
    }
    lessThanAndEqualTo(key, value) {
        if (!this.whereOptions[key]) this.whereOptions[key] = {}
        this.whereOptions[key]["$lte"] = value
    }
    equalTo(key, value) {
        this.whereOptions[key] = value
    }

    async get(id) {
        let url = "http://dev.fmode.cn:1337/parse/classes/" + this.className + "/" + id + "?"

        let response = await fetch(url, {
            "headers": {
                "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
                "x-parse-application-id": "dev"
            },
            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "omit"
        });
        let json = await response?.json();
        return json || {}
    }
    async find() {
        let url = "http://dev.fmode.cn:1337/parse/classes/" + this.className + "?"

        if (Object.keys(this.whereOptions)?.length) {
            let whereStr = JSON.stringify(this.whereOptions)
            url += `where=${whereStr}`
        }

        let response = await fetch(url, {
            "headers": {
                "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
                "x-parse-application-id": "dev"
            },
            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "omit"
        });
        let json = await response?.json();
        return json?.results || []
    }
    async first() {
        let url = "http://dev.fmode.cn:1337/parse/classes/" + this.className + "?"

        if (Object.keys(this.whereOptions)?.length) {
            let whereStr = JSON.stringify(this.whereOptions)
            url += `where=${whereStr}`
        }

        let response = await fetch(url, {
            "headers": {
                "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
                "x-parse-application-id": "dev"
            },
            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "omit"
        });
        let json = await response?.json();
        let exists = json?.results?.[0] || null
        if (exists) {
            let existsObject = new CloudObject(this.className)
            existsObject.set(exists)
            existsObject.id = exists.objectId
            existsObject.createdAt = exists.createdAt
            existsObject.updatedAt = exists.updatedAt
            return existsObject
        }
    }
}

module.exports.CloudObject = CloudObject
module.exports.CloudQuery = CloudQuery