import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { GetArticlesWithoutMovements, ReponseDataGetGetArticlesWithoutMovements } from '../../../interfaces/inventory-management/inventory-management.inerface';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { ResourcesService } from '../../../services/resources.service';
import { InventoryManagementService } from '../../../services/inventory-management/inventory-management.service';
import { lastValueFrom } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';

@Component({
    selector: 'app-without-movements-article',
    templateUrl: './without-movements-article.component.html',
    styleUrl: './without-movements-article.component.css',
    standalone: false
})
export class WithoutMovementsArticleComponent implements OnInit, AfterViewInit {
  public isLoading: boolean;
  public dataSource: MatTableDataSource<GetArticlesWithoutMovements>;
  public displayedColumns: string[];

  @ViewChild(MatPaginator) paginator!: MatPaginator;
  @ViewChild(MatSort) sort!: MatSort;

  constructor (
    public resourcesService: ResourcesService,
    private _inventoryManagementService: InventoryManagementService,
  ) {
    this.isLoading = false;
    this.dataSource = new MatTableDataSource<GetArticlesWithoutMovements>();
    this.displayedColumns = [
      'ID_STOCK',
      'DIFERENCIA_ANIOS',
      'DIFERENCIA_MES',
      'DIFERENCIA_DIAS',
      'FECHA_MODIFICA',
      'USUARIO_MODIFICA',
    ];
  }
  
  async ngOnInit() {
    this.isLoading = true;
    await this.getArticlesWithoutMovements();
    this.isLoading = false;
  }

  ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }

  public async getArticlesWithoutMovements () {
    this.isLoading = true;
    const USER: string = this.resourcesService.getUser();
    const LINE: number = this.resourcesService.getLineNumber();
    await lastValueFrom(this._inventoryManagementService.getArticlesWithoutMovements(USER, LINE)).then(
      (responseData: ReponseDataGetGetArticlesWithoutMovements) => {
        if (!responseData.error) {
          this.dataSource.data = responseData.response;
        } else {
          this.resourcesService.openSnackBar(`${responseData.msg}`);
        }
      }, async (httpErrorResponse: HttpErrorResponse) => {
        let response = this.resourcesService.checkErrors(httpErrorResponse);
        if (response !== null && response.reload) {
          await this.getArticlesWithoutMovements();
        }
      } 
    );
    this.isLoading = false;
  }

  public setTime (TYPE: 'Y' | 'M' | 'D', NUMBER_TIME: number) {
    if (NUMBER_TIME === 1) {
      switch (TYPE) {
        case 'Y':
          return `${NUMBER_TIME} año`;
        case 'M':
          return `${NUMBER_TIME} mes`;
        case 'D':
          return `${NUMBER_TIME} día`;
      }
    } else {
      switch (TYPE) {
        case 'Y':
          return `${NUMBER_TIME} años`;
        case 'M':
          return `${NUMBER_TIME} meses`;
        case 'D':
          return `${NUMBER_TIME} días`;
      }
    }
  }
}
