import {Component, inject, input, OnInit} from '@angular/core';
import {IndexedDB} from '../../storage/indexed-db';
import {Router, RouterLink} from '@angular/router';
import {LoadStatus, ServiceManager} from '../../service-manager';
import {TUI_BREAKPOINT, TuiButton} from '@taiga-ui/core';
import {ChatService} from '@chatenium/chatenium-sdk/services/chatService';
import {NetworkService} from '@chatenium/chatenium-sdk/services/networkService';
import {Oimg} from '../elements/oimg/oimg';
import {TranslatePipe} from '@ngx-translate/core';

@Component({
  selector: 'network-list',
  imports: [
    Oimg,
    TranslatePipe,
    TuiButton,
    RouterLink
  ],
  templateUrl: './network-list.html',
  styleUrl: './network-list.scss',
})
export class NetworkList implements OnInit {
  userid = input<string>("")
  token = input<string>("")

  indexedDb = inject(IndexedDB)
  router = inject(Router)
  serviceManager = inject(ServiceManager)
  breakpoint = inject(TUI_BREAKPOINT)

  async ngOnInit() {
    this.serviceManager.networkService = new NetworkService(this.userid(), this.token(), "", this.indexedDb.getApi(), () => {})
    try {
      this.serviceManager.networks.set(await this.serviceManager.networkService.get())
      this.serviceManager.networksStatus.set(LoadStatus.loaded)
    } catch (e) {
      console.error(e)
      this.serviceManager.networksStatus.set(LoadStatus.error)
    }
  }
}
