/**
 * 查询类
 @example
// 查询指定教练，修改价格
    let query = new CloudQuery("Tutor");
    let tutor = await query.get("imkGFr6Ztj");
    console.log('tutor', tutor)
    tutor.set({
        price: 199
    })
    tutor = await tutor.save()

    // 条件查询：价格大于60
    let query2 = new CloudQuery("FoodMenu");
    query2.lessThan("price", 60);
    // query2.greaterThan("price", 20);
    let foodList = await query2.find();
    console.log("foodList", foodList)

    // 条件查询：性别等于女
    let query3 = new CloudQuery("Tutor");
    query3.equalTo("gender", "女");
    let tutorList = await query3.find();
    console.log("tutorList", tutorList);

 */
const { CloudObject } = require("./cloud-object")


class CloudQuery {
    className
    queryParams = {
        where: {}
    }
    constructor(className) {
        this.className = className
    }

    async get(objectId) {
        let res = await fetch(`http://dev.fmode.cn:1337/parse/classes/${this.className}/${objectId}?`, {
            "headers": {
                "accept": "*/*",
                "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
                "x-parse-application-id": "dev"
            },
            "referrer": "http://localhost:4040/",
            "referrerPolicy": "strict-origin-when-cross-origin",
            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "omit"
        });
        let data = await res.json()
        let dataObj = new CloudObject(this.className);
        dataObj.objectId = data.objectId
        dataObj.set(data)
        console.log(data, dataObj)
        return dataObj
    }
    equalTo(key, value) {
        this.queryParams.where[key] = value
    }
    lessThan(key, value) {
        this.queryParams.where[key] = { "$lt": value }
    } g
    greaterThan(key, value) {
        this.queryParams.where[key] = { "$gt": value }
    }
    async find() {
        // where={%22price%22:{%22$gt%22:60}}
        let whereStr = ``
        if (Object.keys(this.queryParams.where)?.length) {
            whereStr = `where=` + JSON.stringify(this.queryParams.where)
        }
        let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?${whereStr}`
        console.log(url)
        let res = await fetch(url, {
            "headers": {
                "accept": "*/*",
                "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
                "if-none-match": "W/\"b2-UccTNjvsEYMR2Waro9KVbWJiRJs\"",
                "x-parse-application-id": "dev"
            },
            "referrer": "http://localhost:4040/",
            "referrerPolicy": "strict-origin-when-cross-origin",
            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "omit"
        });
        let data = await res.json()
        let list = data.results
        list = list.map(item => {
            let dataObj = new CloudObject(this.className);
            dataObj.objectId = item.objectId
            dataObj.set(item)
            return dataObj
        })
        console.log(list)
        return list
    }

}

module.exports.CloudQuery = CloudQuery