import { CommonModule, NgFor, NgIf, UpperCasePipe } from '@angular/common';
import { Component, inject, ViewChild, ViewContainerRef } from '@angular/core';
import { TextPipe } from '../../text.pipe';
import { SearchComponent } from '../Search/search.component';
import { HotelService } from '../Parent/services/hotel.service';
import { Hotel } from '../HotelItem/hotel';
import { HttpClient } from '@angular/common/http';
import { filter, from, last, map, Observable, scan } from 'rxjs';
import { HotelItem } from '../HotelItem/HotelItem.component';
import { RouterLink } from '@angular/router';

interface User {
  name: string;
  age: number;
}

@Component({
  selector: 'app-hotel-list',
  standalone: true,
  imports: [
    UpperCasePipe,
    TextPipe,
    SearchComponent,
    HotelItem,
    NgFor,
    NgIf,
    RouterLink,
  ],
  templateUrl: './hotel-list.component.html',
  styleUrl: './hotel-list.component.css',
})
export class HotelListComponent {
  public search: string = '';
  public hotelService: HotelService = inject(HotelService);
  public response: any = null;
  public hotels: Array<Hotel> = [{} as Hotel];

  @ViewChild('test2', { read: ViewContainerRef })
  private test2ViewContainerRef!: ViewContainerRef;

  constructor(private http: HttpClient) { }

  async loadTest2() {
    for (let i = 0; i < 1; i++) {
      const { Test2Component } = await import('../test2/test2.component');

      this.test2ViewContainerRef.createComponent(Test2Component);
    }
  }

  ngOnInit() {
    this.http.get<Array<Hotel>>('api/hotels').subscribe((res) => {
      this.hotels = res;
    });

    const users = [
      { name: 'Max', age: 21 },
      { name: 'Peter', age: 31 },
      { name: 'Hans', age: 13 },
      { name: 'Klaus', age: 51 },
      { name: 'Dieter', age: 1 },
      { name: 'Jan-Marlon', age: 3 },
    ];

    const stream: Observable<User> = from(users);

    stream
      .pipe(
        filter((user) => user.age > 18),
        scan((acc, user) => acc + user.age, 0),
        map((ageSum, index) => ageSum / (index + 1)),
        last(),
      )
      .subscribe(console.log);

    // const stream: Observable<number | string> = from([5, 1, 2, 12, 5, 14, 17, 5, "testing"]);

    // stream.pipe(
    //  filter((value) => typeof value === "number"),
    //  tap((value) => console.log("Zahl:" + value)),
    //  filter((value: number) => value % 2 === 0),
    //  tap((value) => console.log("Gerade Zahl: " + value)),
    //  toArray(),
    //).subscribe(console.log);
  }

  public test() {
    console.log(this.search);
  }

  // public foundHotels = this.hotels.pipe(
  //   filter((hotel) => hotel.hotelName.includes(this.search)),
  // );
}
