import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { CloudObject, CloudQuery } from 'src/lib/ncloud';
import { 
  IonHeader, IonToolbar, IonTitle, IonContent, IonButtons, 
  IonBackButton, IonCard, IonCardHeader, IonCardTitle, 
  IonCardSubtitle, IonCardContent, IonChip, IonLabel, 
  IonIcon, IonButton, IonImg, IonSpinner, NavController
} from '@ionic/angular/standalone';
import { addIcons } from 'ionicons';
import { 
  star, downloadOutline, createOutline, bookmarkOutline,
  documentTextOutline, timeOutline, fileTrayOutline, 
  pricetagsOutline, shareSocialOutline
} from 'ionicons/icons';

@Component({
  selector: 'app-page-book-display',
  templateUrl: './page-book-display.component.html',
  styleUrls: ['./page-book-display.component.scss'],
  standalone: true,
  imports: [
    CommonModule,
    IonHeader, IonToolbar, IonTitle, IonContent, IonButtons, 
    IonBackButton, IonCard, IonCardHeader, IonCardTitle, 
    IonCardSubtitle, IonCardContent, IonChip, IonLabel, 
    IonIcon, IonButton, IonImg, IonSpinner
  ]
})
export class PageBookDisplayComponent  implements OnInit {

   book: CloudObject | undefined | null;

  constructor(
    private route: ActivatedRoute,
    private navCtrl: NavController
  ) {
    addIcons({ 
      star, downloadOutline, createOutline, bookmarkOutline,
      documentTextOutline, timeOutline, fileTrayOutline,
      pricetagsOutline, shareSocialOutline
    });
    
    this.route.params.subscribe(params => {
      this.loadBook(params['bookId']);
    });
  }

  //返回
  back() {
    this.navCtrl.back();
  }

  //加载文书
  async loadBook(bookId: string) {
    let query = new CloudQuery("DocumentCategory");
    this.book = await query.get(bookId);
  }

  // 为标签生成随机颜色
  getTagColor(tag: string): string {
    const colors = ['primary', 'secondary', 'tertiary', 'success', 'warning', 'danger'];
    const index = Math.abs(this.hashCode(tag)) % colors.length;
    return colors[index];
  }

  // 简单的字符串hash函数
  private hashCode(str: string): number {
    let hash = 0;
    for (let i = 0; i < str.length; i++) {
      const char = str.charCodeAt(i);
      hash = ((hash << 5) - hash) + char;
      hash |= 0; // Convert to 32bit integer
    }
    return hash;
  }

  ngOnInit() {}

}
