import { Component, OnInit } from '@angular/core';
import { ModalController, ToastController, IonInput, IonItem, IonLabel, IonButton, IonCard, IonCardHeader, IonCardTitle, IonCardSubtitle, IonCardContent } from '@ionic/angular/standalone';
import { CloudObject } from 'src/lib/ncloud';
import { FormsModule } from '@angular/forms'; // 导入 FormsModule
import { CommonModule } from '@angular/common';
import { UserService } from 'src/app/user.service'; // 假设这是你的用户服务

@Component({
  selector: 'app-throw-drift-bottle',
  templateUrl: './throw-drift-bottle.component.html',
  styleUrls: ['./throw-drift-bottle.component.scss'],
  standalone: true,
  imports: [
    IonCard,
    IonCardHeader,
    IonCardTitle,
    IonCardSubtitle,
    IonCardContent,
    IonButton,
    IonInput,
    IonItem,
    IonLabel,
    FormsModule,
    CommonModule
  ],
})
export class ThrowDriftBottleComponent implements OnInit {
  driftbottleData: { content: string, username: string, status: string, throwtime: Date } = { 
    content: '', 
    username: '', 
    status: 'drifting', 
    throwtime: new Date() // 初始化为当前时间
  }; // 漂流瓶内容、用户名、状态和投掷时间

  constructor(
    private modalCtrl: ModalController,
    private userService: UserService, // 注入用户服务
    private toastCtrl: ToastController // 注入 ToastController
  ) {}

  ngOnInit() {
    const username = this.userService.getUsername(); // 获取当前用户的用户名
    if (username) {
      this.driftbottleData.username = username;
    } else {
      console.error('用户未登录或未找到用户名');
    }
  }

  async throw() {
    // 更新投掷时间为当前时间
    this.driftbottleData.throwtime = new Date();

    console.log('投掷漂流瓶:');
    console.log(`内容: ${this.driftbottleData.content}`);
    console.log(`用户名: ${this.driftbottleData.username}`);
    console.log(`状态: ${this.driftbottleData.status}`);
    console.log(`投掷时间: ${this.driftbottleData.throwtime}`);

    const driftbottleData = {
      content: this.driftbottleData.content,
      username: this.driftbottleData.username,
      status: this.driftbottleData.status,
      throwtime: this.driftbottleData.throwtime, // 直接使用 Date 对象
    };

    let driftbottle = new CloudObject("Driftbottle");
    driftbottle.set(driftbottleData);
    try {
      await driftbottle.save();
      console.log('漂流瓶已成功投掷');

      // 显示成功的 toast 提示
      const toast = await this.toastCtrl.create({
        message: '扔出成功！',
        duration: 2000,
        position: 'middle'
      });
      toast.present();

      this.modalCtrl.dismiss(driftbottle, "confirm");
    } catch (err) {
      console.error('投掷漂流瓶时出错', err);

      // 显示错误的 toast 提示
      const toast = await this.toastCtrl.create({
        message: '扔出失败，请重试。',
        duration: 2000,
        position: 'middle'
      });
      toast.present();
    }
  }

  cancel() {
    this.modalCtrl.dismiss(null, "cancel");
  }
}

export async function openThrowDriftBottleModal(modalCtrl: ModalController): Promise<any> {
  const modal = await modalCtrl.create({
    component: ThrowDriftBottleComponent,
    breakpoints: [0.7, 1.0],
    initialBreakpoint: 0.7
  });
  modal.present();

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

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