import {
  AfterViewInit,
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  OnDestroy,
  OnInit,
  ViewChild,
  ViewEncapsulation,
} from '@angular/core';

import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { map, merge, Observable, Subject, switchMap, takeUntil } from 'rxjs';
import { fuseAnimations } from '@fuse/animations';
import { FuseConfirmationService } from '@fuse/services/confirmation';

import { Loosing } from '../models/loosing';

import { LoosingService } from '../services/loosing.service';
import { Router } from '@angular/router';
import { MatTableDataSource } from '@angular/material/table';
import { LoosingReport } from '../models/loosing-report';

@Component({
  selector: 'loosing-view-02',
  templateUrl: './view-02.component.html',
  styles: [
    /* language=SCSS */
    `
      .loosing-view-02-grid {
        grid-template-columns: 60px auto 40px;

        @screen sm {
          grid-template-columns: 40px 70px 70px 70px 70px 100px;
        }

        @screen md {
          grid-template-columns: 40px 70px 70px 70px 70px 100px;
        }

        @screen lg {
          grid-template-columns: 40px 70px 70px 70px 70px 100px;
        }
      }
    `,
  ],
  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush,
  animations: fuseAnimations,
})
export class View02Component implements OnInit, AfterViewInit, OnDestroy {
  @ViewChild(MatPaginator) private _paginator!: MatPaginator;
  @ViewChild(MatSort) private _sort!: MatSort;

  loosings$!: Observable<Loosing[] | undefined>;

  isLoading = false;

  loosingDataSource: MatTableDataSource<Loosing> = new MatTableDataSource();

  private _unsubscribeAll: Subject<any> = new Subject<any>();

  displayedTableColumns: string[] = [
    'userId',
    'userCode',
    'casinoWinLoss',
    'casinoRolling',
    'casinoProfitLoss',
    'casinoLoosing',
    'casinoSelf',
    'casinoBottom',
    'casinoCalculate',
    'slotWinLoss',
    'slotRolling',
    'slotSelf',
    'slotProfitLoss',
    'slotLoosing',
    'slotBottom',
    'slotCalculate',
    'totalCalculate',
  ];

  /**
   * Constructor
   */
  constructor(
    private _changeDetectorRef: ChangeDetectorRef,
    private _fuseConfirmationService: FuseConfirmationService,
    private _loosingService: LoosingService,
    private _router: Router
  ) {}

  // -----------------------------------------------------------------------------------------------------
  // @ Lifecycle hooks
  // -----------------------------------------------------------------------------------------------------

  /**
   * On init
   */
  ngOnInit(): void {
    // Get the products
    this._loosingService.loosing$
      .pipe(takeUntil(this._unsubscribeAll))
      .subscribe((loosingReport: LoosingReport[] | undefined) => {
        if (!loosingReport) {
          return;
        }

        this.loosingDataSource.data = loosingReport;
        // Mark for check
        this._changeDetectorRef.markForCheck();
      });
  }

  /**
   * After view init
   */
  ngAfterViewInit(): void {}

  /**
   * On destroy
   */
  ngOnDestroy(): void {
    // Unsubscribe from all subscriptions
    this._unsubscribeAll.next(null);
    this._unsubscribeAll.complete();
  }

  // -----------------------------------------------------------------------------------------------------
  // @ Public methods
  // -----------------------------------------------------------------------------------------------------

  /* viewUserDetail(id: string): void {
    let url: string = 'member/user/' + id;
    this.router.navigateByUrl(url);
  } */
  // -----------------------------------------------------------------------------------------------------
  // @ Private methods
  // -----------------------------------------------------------------------------------------------------

  /**
   * Create product
   */
  __createProduct(): void {}

  __onClickPrev(event: MouseEvent): void {
    this._router.navigateByUrl('/report/loosing');
  }

  /**
   * Toggle product details
   *
   * @param productId
   */
  __toggleDetails(productId: string): void {}

  /**
   * Track by function for ngFor loops
   *
   * @param index
   * @param item
   */
  __trackByFn(index: number, item: any): any {
    return item.id || index;
  }
}
