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 = "https://dev.fmode.cn/parse" + 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?.objectId){this.id = result?.objectId}
      return this
    }
    // 删除
    async destory(){
        if(!this.id) return
        let response = await fetch("https://dev.fmode.cn/parse"+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 = {}
    equalTo(key,value){
        this.whereOptions[key] = value
    }
    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] = {}
    }
    async get(id){ // 通过id查询
        let url = "https://dev.fmode.cn/parse"+this.className+"/"+id+"?"
        let response = await fetch(url, {
            "headers": {
              "if-none-match": "W/\"22c-NGsQZp5SnqjXTAX+NXjLAv6LaLA\"",
              "x-parse-application-id": "dev"
            },
            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "omit"
          });
          let json = await response?.json();
          return json || {}
    }
    async find(){ // 直接查询
        let url = "https://dev.fmode.cn/parse"+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/\"22c-NGsQZp5SnqjXTAX+NXjLAv6LaLA\"",
              "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 = "https://dev.fmode.cn/parse"+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/\"22c-NGsQZp5SnqjXTAX+NXjLAv6LaLA\"",
              "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.updateAt = exists.updateAt
            return existsObject
          }
    }
}
module.exports.CloudObject = CloudObject
module.exports.CloudQuery = CloudQuery