import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import {
  ionicStandaloneModules,
  LoadingController,
} from '../../ionic-standalone.modules';
import * as Parse from 'parse';
import { AiChatService } from '../../../services/aichart.service';
import { InfiniteScrollCustomEvent } from '@ionic/core';
import { NavComponent } from '../../../app/components/nav/nav.component';

@Component({
  selector: 'app-comment',
  templateUrl: './comment.component.html',
  styleUrls: ['./comment.component.scss'],
  standalone: true,
  imports: [...ionicStandaloneModules, NavComponent],
})
export class CommentComponent implements OnInit {
  rid: string = '';
  commentList: Array<Parse.Object> = [];

  constructor(
    private activateRoute: ActivatedRoute,
    private loadingCtrl: LoadingController,
    public aiChatServ: AiChatService
  ) {}

  ngOnInit() {
    this.activateRoute.paramMap.subscribe(async (params) => {
      let rid = params.get('rid');
      this.rid = rid;
      this.getCommentLeng();
    });
  }

  async getCommentLeng() {
    const loading = await this.loadingCtrl.create({
      message: '加载中',
    });
    loading.present();
    let queryPost = new Parse.Query('DramaPostLog');
    queryPost.equalTo('room', this.rid);
    queryPost.notEqualTo('isDeleted', true);
    queryPost.include('user');
    queryPost.descending('createdAt');
    queryPost.equalTo('isVerify', true);
    queryPost.limit(20);
    queryPost.skip(this.commentList.length);
    let r = await queryPost.find();
    this.commentList.push(...r);
    loading.dismiss();
    return r;
  }
  async onIonInfinite(ev: any) {
    let result = await this.getCommentLeng();
    if (result.length == 0) {
      (ev as InfiniteScrollCustomEvent).target.disabled = true;
    }
    setTimeout(() => {
      (ev as InfiniteScrollCustomEvent).target.complete();
    }, 500);
  }
}
