import { Component, OnInit } from '@angular/core';
import { ModalController, NavController } from '@ionic/angular';
import { FilmDetailsModalComponent } from '../film-details-modal/film-details-modal.component';
import { FilmService } from '../../server/firm.serve';
import { ActivatedRoute } from '@angular/router';
import { EditRatioStarComponent } from 'src/modules/coms/edit-ratio-star/edit-ratio-star.component';
import Parse from "parse";
@Component({
  selector: 'app-fiml',
  templateUrl: './fiml.page.html',
  styleUrls: ['./fiml.page.scss'],
})
export class FimlPage implements OnInit {

  films: any[] = [];
  searchName: string = '';
  searchDetails: string = '';
  selectedRating: number =6;
  sortByRating: boolean = false;
  isAscending: boolean = true;
  score:number = 0;

  constructor(private filmService: FilmService,
    private route:ActivatedRoute,
    private navCtrl:NavController
  ) { 
    
  }
  
  async loadFilms() {
    const queryParams = {
      name: this.searchName,
      details: this.searchDetails,
      rating: this.selectedRating,
      sortByRating:this.sortByRating
      
    };
    this.films=await this.filmService.getFilms(queryParams);
    //this.score=this.selectedRating;
  }

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

    // 更新全局平均分数
    let query = new Parse.Query("FilmRating");
    query.equalTo("film",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("FilmRating");
    query.equalTo("user",Parse.User.current()?.toPointer())
    query.equalTo("film",film?.toPointer())
    return await query.first()
  }

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


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

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

  // films = [
  //   { title: '奥特曼', image: 'assets/image/ATeMan.png', details: '奥特曼的详情描述', rating: 8.5 },
  //   { title: '肖生克的救赎', image: 'assets/image/The Shawshank Redemption.jpg', details: '肖生克的救赎的详情描述', rating: 9.3 },
  //   { title: '哆啦A梦', image: 'assets/image/Doraemon.png', details: '哆啦A梦的详情描述', rating: 8.0 },
  //   { title: '蜘蛛侠', image: 'assets/image/spiderman.png', details: '蜘蛛侠的详情描述', rating: 8.7 },
  //   { title: '名侦探柯南', image: 'assets/image/MingZhenTang.jpg', details: '蜘蛛侠的详情描述', rating: 9.4 },
  //   { title: '神偷奶爸', image: 'assets/image/ShenTouNaiBa.png', details: '蜘蛛侠的详情描述', rating: 9.3 },
  //   { title: '飞驰人生2', image: 'assets/image/FeiChiRenShen.jpg', details: '蜘蛛侠的详情描述', rating: 9.6 },
  //   { title: '周处除三害', image: 'assets/image/ZhouChuChuSanHai.webp', details: '蜘蛛侠的详情描述', rating: 9.3},
  //   { title: '侏罗纪世界', image: 'assets/image/ZhuLouJiShiJi.jpg', details: '蜘蛛侠的详情描述', rating: 9.0 },
  //   { title: '末日崩塌', image: 'assets/image/MoReBengTa.png', details: '蜘蛛侠的详情描述', rating: 9.2 },
  //   { title: '疯狂动物城', image: 'assets/image/FengKuangDongWuCheng.jpg', details: '蜘蛛侠的详情描述', rating: 9.5 },
  //   { title: '唐人街探案', image: 'assets/image/TanRenJieTanAn.jpg', details: '蜘蛛侠的详情描述', rating: 9.2 },
  // ];

  // constructor(private modalController: ModalController) {}

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


}
