import { Component } from '@angular/core';
import { NavController } from '@ionic/angular';
import { Router } from '@angular/router';
import Parse from "parse";
@Component({
  selector: 'app-session',
  templateUrl: 'session.page.html',
  styleUrls: ['session.page.scss']
})
export class SessionPage {
  skeletonList: { username: string, avatar: string, lastMessageTime: string, lastMessage: string }[] = [
    {
      username: 'Alice',
      avatar: '/assets/img/female.png',
      lastMessageTime: '10:30 AM',
      lastMessage: 'Hey, how are you?',
    },
    {
      username: 'Bob',
      avatar: '/assets/img/male.png',
      lastMessageTime: 'Yesterday',
      lastMessage: 'Let\'s meet tomorrow.',
    },
  ];

  constructor(private navCtrl: NavController) {
    this.loadSessions()
  }

  targetUser(session:Parse.Object){
    let user = Parse.User.current();
    let target = null
    if(session?.get("sendUser")?.id==user?.id){
      target = session?.get("receiveUser")
    }
    if(session?.get("receiveUser")?.id==user?.id){
      target = session?.get("sendUser")
    }
    return target
  }
  goSession(session:Parse.Object){
    let user = Parse.User.current();
    if(session?.get("sendUser")?.id==user?.id){
      this.navCtrl.navigateForward(`/contact/chat/${session?.get("receiveUser")?.id}`);
    }
    if(session?.get("receiveUser")?.id==user?.id){
      this.navCtrl.navigateForward(`/contact/chat/${session?.get("sendUser")?.id}`);
    }
  }
  sessionList:Parse.Object[] = []
  async loadSessions(){
    console.log("loadSessions")
    let user = Parse.User.current();
    let query = Parse.Query.fromJSON('MessageSession',{where: {
      $or: [
        {
          sendUser: user?.id,
        },
        {
          receiveUser: user?.id
        }
      ]
    }})
    query.include("sendUser","receiveUser");
    this.sessionList = await query.find();
  }


}