import { Component, OnInit } from '@angular/core';
import { ModalController, NavController } from '@ionic/angular';
import { FilmDetailsModalComponent } from '../film-details-modal/film-details-modal.component';
import { MusicService } from 'src/modules/server/music.serve';
import { ActivatedRoute } from '@angular/router';
import Parse from 'parse';

@Component({
  selector: 'app-music',
  templateUrl: './music.page.html',
  styleUrls: ['./music.page.scss'],
})
export class MusicPage implements OnInit {

  musics: any[] = [];
  searchName: string = '';
  searchAuthor: string = '';
  selectedRating: string = '';
  sortByRating: boolean = false;
  isAscending:boolean=true;
  score:number=0;

  constructor(private musicService: MusicService,
    private route:ActivatedRoute,
    private navCtrl:NavController
  ) { 
    
  }

  async updateFilmRating(film:Parse.Object,score:any){
    if(!score) return
    let rating = await this.checkExists(film)
    if(!rating?.id){
      let FilmRating = Parse.Object.extend("LQLMusicRating");
      rating = new FilmRating
    }
    // 更新个人打分
    rating?.set("user",Parse.User.current()?.toPointer())
    rating?.set("music",film?.toPointer())
    rating?.set("score",score)
    await rating?.save();

    // 更新全局平均分数
    let query = new Parse.Query("LQLMusicRating");
    query.equalTo("music",film?.toPointer())
    let list = await query.find()
    if(!list?.length) return
    console.log(list)
    let sum:any = 0
     list?.forEach((item)=>sum+=item.get("score"))
    let avgRating = sum / list?.length
    film.set("rating",avgRating);
    film.save()
  }
  async checkExists(film:Parse.Object){
    let query = new Parse.Query("LQLMusicRating");
    query.equalTo("user",Parse.User.current()?.toPointer())
    query.equalTo("music",film?.toPointer())
    return await query.first()
  }

  ngOnInit(): void {
    this.loadMusics();
  }

  async loadMusics() {
    const queryParams = {
      name: this.searchName,
      author: this.searchAuthor,
      rating: this.selectedRating
    };
    this.musics=await this.musicService.getMusics(queryParams);
    //this.films = await this.filmService.getFilms();
  }

  toggleSort() {
    if (this.isAscending) {
      this.musics = this.musics.sort((a, b) => a.get('rating') - b.get('rating'));
      this.isAscending = false;
    } else {
      this.musics = this.musics.sort((a, b) => b.get('rating') - a.get('rating'));
      this.isAscending = true;
    }
    // this.sortByRating = !this.sortByRating;
    // this.loadMusics();
  }

  goToBack()
  {
    this.navCtrl.back();
  }


  // films = [
  //   { title: '偏爱', image: 'assets/image/BoFan.png', details: '张芸京', rating: 8.5 },
  //   { title: '雪之华', image: 'assets/image/BoFan.png', details: '中岛美嘉', rating: 9.3 },
  //   { title: '是你', image: 'assets/image/BoFan.png', details: '梦然', rating: 9. },
  //   { title: '悬溺', image: 'assets/image/BoFan.png', details: '葛东琪', rating: 8.7 },
  //   { title: '雾里', image: 'assets/image/BoFan.png', details: '朵芊', rating: 9.3 },
  //   { title: '祈愿~致那个时候的你~', image: 'assets/image/BoFan.png', details: '当山真玲', rating: 9.1 },
  //   { title: 'SAKURA', image: 'assets/image/BoFan.png', details: 'Che·Nelle', rating: 9.2 },
  //   { title: 'Letting Go', image: 'assets/image/BoFan.png', details: '蔡建雅', rating: 9.4 },
  //   { title: '我们啊', image: 'assets/image/BoFan.png', details: '三块木头', rating: 9.5 },
  //   { title: '星辰不坠落', image: 'assets/image/BoFan.png', details: '蓝心羽', rating: 9.6 },
  //   { title: '溯', image: 'assets/image/BoFan.png', details: '马吟吟', rating: 9.1 },
  //   { title: '你啊你啊', image: 'assets/image/BoFan.png', details: '魏如宣', rating: 9.0 },
  //   { title: 'Tattoo', image: 'assets/image/BoFan.png', details: 'GJan', rating: 9.3 },
  // ];

  // constructor(private modalController: ModalController) {}

  // async showFilmDetails(film:any) {
  //   // 弹出模态框显示影视介绍
  //   const modal = await this.modalController.create({
  //     component: FilmDetailsModalComponent,
  //     componentProps: {
  //       film: film
  //     }
  //   });
  //   return await modal.present();
  // }
  // ngOnInit() {
  // }
}
