import {
  AfterViewInit,
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  OnDestroy,
  OnInit,
  ViewChild,
  ViewEncapsulation,
} from '@angular/core';
import {
  FormBuilder,
  FormControl,
  FormGroup,
  Validators,
} from '@angular/forms';
import { MatCheckboxChange } from '@angular/material/checkbox';
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 { User } from 'app/modules/admin/member/user/models/user';
import { UserService } from 'app/modules/admin/member/user/services/user.service';
import { ActivatedRoute, Router } from '@angular/router';
import { CustomerTemplateService } from '../services/customer-template.service';
import { CustomerTemplate } from '../models/ customer-template';

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

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

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

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

  isLoading = false;
  searchInputControl = new FormControl();
  targetTemplateForm!: FormGroup;
  selectedUser?: User;

  targetTemplate?: CustomerTemplate | undefined;

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

  /**
   * Constructor
   */
  constructor(
    private _changeDetectorRef: ChangeDetectorRef,
    private _fuseConfirmationService: FuseConfirmationService,
    private _formBuilder: FormBuilder,
    private _customerTemplateService: CustomerTemplateService,
    private _userService: UserService,
    private _route: ActivatedRoute,
    private _router: Router
  ) {}

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

  /**
   * On init
   */
  ngOnInit(): void {
    this.targetTemplateForm = this._formBuilder.group({
      title: [''],
      order: [''],
      content: [''],
    });
    const idx = this._route.snapshot.params['id'];

    // Get the User
    this._customerTemplateService.customerTemplate$
      .pipe(takeUntil(this._unsubscribeAll))
      .subscribe((targetTemplate: CustomerTemplate | undefined) => {
        this.targetTemplate = targetTemplate;

        if (!this.targetTemplate) {
          return;
        }

        this.targetTemplateForm.patchValue({
          title: this.targetTemplate.title,
          content: this.targetTemplate.content,
          order: this.targetTemplate.order,
        });
        // 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 {}
  __onClickeCancel(): void {
    let url: string = 'board/customer-template/';
    this._router.navigateByUrl(url);
  }
  __onClickReditBtn(targetTemplate: CustomerTemplate | undefined): void {
    console.log('click: ', targetTemplate?.id);
  }

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