import {
  ChangeDetectorRef,
  Component,
  OnInit,
  ViewChild,
  AfterViewInit,
} from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { MatPaginator, MatPaginatorIntl } from '@angular/material/paginator';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { ActivatedRoute, Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { lastValueFrom } from 'rxjs';
import { WorkflowService } from 'src/app/services/samwf/workflow.service';
import { WorkflowDetailAdvanceComponent } from './workflow-detail-advance/workflow-detail-advance.component';
import { ResourcesService } from '../../../services/resources/resources.service';
import { HttpErrorResponse } from '@angular/common/http';
@Component({
  selector: 'app-workflow-advance',
  templateUrl: './workflow-advance.component.html',
  styleUrls: ['./workflow-advance.component.css'],
})
export class WorkflowAdvanceComponent implements AfterViewInit {
  data_empty = false;

  displayedColumns: string[] = ['name', 'module', 'created_at', 'action'];
  advance: any[] = [];
  showProgressBar: boolean = false;
  public dataSource: MatTableDataSource<any>;

  public isLoading: boolean = true;
  public isLoadingForm: boolean = false;

  // ---- SmartPage ----
  public btnSmall: boolean = false;
  public card_size = 'card-size-normal mat-elevation-z8';

  loading = false;
  @ViewChild(MatPaginator) paginator!: MatPaginator;
  @ViewChild(MatSort) sort!: MatSort;

  constructor(
    private routerNavigate: ActivatedRoute,
    private router: Router,
    public dialog: MatDialog,
    private translate: TranslateService,
    private _samwfService: WorkflowService,
    private _snackBar: MatSnackBar,
    private _resourceService: ResourcesService
  ) {
    this.dataSource = new MatTableDataSource();
    this.getRequestUser();

    translate.setDefaultLang('es');
  }

  ngOnInit(): void {}

  backToWorkflows() {
    this.router.navigate(['workflow']);
  }

  updatePaginator() {
    this.translate.get('paginador').subscribe((data: any) => {
      this.dataSource.paginator = this.paginator;
      this.paginator._intl.itemsPerPageLabel = data[0];
      this.paginator._intl.firstPageLabel = data[1];
      this.paginator._intl.lastPageLabel = data[2];
      this.paginator._intl.nextPageLabel = data[3];
      this.paginator._intl.previousPageLabel = data[4];
    });
  }

  ngAfterViewInit(): void {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;

    if (window.innerWidth <= 721) {
      this.btnSmall = true;
      this.card_size = 'card-size-small';
    } else if (window.innerWidth > 721) {
      this.btnSmall = false;
      this.card_size = 'card-size-normal mat-elevation-z8';
    }

    this.translate.get('paginador').subscribe((data: any) => {
      this.dataSource.paginator = this.paginator;
      this.paginator._intl.itemsPerPageLabel = data[0];
      this.paginator._intl.firstPageLabel = data[1];
      this.paginator._intl.lastPageLabel = data[2];
      this.paginator._intl.nextPageLabel = data[3];
      this.paginator._intl.previousPageLabel = data[4];
    });
  }

  async openAdavanceRequest(request: any) {
    this.isLoadingForm = true;
    const dialogRef = this.dialog.open(WorkflowDetailAdvanceComponent, {
      data: request,
      width: '800px',
    });

    dialogRef.afterClosed().subscribe((result: any) => {});
    this.isLoadingForm = false;
  }

  private async getRequestUser() {
    this.isLoading = true;

    await lastValueFrom(
      this._samwfService.getRequestsAdvanceByUser('SAM001')
    ).then(
      (data: any) => {
        this.dataSource.data = data.response;
        console.log(data.response);

        this.advance = data.response;

        this.dataSource.data.length > 0
          ? (this.data_empty = false)
          : (this.data_empty = true);
      },
      (error: HttpErrorResponse) => {
        this._resourceService.checkErrors(error);
      }
    );
    this.isLoading = false;
  }

  public applyFilter(event: Event) {
    const filterValue = (event.target as HTMLInputElement).value;
    this.dataSource.filter = filterValue.trim().toLowerCase();
    if (this.dataSource.paginator) {
      this.dataSource.paginator.firstPage();
    }
  }

  public onResize(event: any) {
    if (event.target.innerWidth <= 721) {
      this.btnSmall = true;
      this.card_size = 'card-size-small';
    } else if (event.target.innerWidth > 721) {
      this.btnSmall = false;
      this.card_size = 'card-size-normal mat-elevation-z8';
    }
  }
}
