import { Component, OnInit } from '@angular/core';
import { NavComponent } from '../../../app/components/nav/nav.component';
import { InfiniteScrollCustomEvent, ToastController } from '@ionic/angular';
import * as Parse from 'parse';
import { ionicStandaloneModules } from '../../ionic-standalone.modules';
import { SharedModule } from '../../shared.module';
import { Router } from '@angular/router';
import { CommonModule, DatePipe } from '@angular/common';
import { AiChatService } from '../../../services/aichart.service';
@Component({
  selector: 'app-gift-log',
  templateUrl: './gift-log.component.html',
  styleUrls: ['./gift-log.component.scss'],
  standalone: true,
  imports: [...ionicStandaloneModules, NavComponent, SharedModule,CommonModule],
  providers: [DatePipe],
})
export class GiftLogComponent implements OnInit {
  active: string = 'from';
  list: Array<Parse.Object> = [];
  disbable: boolean = true;
  constructor(private router: Router, public aiChatServ: AiChatService) {}

  ngOnInit() {
    this.getBrowseHistory();
  }
  segmentChanged(e: any) {
    let { value } = e.detail;
    this.active = value;
    // console.log(this.active);
    this.list = [];
    this.disbable = true;
    this.getBrowseHistory();
  }

  async getBrowseHistory() {
    let uid = Parse.User.current().id;
    let query = new Parse.Query('LoveRender');
    // query.equalTo('toUser', uid);
    query.notEqualTo('isDeleted', true);
    query.equalTo(
      this.active == 'from' ? 'fromUser' : 'toUser',
      Parse.User.current()
    );
    // if (this.active == 'from') {
    //   query.notEqualTo('toUser', Parse.User.current());
    // } else {
    //   query.notEqualTo('fromUser', Parse.User.current());
    // }
    query.descending('createdAt');
    query.limit(20);
    query.skip(this.list.length);
    query.include('fromUser', 'toUser');
    let r = await query.find();
    this.list.push(...r);
    return r;
  }

  async onIonInfinite(ev: any) {
    let result = await this.getBrowseHistory();
    if (result.length == 0) {
      this.disbable = false;
    }
    setTimeout(() => {
      (ev as InfiniteScrollCustomEvent).target.complete();
    }, 500);
  }
  toUrl(id: string) {
    this.router.navigate(['/user/profile/' + id]);
  }
}
