import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { IonAvatar, IonButton, IonContent, IonHeader, IonIcon, IonItem, IonLabel, IonList } from '@ionic/angular/standalone';
import { addIcons } from 'ionicons';
import { chevronBackOutline } from 'ionicons/icons';
import { CustomHeaderComponent } from 'src/lib/component/custom-header/custom-header.component';
import { CloudObject, CloudQuery, CloudUser } from 'src/lib/ncloud';

addIcons({ chevronBackOutline,});


@Component({
  selector: 'app-attention',
  templateUrl: './attention.component.html',
  styleUrls: ['./attention.component.scss'],
  standalone: true,
  imports: [IonContent,IonList,IonItem,IonAvatar,IonLabel,IonButton,CommonModule,CustomHeaderComponent,IonHeader,IonIcon]
})
export class AttentionComponent  implements OnInit {

  constructor(private router: Router) {
    
   }

  ngOnInit() {
    const navigation = this.router.getCurrentNavigation();
    if (navigation && navigation.extras.state) {
      this.currentTab = navigation.extras.state['currentTab']; // 获取传递的状态
    }
    this.loadAttentionList();
    this.loadFanList();
  }

  attentionList:CloudObject[] | any;
  fanList:CloudObject[] | any;
  currentTab=true; //为true时显示关注列表，为false时显示粉丝列表
  
//加载关注列表
 async loadAttentionList() {
    const query = new CloudQuery('attention');
    const user = new CloudUser().toPointer();
    query.equalTo('UserID', user);
    query.include('attentionID');
    this.attentionList= await query.find();
    console.log(this.attentionList);
  }
//切换关注状态并更新数据库
  async toggleFollow(attention: CloudObject) {
    attention.isFollowed = !attention.isFollowed;
    if(attention.isFollowed) {
      await this.saveAttentionRecord(attention.get('attentionID').objectId);
    }
    else {
      await this.removePostLikeRecord(attention.get('attentionID').objectId);
    }
}

 // 保存关注记录
 async saveAttentionRecord( attentionId:any){
  // 创建新的关注对象
  const attentionRecord = new CloudObject("attention"); 
  const user = new CloudUser();
  const currentUser =  user.current(); // 获取当前用户信息
if (currentUser!=null) {
// 设置关注记录的字段
await attentionRecord.set({
   UserID: new CloudUser().toPointer(), // 指向当前用户
   attentionID: { "__type": "Pointer", "className": "_User", "objectId": attentionId }, // 关注对象指针
});
// 保存关注记录
await attentionRecord.save().then(() => {
  console.log('关注记录已保存:');
})
}
}

// 删除关注记录
async removePostLikeRecord(attentionId:any){
const query = new CloudQuery('attention'); // 创建查询对象
query.equalTo('attentionID', attentionId); // 根据帖子ID查询
query.equalTo('UserID', new CloudUser().toPointer()); // 根据用户ID查询
const attentionRecord = await query.first(); // 获取关注记录对象
//console.log(likeRecord);
if(attentionRecord){
  await attentionRecord.destroy().then(() => {
    console.log('关注记录已删除:');
  })
}

}


//加载粉丝列表
async loadFanList() {
  const query = new CloudQuery('attention');
  const user = new CloudUser().toPointer();
  query.equalTo('attentionID', user);
  query.include('UserID');
  this.fanList= await query.find();
  console.log(this.fanList);

  for(const fan of this.fanList){
    const query = new CloudQuery('attention'); // 创建查询对象
    query.equalTo('attentionID', {objectId: fan.get('UserID').objectId, __type: 'Pointer', className: '_User'}); 
    query.equalTo('UserID', new CloudUser().toPointer()); 
    const attentionRecord = await query.first(); // 获取关注记录对象
    if(attentionRecord){
      fan.isFollowed=true;
    }
    else{
      fan.isFollowed=false;
    }
  }
}

//切换关注状态并更新数据库
async tofanFollow(fan: CloudObject) {
  fan.isFollowed = !fan.isFollowed;
  if(fan.isFollowed) {
    await this.saveAttentionRecord(fan.get('UserID').objectId);
  }
  else {
    await this.removePostLikeRecord(fan.get('UserID').objectId);
  }
}



goBack() {
  window.history.back();
}

tofan(){
  this.currentTab=false;
}

toattention(){
  this.currentTab=true;

}

}
