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

@Component({
  selector: 'app-outdoor',
  templateUrl: './outdoor.page.html',
  styleUrls: ['./outdoor.page.scss'],
})
export class OutdoorPage implements OnInit {
  outdoors: any[] = [];
  searchName: string = '';
  searchDetails: string = '';
  selectedRating: string = '';
  sortByRating: boolean = false;
  isAscending: boolean = true;
  score:number=0;

  constructor(private outdoorService: OutdoorService,
    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("LQLOutdoorRating");
      rating = new FilmRating
    }
    // 更新个人打分
    rating?.set("user",Parse.User.current()?.toPointer())
    rating?.set("outdoor",film?.toPointer())
    rating?.set("score",score)
    await rating?.save();

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


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

  async loadOutdoors() {
    const queryParams = {
      name: this.searchName,
      details: this.searchDetails,
      rating: this.selectedRating,
      sortByRating:this.sortByRating
    };
    this.outdoors=await this.outdoorService.getOutdoors(queryParams);
   
  }

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

  goToBack()
  {
    this.navCtrl.back();
  }
  // films = [
  //   { title: '足球', image: 'assets/image/ZuQu.jpg', details: '奥特曼的详情描述', rating: 8.5 },
  //   { title: '篮球', image: 'assets/image/LanQu.png', details: '肖生克的救赎的详情描述', rating: 9.3 },
  //   { title: '远足', image: 'assets/image/YuanZu.png', details: '哆啦A梦的详情描述', rating: 8.0 },
  //   { title: '羽毛球', image: 'assets/image/YuMaoQu.png', details: '蜘蛛侠的详情描述', rating: 8.7 },
  //   { title: '徒步旅行', image: 'assets/image/TuBuLuXin.jpg', details: '哆啦A梦的详情描述', rating: 9.2 },
  //   { title: '骑行', image: 'assets/image/QiXin.jpg', details: '哆啦A梦的详情描述', rating: 9.5 },
  //   { title: '登山', image: 'assets/image/DengShan.jpg', details: '哆啦A梦的详情描述', rating: 9.2 },
  //   { title: '钓鱼', image: 'assets/image/DiaoYu.png', details: '哆啦A梦的详情描述', rating: 9.5 },
  //   { title: '野餐', image: 'assets/image/YeCang.jpg', details: '哆啦A梦的详情描述', rating: 9.1 },
  //   { title: '滑雪', image: 'assets/image/HuaXue.png', details: '哆啦A梦的详情描述', rating: 9.0 },
  //   { title: '攀岩', image: 'assets/image/PangYan.png', details: '哆啦A梦的详情描述', rating: 9.1 },
  // ];

  // constructor(private modalController: ModalController) {}

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