import { Injectable } from '@angular/core';
import * as Parse from 'parse';


@Injectable({
  providedIn: 'root'
})
export class WyContactService {

  constructor() {
    const Parse = require("parse/node")
Parse.serverURL = "http://web2023.fmode.cn:9999/parse" // 配置服务器地址
Parse.initialize("dev") // 配置应用名称
  }

  async getContacts(): Promise<any[]> {
    const WyContact = Parse.Object.extend('WyContact');
    const query = new Parse.Query(WyContact);
    try {
      const results = await query.find();
      return results.map(result => ({
        id: result.id,
        name: result.get('name'),
        avatar: result.get('avatar'), // Assuming there's an 'avatar' field in WyContact schema
        lastMessageTime: result.get('lastMessageTime'), // Assuming there's a 'lastMessageTime' field
        lastMessage: result.get('lastMessage') // Assuming there's a 'lastMessage' field
      }));
    } catch (error) {
      console.error('Error fetching contacts', error);
      return [];
    }
  }

  async searchContacts(searchTerm: string): Promise<any[]> {
    const WyContact = Parse.Object.extend('WyContact');
    const query = new Parse.Query(WyContact);
    query.contains('name', searchTerm);
    try {
      const results = await query.find();
      return results.map(result => ({
        id: result.id,
        name: result.get('name'),
        avatar: result.get('avatar'), // Assuming there's an 'avatar' field in WyContact schema
        lastMessageTime: result.get('lastMessageTime'), // Assuming there's a 'lastMessageTime' field
        lastMessage: result.get('lastMessage') // Assuming there's a 'lastMessage' field
      }));
    } catch (error) {
      console.error('Error searching contacts', error);
      return [];
    }
  }
}
