import {
  AfterViewInit,
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  OnDestroy,
  OnInit,
  ViewChild,
  ViewEncapsulation,
} from '@angular/core';
import {
  FormBuilder,
  FormControl,
  FormGroup,
  Validators,
} from '@angular/forms';

import { MatTableDataSource } from '@angular/material/table';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import {
  debounceTime,
  map,
  merge,
  Observable,
  Subject,
  switchMap,
  takeUntil,
} from 'rxjs';
import { fuseAnimations } from '@fuse/animations';
import { FuseConfirmationService } from '@fuse/services/confirmation';
import { EvoService } from '../services/evo.service';
import { MatSelectChange } from '@angular/material/select';

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

        @screen sm {
          grid-template-columns: 60px auto 60px 72px;
        }

        @screen md {
          grid-template-columns: 60px 60px auto 112px 72px;
        }

        @screen lg {
          grid-template-columns: 60px 60px auto 112px 96px 96px 72px;
        }
      }
    `,
  ],
  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush,
  animations: fuseAnimations,
})
export class ListComponent implements OnInit, AfterViewInit, OnDestroy {
  @ViewChild(MatPaginator) private _paginator!: MatPaginator;
  @ViewChild(MatSort) private _sort!: MatSort;

  amountScopeOptions = [
    { idx: 0, value: '1,000~5,000,000(기본유저)' },
    { idx: 1, value: '1,000~3,000,000(소액유저)' },
    { idx: 2, value: '1,000~20,000,000(고액유저)' },
  ];
  evoSettingTableColumns: string[] = [
    'level',
    'amountScope',
    'description',
    'updateBtn',
  ];

  botaSettingTableColumns: string[] = [
    'level',
    'amountScope',
    'description',
    'updateBtn',
  ];

  evoSettingDataSource: MatTableDataSource<any> = new MatTableDataSource();
  botaSettingDataSource: MatTableDataSource<any> = new MatTableDataSource();

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

  /**
   * Constructor
   */
  constructor(
    private _changeDetectorRef: ChangeDetectorRef,
    private _fuseConfirmationService: FuseConfirmationService,
    private _formBuilder: FormBuilder,
    private _evoService: EvoService
  ) {}

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

  /**
   * On init
   */
  ngOnInit(): void {
    // Get the products
    this._evoService.evoSetting$
      .pipe(takeUntil(this._unsubscribeAll))
      .subscribe((evoSetting: any | undefined) => {
        this.evoSettingDataSource = evoSetting.evo;
        this.botaSettingDataSource = evoSetting.bota;
        // 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
  // -----------------------------------------------------------------------------------------------------

  // -----------------------------------------------------------------------------------------------------
  // @ Private methods
  // -----------------------------------------------------------------------------------------------------

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

  /**
   * 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;
  }

  __onSelectionAmountScope(event: MatSelectChange): void {}
}
