import { Component, OnInit } from '@angular/core';
import { IonicModule } from '@ionic/angular';
import { IonButton, IonCheckbox, IonContent, IonIcon, IonInput, ModalController } from '@ionic/angular/standalone';
import { CloudUser } from 'src/lib/ncloud';
import { addIcons } from 'ionicons';
import { arrowBackOutline, logoWechat, personOutline, phonePortraitOutline } from 'ionicons/icons';
import { openUserLoginMainModal } from '../modal-user-login-main/modal-user-login-main.component';
addIcons({ arrowBackOutline, logoWechat, phonePortraitOutline, personOutline });
@Component({
  selector: 'app-modal-user-login',
  templateUrl: './modal-user-login.component.html',
  styleUrls: ['./modal-user-login.component.scss'],
  standalone: true,
  imports: [IonButton, IonIcon, IonInput, IonContent, IonCheckbox],
})
export class ModalUserLoginComponent implements OnInit {
  constructor(private modalCtrl: ModalController) { }
  ngOnInit() { }

  goBack() {
    this.modalCtrl.dismiss(null, "cancel");
  }
  /**
   * @跳转登录方式功能函数
   */
  async goPhoneSignPage() {
    const user: CloudUser | null = await openUserLoginMainModal(this.modalCtrl, 'phone');
    if (user?.id) {
      this.modalCtrl.dismiss(user, "confirm"); // 关闭主模态并返回用户
    } else {
      this.modalCtrl.dismiss(null, "cancel"); // 关闭主模态
    }
  }

  async goUsernameLoginPage() {
    console.warn("-------------------------");
    console.warn("即将打开loginMain模态");
    const user: CloudUser | null = await openUserLoginMainModal(this.modalCtrl, 'username');
    console.log("已接受来自loginMain的返回数据，如下：");
    console.log(user);
    if (user?.id) {
      console.log("即将关闭loginMain模态并返回用户");
      setTimeout(async () => {
        await this.modalCtrl.dismiss(user, "confirm");

      }, 500); // 延迟500毫秒
    } else {
      await this.modalCtrl.dismiss(null, "cancel"); // 关闭主模态
    }
    console.warn("模态已关闭，结束login模态流程");
    console.warn("-------------------------")
  }
}

export async function openUserLoginModal(modalCtrl: ModalController): Promise<CloudUser | null> {
  const modal = await modalCtrl.create({
    component: ModalUserLoginComponent,
  });
  await modal.present();

  const { data, role } = await modal.onWillDismiss();

  if (role === 'confirm') {
    return data;
  }
  return null;
}