import { Component } from '@angular/core';

import Parse from "parse"
Parse.initialize("dev");
Parse.serverURL="http://web2023.fmode.cn:9999/parse";

export class ParseObject{
  className
  id:any=""
  serverURL = "http://web2023.fmode.cn:9999/parse"
  data = {}
  toJSON(){
      return this.data
  }
  constructor(className:any){
      this.className = className
  }
  async get(id:any){
    console.log("Objectid:",this.id)
      let response = await fetch(this.serverURL+"/classes/"+this.className+"/"+id, {
          "headers": {
              "x-parse-application-id": "dev"
            },
          "body": null,
          "method": "GET",
          "mode": "cors",
          "credentials": "omit"
        });
        if (typeof response?.status === "number" && response?.status === 200) {
          let json = await response.json()
          this.id = json?.objectId;
          delete json.objectId
          delete json.createdAt
          delete json.updatedAt
          this.data = json;
          return this
        }else{
          return []
        }
  }
  async findAll(){
      let response = await fetch(this.serverURL+"/classes/"+this.className, {
          "headers": {
              "x-parse-application-id": "dev"
            },
          "body": null,
          "method": "GET",
          "mode": "cors",
          "credentials": "omit"
        });
        // console.log(response)
        // return []
        if (typeof response?.status === "number" && response?.status === 200) {
          let json = await response.json()
          // console.log(json)
          return json?.results || []
        }else{
          return []
        }
  }
  set(data:any){
      this.data = data
  }
  async save(): Promise<string> {
    let body = JSON.stringify(this.data);
    let url = this.serverURL + "/classes/" + this.className; // 创建URL
    let method = "POST";
  
    console.log(url, method, body);
    try {
      let response = await fetch(url, {
        headers: {
          "Content-Type": "application/json", // 设置正确的Content-Type
          "X-Parse-Application-Id": "dev"
        },
        body: body,
        method: method,
        mode: "cors",
        credentials: "omit"
      });
  
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }
  
      let text = await response.text();
      let json = JSON.parse(text);
  
      if (json.objectId) {
        console.log(json);
        this.id = json.objectId;
        console.log("ObjectId:", this.id);
        return this.id;
      } else {
        throw new Error("No objectId returned from server");
      }
    } catch (error) {
      console.error("保存失败：", error);
      throw error; // 抛出错误，以便调用者可以捕获它
    }
  }


  async save2(id:any){
    console.log("Objectid3:",id)
    let body = JSON.stringify(this.data)
    let url = this.serverURL+"/classes/"+this.className+"/"+id  // 创建URL
    let method = "PUT"
    console.log(url,method,body,"11111111111111111",id)
    let response = await fetch(url, {
        "headers": {
            // "content-type": "text/plain;charset=UTF-8",
            "x-parse-application-id": "dev"
        },
        "body": body,
        "method": method,
        "mode": "cors",
        "credentials": "omit"
    });
    console.log(body)
    let text = await response?.text();
    console.log(text)
    let json = await response?.json();
    if(json?.objectId){
        console.log(json)
        this.id = json?.objectId
        return this
    }else{
        return null
    }
}

async saveChat(id:any){
  console.log("Objectid3:",id)
  let body = JSON.stringify(this.data)
  let url = this.serverURL+"/classes/"+this.className+"/"+id  // 创建URL
  let method = "PUT"
  console.log(url,method,body,"11111111111111111",id)
  let response = await fetch(url, {
      "headers": {
          // "content-type": "text/plain;charset=UTF-8",
          "x-parse-application-id": "dev"
      },
      "body": body,
      "method": method,
      "mode": "cors",
      "credentials": "omit"
  });
  console.log(body)
  let text = await response?.text();
  console.log(text)
  let json = await response?.json();
  if(json?.objectId){
      console.log(json)
     const foreheadHeight= document.getElementById('foreheadHeight');   
     if (foreheadHeight) foreheadHeight.textContent = json.面部特征分析.分析;
      
       console.log("66666666666666",json.面部特征分析.形象设计建议)  
        //  json.五眼比例.分析
        //  json.五眼比例.形象设计建议
        //  json.脸型.分析
        //  json.脸型.形象设计建议
        //  json.眉形.分析
        //  json.眉形.形象设计建议
        //  json.唇形.分析
        //  json.唇形.形象设计建议
        //  json.眼睛形状.分析
        //  json.眼睛形状.形象设计建议
        //  json.鼻形.分析
        //  json.鼻形.形象设计建议
        //  json.颧骨.分析
        //  json.颧骨.形象设计建议
        //  json.下巴.分析
        //  json.下巴.形象设计建议
        //  json.额头.分析
        //  json.额头.形象设计建议
        //  json.脸颊.分析
        //  json.脸颊.形象设计建议
      this.id = json?.objectId
      return this
  }else{
      return null
  }
}



  async delete(id:any){
      let response = await fetch(this.serverURL+"/classes/"+this.className+"/"+id, {
          "headers": {
              "x-parse-application-id": "dev"
            },
          "body": null,
          "method": "DELETE",
          "mode": "cors",
          "credentials": "omit"
        });
        if (typeof response?.status === "number" && response?.status === 200) {
          let json = await response.json()
          return json
        }else{
          return []
        }
  }

  
}