import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgSelectModule } from '@ng-select/ng-select';
import { TranslateModule } from '@ngx-translate/core';
import { pageSizeOptions } from '../../utils/app.constants';
import { SetupUser } from '../../models/user';
import { UserSetupService } from '../../services/user-setup.service';
import { error } from 'node:console';

@Component({
  selector: 'app-setup-user',
  standalone: true,
  imports: [TranslateModule, ReactiveFormsModule, FormsModule, CommonModule, NgSelectModule],
  templateUrl: './setup-user.component.html',
  styleUrl: './setup-user.component.scss'
})
export class SetupUserComponent implements OnInit {
  allItems: SetupUser[] = [];
  currentPage: number = 1;
  pageSizeOptions = pageSizeOptions
  itemsPerPage: number = 5;
  searchText: string = '';
  renewalDataExpanded: boolean = true;
  totalCount: number = 0;
  userId!: string;
  userFullname!: string;
  defaultPassword!: string;
  mode: 'edit' | 'view' = 'view';
  showForm = false;
  selectedUserId!: any;
  user: any;

  constructor(private userService: UserSetupService){}

  onSearch(value: string): void {
  this.userService.setSearchText(value);
}

  onPageSizeChange(pageSize: number): void {
    this.userService.setItemsPerPage(pageSize);
  }

  nextPage(): void {
    this.userService.nextPage();
  }

  previousPage(): void {
    this.userService.previousPage();
  }

  totalPages() {
    return 1;
  }
  
  toggleCard(arg0: string) {
    throw new Error('Method not implemented.');
  }
  getTotalPages(): number {
    return this.userService.getTotalPages();
  }
  onSubmit() {
    if(!this.userId || !this.userFullname|| !this.defaultPassword){
       console.warn('Form incomplete');
      return
    }

    const newUser : SetupUser = {
      userId: this.userId,
      userFullname: this.userFullname,
      email: `${this.userId}@dummy.com` ,// temporary placeholder
      role: 'ADMIN',
      defaultPassword: this.defaultPassword
    }
    this.userService.addUser(newUser).subscribe({
      next: () => {
        this.userService.loadUsers();
        this.userId = '';
        this.userFullname = '';
        this.defaultPassword = '';
      },
      error: (err: any) => console.error(err)
    });


    
  }
  onView(userId: any){
    this.mode = 'view';
    this.showForm = true;
    this.selectedUserId = userId;
    this.userService.getUserById(userId).subscribe((user: any)=>{
      this.userId = user.userId;
      this.userFullname = user.userFullname;
      this.defaultPassword = '';
    })
  }

  onDelete(userId: any){
    this.userService.deleteUser(userId).subscribe({
      next: (res: any) => {
        this.userService.loadUsers();
        this.showForm = false;
        this.userId = '';
        this.userFullname = '';
        this.defaultPassword = '';
        this.selectedUserId = null;
      },
      error: (err:any) =>{
        console.log('user not deleted')
      }
      
    });

  }
  
ngOnInit(): void {
  this.userService.loadUsers();
  this.userService.paginatedUsers$.subscribe((users: SetupUser[]) => this.allItems = users);

  this.userService.currentPage$.subscribe((page: number) => {
      this.currentPage = page;
    });
    this.userService.totalCount$.subscribe((count: number) => {
      this.totalCount = count;
    });
    this.userService.searchText$.subscribe((text: string) => {
      this.searchText = text;
    });
    this.userService.itemsPerPage$.subscribe((size: number) => {
      this.itemsPerPage = size;
    });
}

}
