import { Component, OnInit, ViewChild } from '@angular/core';
import * as Parse from 'parse';
import {
  AlertController,
  IonicModule,
  LoadingController,
} from '@ionic/angular';
import { Router } from '@angular/router';
import { Swiper } from 'swiper';
import { AiChatService } from '../../../services/aichart.service';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss'],
  standalone: true,
  imports: [IonicModule],
  // schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class HomeComponent implements OnInit {
  @ViewChild('modal') modal!: any;
  options: Array<any> = [
    {
      label: '关注',
      value: 'follow',
      icon: 'home-outline',
      color: 'primary',
    },
    {
      label: '推荐',
      value: 'recommend',
      icon: 'home-outline',
      color: 'primary',
    },
    {
      label: '新人',
      value: 'news',
      icon: 'videocam-outline',
      color: 'danger',
    },
    {
      label: '三星',
      value: '三星',
      icon: 'videocam-outline',
      color: 'danger',
    },
    {
      label: '四星',
      value: '四星',
      icon: 'videocam-outline',
      color: 'danger',
    },
    {
      label: '五星',
      value: '五星',
      icon: 'people-outline',
    },
  ];
  currentValue: string = 'recommend';
  oldCurrentValue: string = 'recommend';
  isOpen: boolean = false; //显示选择弹窗
  banner: Array<Parse.Object> = [];
  roomList: Array<any> = [];
  pageSwiper: Swiper | undefined | any;
  notices: Array<any> = [
    {
      title: '【公告】',
      content:
        '欢迎来到直播间，请遵守直播规则，禁止一切违法直播行为，否则封号处理。',
      time: '2022-06-01',
    },
    {
      title: '【公告】',
      content:
        '欢迎来到直播间，请遵守直播规则，禁止一切违法直播行为，否则封号处理。',
      time: '2022-06-01',
    },
    {
      title: '【公告】',
      content:
        '欢迎来到直播间，请遵守直播规则，禁止一切违法直播行为，否则封号处理。',
      time: '2022-06-01',
    },
  ];
  viewAnchor: string = localStorage.getItem('viewSex') || '女';
  get sex(): string {
    const map: any = {
      all: '全部',
      男: '男主播',
      女: '女主播',
    };
    return map[this.viewAnchor];
  }
  constructor(
    private loadingCtrl: LoadingController,
    private alertController: AlertController,
    private router: Router,
    private aiServ: AiChatService
  ) {}

  ngOnInit() {
    this.refresh();
  }
  async refresh() {
    await this.getBanner();
    await this.getRoom();
    setTimeout(() => {
      this.initSwiperTimeEvent();
    }, 0);
  }
  async getBanner() {
    let query = new Parse.Query('Banner');
    query.equalTo('company', this.aiServ.company);
    query.descending('index');
    query.equalTo('isEnabled', true);
    query.notEqualTo('isDeleted', true);
    let banner = await query.find();
    this.banner = banner;
  }
  initSwiperTimeEvent() {
    // 初始化轮播图
    let swiper = new Swiper('.mySwiper', {
      loop: true, // 循环模式选项
      observer: false, //修改swiper自己或子元素时，自动初始化swiper
      observeParents: true, //修改swiper的父元素时，自动初始化swiper
      autoplay: {
        delay: 3000,
      },
      pagination: {
        el: '.swiper-pagination',
      },
    });
    swiper.on('slideChange', function (event: any) {
      // console.log(event);
    });

    let swiperNot = new Swiper('.swiper-notice', {
      loop: true, // 循环模式选项
      observer: false, //修改swiper自己或子元素时，自动初始化swiper
      observeParents: true, //修改swiper的父元素时，自动初始化swiper
      autoplay: {
        delay: 5000,
      },
      direction: 'vertical',
    });
  }
  async getRoom(type?: string) {
    const loading = await this.loadingCtrl.create({
      message: '正在加载',
    });
    loading.present();
    let data: Array<any> = [];
    if (!type) type = this.currentValue;
    let uid = Parse.User.current()?.id;
    let sex = this.viewAnchor == 'all' ? null : this.viewAnchor;
    switch (type) {
      case 'follow':
        data = await this.aiServ.getRooms({ uid: uid, follow: true, sex });
        break;
      case 'recommend':
        data = await this.aiServ.getRooms({ uid: uid, recommend: true, sex });
        break;
      case 'news':
        data = await this.aiServ.getRooms({ uid: uid, sex });
        break;
      case '三星':
        data = await this.aiServ.getRooms({ uid: uid, star: '三星', sex });
        break;
      case '四星':
        data = await this.aiServ.getRooms({ uid: uid, star: '四星', sex });
        break;
      case '五星':
        data = await this.aiServ.getRooms({ uid: uid, star: '五星', sex });
        break;
      default:
        break;
    }
    this.roomList = data;
    console.log(data);
    loading.dismiss();
  }
  async presentAlert(item: any) {
    const alert = await this.alertController.create({
      header: item.title || '消息通知',
      message:
        item.content || 'A message should be a short, complete sentence.',
      buttons: ['关闭'],
    });
    await alert.present();
  }
  segmentChanged(e: any) {
    let { value } = e.detail;
    this.currentValue = value;
  }
  onIonChange(event: CustomEvent) {
    this.currentValue = event.detail.value;
  }
  /* 关闭弹窗回调 */
  onDidDismiss(event: CustomEvent) {
    this.isOpen = false;
    // console.log(this.currentValue);
  }
  cancel(type: string, value?: string) {
    // console.log(type, value);
    if (type == 'cancel') {
      this.currentValue = this.oldCurrentValue;
    } else {
      this.oldCurrentValue = this.currentValue;
    }
    this.isOpen = false;
    this.modal.dismiss();
    this.getRoom();
  }
  onChangeSex(e: any) {
    // console.log(e.detail.value);
    localStorage.setItem('viewSex', e.detail.value);
    this.viewAnchor = e.detail.value;
    this.getRoom();
  }
  search() {
    this.router.navigate(['live/search']);
  }
  toUrl(url: string) {
    this.router.navigate([url]);
  }
}
