import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import {
  AlertController,
  IonicModule,
  LoadingController,
} from '@ionic/angular';
import { AiChatService } from '../../../services/aichart.service';
import { LiveService } from '../../../services/live.service';
import * as Parse from 'parse';
@Component({
  selector: 'app-gift-modal',
  templateUrl: './gift-modal.component.html',
  styleUrls: ['./gift-modal.component.scss'],
  standalone: true,
  imports: [IonicModule, CommonModule, FormsModule],
})
export class GiftModalComponent implements OnInit {
  @Input('toUid') toUid!: string; // 接收礼物的uid

  isOpenGift: boolean = false; // 是否显示礼物弹窗
  giftList: Array<any> = [];
  tabs: Array<any> = [
    {
      name: '礼物',
      val: 'all',
    },
    {
      name: '专属礼物',
      val: 'vip',
    },
    {
      name: '守护',
      val: 'guard',
    },
  ];
  activeTab: string = 'all';
  currentGift: any; //当前礼物
  giftCount: number = 1;
  wallet: any = { balance: 0 };
  isShowGiftModal: boolean = false; // 是否显示礼物动效弹窗
  times: any;
  @Output() sendEmit: EventEmitter<any> = new EventEmitter<any>();

  constructor(
    private aiServ: AiChatService,
    private alertController: AlertController,
    public liveService: LiveService,
    private loadingCtrl: LoadingController
  ) {}

  async ngOnInit() {
    this.giftList = await this.aiServ.getGift();
  }
  async openModal() {
    let uid: any = Parse.User.current()?.id;
    this.wallet = await this.aiServ.getWallet(uid);
    this.isOpenGift = true;
  }
  async selectTab(val: string) {
    if (this.activeTab === val) return;
    this.giftList = await this.aiServ.getGift(val == 'all' ? '' : val);
    this.activeTab = val;
    this.currentGift = null;
  }
  onChange() {
    this.giftCount = this.giftCount < 1 ? 1 : Math.floor(this.giftCount);
    console.log(this.giftCount);
  }

  async sendGift() {
    let _this = this
    if (!this.currentGift?.id || this.giftCount < 1) return;
    const alert = await this.alertController.create({
      cssClass: 'my-custom-class',
      header: '送出礼物',
      message: `确定送出${this.giftCount}件${this.currentGift.name}吗`,
      buttons: [
        {
          text: '取消',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {},
        },
        {
          text: '确定',
          handler: async () => {
            const loading = await _this.loadingCtrl.create({
              message: '送出礼物中...',
            });
            loading.present();
            _this.aiServ
              .putGift(_this.toUid, _this.currentGift.id, _this.giftCount)
              .then((data) => {
                console.log(data);
                // _this.liveService.get_duration();
                _this.sendEmit.emit(); //触发父组件的sendEmit事件
                loading.dismiss();
                _this.isOpenGift = false;
                _this.isShowGiftModal = true;
                setTimeout(() => {
                  _this.isShowGiftModal = false;
                  console.log(_this.isShowGiftModal);
                  console.log('5s后关闭');
                }, 5000);
              })
              .catch((err) => {
                _this.isOpenGift = false;
                loading.dismiss();
                console.error(err);
              });
          },
        },
      ],
    });
    await alert.present();
  }
}
