import { DatePipe } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import { LoadingController, ToastController } from '@ionic/angular';
import * as Parse from 'parse';
import { NavComponent } from '../../../app/components/nav/nav.component';
import { AiChatService } from '../../../services/aichart.service';
import { ionicStandaloneModules } from '../../ionic-standalone.modules';

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.scss'],
  standalone: true,
  imports: [FormsModule, NavComponent, ...ionicStandaloneModules],
  providers: [DatePipe]
})
export class SearchComponent implements OnInit {
  roomList: any = [];
  company: string = Parse.User.current()?.get('company');
  value: String | undefined;
  Search: any = [];
  hots: Array<Parse.Object> = [];

  constructor(
    private aiServ: AiChatService,
    private router: Router,
    private toastController: ToastController
  ) {}

  async presentToast(message: string) {
    const toast = await this.toastController.create({
      message: message,
      duration: 2000,
    });
    toast.present();
  }

  ngOnInit() {
    this.getLocalStorage();
    this.getRoom();
  }

  // 热门搜索
  async getRoom() {
    let query = new Parse.Query('Room');
    query.equalTo('company', this.company);
    query.limit(5);
    // query.descending("score")
    let res = await query.find();
    this.hots = res;
    // console.log(res);
    // if (res && res.length > 0) {
    //   res.forEach(item => {
    //     games.push(item.get('title'))
    //   })
    //   this.hots = games
    // }
  }

  getLocalStorage() {
    let storage = localStorage.getItem('Search');
    this.Search = storage && JSON.parse(storage);
    console.log(this.Search);
  }

  SearchHot(value: String) {
    this.value = value;
    this.toSearch();
  }
  //搜索
  toSearch() {
    this.roomList = [];
    if (!this.value || this.value?.trim() === '') {
      this.presentToast('请输入搜索内容');
      return;
    }
    this.getSearch();
    let storage = localStorage.getItem('Search');
    if (!storage) {
      let searchs = [this.value];
      localStorage.setItem('Search', JSON.stringify(searchs));
      return;
    }
    console.log(storage);
    let list = JSON.parse(storage);
    list.splice(0, 0, this.value);
    let arr: any = new Set(list);
    list = Array.from(arr);
    if (list.length > 10) {
      list.splice(-1, 1);
    }
    localStorage.setItem('Search', JSON.stringify(list));
    this.Search = list;
  }
  async getSearch() {
    let query = new Parse.Query('Room');
    // query.equalTo('company', this.company);
    // query.equalTo('title', this.value);
    // query.notEqualTo('isDeleted', true);
    // let r = await query.find();
    // this.roomList = r;
    let uid = Parse.User.current()?.id;
    this.roomList = await this.aiServ.getRooms({
      title:this.value,
      uid: uid,
    });
    if (this.roomList.length == 0) this.presentToast('暂无搜索结果');
  }
  toUrl(url: string) {
    this.router.navigate([url]);
  }
}
