import { Component, Input } from '@angular/core';
import { AngularMaterialModule } from '../../angular-material/angular-material.module';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { MatTableModule, MatTableDataSource } from '@angular/material/table';
import { MatSelectModule, MatSelectChange } from '@angular/material/select';
import { RankedUser, User } from '../../auth/model/user.model';

@Component({
  selector: 'app-table-search',
  standalone: true,
  imports: [
    AngularMaterialModule,
    CommonModule,
    FormsModule,
    MatSelectModule,
    MatTableModule,
  ],
  templateUrl: './table-search.component.html',
  styleUrl: './table-search.component.css',
})
export class TableSearchComponent {
  @Input() dataSource = new MatTableDataSource<RankedUser>([]);
  @Input() isSearchActive = false;
  company: string[] = ['All', 'Atos', 'Eviden'];
  companyDefaultValue = 'All';
  companySelect = {
    name: 'company',
    options: this.company,
    defaultValue: this.companyDefaultValue,
  };

  scoreOperation: string[] = ['weniger als', 'mehr als'];
  scoreOperationDefaultValue = 'weniger als';
  scoreSelect = {
    name: 'operation',
    options: this.scoreOperation,
    defaultValue: this.scoreOperationDefaultValue,
  };

  filteredValues = {
    name: '',
    score: { value: null, operation: 'weniger als' },
    company: '',
  };

  ngOnInit() {
    this.dataSource.filterPredicate = this.customFilterPredicate();
  }

  customFilterPredicate() {
    const myFilterPredicate = (data: User, filter: string): boolean => {
      let searchString = JSON.parse(filter);
      let name =
        (data.firstName ? data.firstName : '') +
        (data.lastName ? data.lastName : '');
      let nameResult = name
        .toString()
        .trim()
        .toLowerCase()
        .includes(searchString.name.trim().toLowerCase());

      let companyResult =
        searchString.company == 'All' ||
        (data.email ? data.email : '')
          .toString()
          .trim()
          .toLowerCase()
          .includes(searchString.company.trim().toLowerCase());
      let scoreResult = true;
      if (searchString.score.value != null && data.wins != undefined) {
        switch (searchString.score.operation) {
          case 'weniger als':
            scoreResult = data.wins <= searchString.score.value;
            break;
          case 'mehr als':
            scoreResult = data.wins >= searchString.score.value;
            break;
        }
      }

      return nameResult && companyResult && scoreResult;
    };
    return myFilterPredicate;
  }

  applyFilter() {
    this.dataSource.filter = JSON.stringify(this.filteredValues);
    this.dataSource.paginator?.firstPage();
  }

  applyNameFilter(filter: any) {
    this.filteredValues.name = filter;
    this.applyFilter();
  }

  applySelectFilter(event: MatSelectChange) {
    this.filteredValues.company = event.value;
    this.applyFilter();
  }

  applyScoreFilter(event: MatSelectChange) {
    this.filteredValues.score.operation = event.value;
    this.applyFilter();
  }
}
