import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, } from '@angular/forms';
import { IonAccordion, IonAccordionGroup, IonButton, IonButtons, IonContent, IonHeader, IonIcon, IonItem, IonLabel,
   IonList,IonCard ,
   IonTitle, IonToolbar, 
   NavController} from '@ionic/angular/standalone';
import { addIcons } from 'ionicons';
import { close } from 'ionicons/icons';
import { CloudComment, CloudObject, CloudQuery, CloudUser } from 'src/lib/ncloud';


@Component({
  selector: 'app-myposts',
  templateUrl: './myposts.page.html',
  styleUrls: ['./myposts.page.scss'],
  standalone: true,
  imports:[IonContent, IonHeader, IonTitle, IonToolbar, CommonModule,
  IonAccordionGroup, FormsModule,IonItem,IonLabel,IonAccordion,
  IonButton,IonButtons,IonIcon,IonList,FormsModule,IonCard,
]
  
})
export class MypostsPage implements OnInit {
  currentUser:CloudUser|undefined
  userPosts: CloudObject[] = [];
  userComments: CloudObject[] = [];
  private contentStates: { [key: string]: boolean } = {}; // 存储帖子内容展开/收起状态
  postsMap = new Map<string, CloudObject[]>();



  constructor(private navCtrl:NavController) {
    addIcons({close});
    this.currentUser = new CloudUser();
   }


  closeHelp() {
    this.navCtrl.back();
  }


  ngOnInit() {
    this.fetchUserPosts();
    this.fetchUserComments();
  }
  async fetchUserPosts() {
    const query = new CloudQuery('FilmPost');
    query.equalTo('author', this.currentUser?.toPointer());
    this.userPosts = await query.find();
// 初始化内容状态
this.userPosts.forEach(post => {
  this.contentStates[post?.id?.toString() || ''] = true; 
});
  }
   // 切换帖子内容展开/收起状态的函数
 toggleContent(post: CloudObject) {
  const postId = post?.id?.toString() || '';
  this.contentStates[postId] = !this.contentStates[postId];
}

  // 显示帖子完整内容的函数
  showFullContent(post: CloudObject): string {
    const content = post?.get('content') as string;
    const postId = post.id?.toString() || '';
    return this.contentStates[postId] ? `${content.slice(0, 200)}` :content ;
  }
 
  // 判断帖子内容是否已折叠的函数
  isContentCollapsed(post: CloudObject): boolean {
    const postId = post.id?.toString() || '';
    return this.contentStates[postId];
  }

  async fetchUserComments() {
    const query = new CloudQuery('FilmPostComment');
    query.equalTo('user', this.currentUser?.toPointer());
    this.userComments = await query.find();

    // 获取每个评论对应的帖子信息
    for (const comment of this.userComments) {
      const postId = comment?.get('postId');
      if (postId) {
        const postQuery = new CloudQuery('FilmPost');
        postQuery.equalTo('objectId', postId);
        try {
          const results: Array<CloudObject> = await postQuery.find();
          console.log(results);
          this.postsMap.set(postId, results);
        } catch (error) {
          console.error('查询出错:', error);
        }
      }
    }
  }

  getPostByComment(comment: CloudObject): CloudObject | undefined {
    const postId = comment?.get('postId');
    const posts = this.postsMap.get(postId);
    return posts ? posts[0] : undefined; // 假设每个postId对应一个帖子
  }
  }