import { animate, style, transition, trigger } from '@angular/animations';
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { AlertController, InfiniteScrollCustomEvent, NavController } from '@ionic/angular';
import Parse from "parse";

interface BookInfo {
  src: string;
  title: string;
  authors: string;
  publisher: string;
  publicationDate: string;
  isbn: string;
  price: string;
  pages: string;
  size: string;
  editor: string;
  subject: string;
  classificationNumbers: any;
  collectionStatus: string;
  borrowingAttribute: string;
  acquisitionDate: string;
  callNumber: string;
  yearVolumeIssue: string;
}
// 引用Parse JS SDK

Parse.initialize("dev"); // 设置applicationId
Parse.serverURL = "http://web2023.fmode.cn:9999/parse"; // 设置serverURL

@Component({
  selector: 'app-details-book',
  templateUrl: './details-book.page.html',
  styleUrls: ['./details-book.page.scss']
})
export class DetailsBookPage implements OnInit {
  bookList:any[] = [];
  bookInfo:any;
  items:any[] = [];
  book:BookInfo={
    src: '',
    title: '',
    authors: '',
    publisher: '',
    publicationDate: '',
    isbn: '',
    price: '',
    pages: '',
    size: '',
    editor: '',
    subject: '',
    classificationNumbers: [],
    collectionStatus: '',
    borrowingAttribute: '',
    acquisitionDate: '',
    callNumber: '',
    yearVolumeIssue: ''
  };

  @Input()
  fillIcon:string = "star"
  @Input()
  emptyIcon:string = "star-outline"
  @Input()
  color:string = "red"
  @Input()
  value:number = 0;
  @Output()
  onValueChange:EventEmitter<number> = new EventEmitter<number>

  maxValue:number = 5;
  starCheckList:Array<boolean> = []
  

  constructor(
    private router: Router,
    private route:ActivatedRoute,
    private navCtrl:NavController,
    private alertController:AlertController
  ) {}

  ngOnInit() {
    this.loadBookById()
    // this.generateItems();
    this.makeStarList();
  }

  async loadBookById(){
    // let id = location.pathname.split("/").pop();
    let id = this.route.snapshot.params["id"]
    
    if(id){
      let query = new Parse.Query("AWBook");
      this.bookInfo = await query.get(id);
      this.initBookInfo(this.bookInfo);
      this.loadItemByBookTitle(this.bookInfo);
    }
  }

  initBookInfo(bookInfo:Parse.Object){
    this.book.src = bookInfo.get("src");
    this.book.title = bookInfo.get("title");
    this.book.authors = bookInfo.get("authors");
    this.book.publisher = bookInfo.get("publisher");
    this.book.publicationDate = bookInfo.get("publicationDate");
    this.book.isbn = bookInfo.get("isbn");
    this.book.price = bookInfo.get("price");
    this.book.pages = bookInfo.get("pages");
    this.book.subject = bookInfo.get("subject");
    this.book.size = bookInfo.get("size");
    this.book.editor = bookInfo.get("editor");
    this.book.classificationNumbers[0] = bookInfo.get("classificationNumber0");
    this.book.classificationNumbers[1] = bookInfo.get("classificationNumber1");
    this.book.collectionStatus = bookInfo.get("collectionStatus");
    this.book.borrowingAttribute = bookInfo.get("borrowingAttribute");
    this.book.acquisitionDate = bookInfo.get("acquisitionDate");
    this.book.callNumber = bookInfo.get("callNumber");
    this.book.yearVolumeIssue = bookInfo.get("yearVolumeIssue");
  }

  async loadItemByBookTitle(bookInfo:Parse.Object){
    let query = new Parse.Query("AWItem");
    query.equalTo("bookTitle", bookInfo.get("title")); 

    let bookInfoList = await query.find();
    this.items = bookInfoList;
  }



  // private generateItems() {
  //   const count = this.items.length + 1;
  //   for (let i = 0; i < 5; i++) {
  //     let item:ItemInfo = {
  //       title:"111",
  //       callNumber:"",
  //       barCodeNumber:"",
  //       bookStatus:"",
  //       borrowingProperties:"",
  //       depositDate:"",
  //       shelvingNumber:"",
  //       annualRollPeriod:"",
  //       donor:""
  //     }
  //     this.items.push(item);
  //   }
  // }

  onStarClick(index:number){
    let score = index + 1

    if(score==this.value){ // 点原星星,设置0分
      this.value = 0
    }else{ // 根据星星位置,设置分数
      this.value = score;
    }

    this.makeStarList();
    this.onValueChange.emit(this.value)
  }
  makeStarList(){
    let starList = []
    for (let index = 0; index < this.value; index++) {
      starList.push(true)
    }
    let leftCount = this.maxValue - this.value
    for (let index = 0; index < leftCount; index++) {
      starList.push(false)
    }
    this.starCheckList = starList
  }


  onClickRequest(event:MouseEvent){
    let id = this.route.snapshot.params["id"]
    this.router.navigate(['request/'+id]);
  }

  // onIonInfinite(ev: any) {
  //   this.generateItems();
  //   setTimeout(() => {
  //     (ev as InfiniteScrollCustomEvent).target.complete();
  //   }, 500);
  // }

}
