// tab3.page.ts
import { Component } from '@angular/core';
import { ModalController, NavController } from '@ionic/angular';
import { ModalUserEditComponent } from '../../lib/user/modal-user-edit/modal-user-edit.component';
import { CloudApi, CloudUser } from 'src/lib/ncloud';
import { 
  IonHeader, IonToolbar, IonTitle, IonContent, IonButton, IonIcon, 
  IonCard, IonCardHeader, IonCardTitle, IonCardSubtitle, IonCardContent,
  IonGrid, IonRow, IonCol, IonList, IonItem, IonLabel, IonNote,
  IonBadge, IonAvatar, IonText, IonButtons
} from '@ionic/angular/standalone';
import { addIcons } from 'ionicons';
import { 
  settings, time, heart, create, chatbubbles, 
  shieldCheckmark, documentText, logIn, logOut, personAdd 
} from 'ionicons/icons';

/**
 * 个人中心页面组件
 * @Component 装饰器定义组件元数据
 * @description 提供用户个人中心功能，包括登录/注销、编辑资料、查看收藏和记录等
 */
@Component({
  selector: 'app-tab3',
  templateUrl: 'tab3.page.html',
  styleUrls: ['tab3.page.scss'],
  standalone: false
})
export class Tab3Page {
  /**
   * 当前登录用户
   * @type {CloudUser | undefined}
   */
  currentUser: CloudUser | undefined;

  /**
   * 构造函数
   * @param {NavController} navCtrl 导航控制器
   * @param {ModalController} modalCtrl 模态控制器
   * 
   * @description 
   * 初始化用户实例并添加所需图标
   */
  constructor(private navCtrl: NavController, private modalCtrl: ModalController) { 
    this.currentUser = new CloudUser();
    addIcons({
      settings, time, heart, create, chatbubbles, 
      shieldCheckmark, documentText, logIn, logOut, personAdd
    });
  }

  /**
   * 处理按钮点击动作
   * @param {string} type 动作类型
   * @returns {void}
   * 
   * @example
   * handleAction('feedback'); // 处理意见反馈
   */
  handleAction(type: string): void {
    switch(type) {
      case 'feedback':
        console.log('打开意见反馈');
        break;
      case 'privacy':
        console.log('打开隐私政策');
        break;
      case 'agreement':
        console.log('打开用户协议');
        break;
    }
  }

  /**
   * 用户登录
   * @async
   * @returns {Promise<void>}
   * 
   * @description
   * 使用用户名和密码登录，成功后更新当前用户信息
   */
  async login(): Promise<void> {
    let user: any = new CloudUser();
    user = await this.currentUser?.login("0224989", "123456");
    if(user?.id){
      this.currentUser = user;
    }
  }

  /**
   * 用户注销
   * @returns {void}
   * 
   * @description
   * 注销当前用户并清空用户信息
   */
  logout(): void {
    this.currentUser?.logout();
    this.currentUser = undefined;
  }

  /**
   * 打开用户编辑模态框
   * @async
   * @returns {Promise<void>}
   */
  async edit(): Promise<void> {
    const modal = await this.modalCtrl.create({
      component: ModalUserEditComponent,
    });
    modal.present();
    const {data, role} = await modal.onWillDismiss();
  }

  /**
   * 导航到记录页面
   * @returns {void}
   */
  goToRecords(): void {
    this.navCtrl.navigateForward(["tabs","tab3","page-records"]);
    console.log('Navigating to page-records');
  }

  /**
   * 导航到收藏页面
   * @returns {void}
   */
  goToCollections(): void {
    this.navCtrl.navigateForward(["tabs","tab3","page-collections"]);
    console.log('Navigating to page-collections');
  }

  /**
   * 导航到食谱详情页面
   * @param {any} recipeId 食谱ID
   * @returns {void}
   */
  goToDetail(recipeId: any): void {
    this.navCtrl.navigateForward(`/tabs/tab1/page-detail/${recipeId}`);
    console.log('Navigating to page-detail');
  }
}