import { Component, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Subject } from 'rxjs';
import { takeUntil, map } from 'rxjs/operators';

import { fuseAnimations } from 'src/@fuse/animations';
import { User } from 'src/modules/user/model/user.model';
import { UserService } from 'src/modules/user/service/user.service';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-pages-users-user-detail',
  templateUrl: './detail.component.html',
  styleUrls: ['./detail.component.scss'],
  encapsulation: ViewEncapsulation.None,
  animations: fuseAnimations
})
export class DetailComponent implements OnInit, OnDestroy {
  user: User;
  userForm: FormGroup;

  // Private
  private _unsubscribeAll: Subject<any>;

  /**
   * Constructor
   */
  constructor(
    private userService: UserService,
    private activatedRoute: ActivatedRoute,
    private formBuilder: FormBuilder
  ) {
    // Set the private defaults
    this._unsubscribeAll = new Subject();
  }

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

  /**
   * On init
   */
  ngOnInit(): void {
    this.activatedRoute.data.subscribe(data => {
      this.user = data.user;

      this.initializeForm();
    });
  }

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

  // -----------------------------------------------------------------------------------------------------
  // @ Private methods
  // -----------------------------------------------------------------------------------------------------
  initializeForm(): void {
    this.userForm = this.formBuilder.group({
      username: this.user.username,
      password: '',
      password_confirm: ''
    });
  }

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

  /**
   * Update status
   */
  updateStatus(): void {
    // const newStatusId = Number.parseInt(this.statusForm.get('newStatus').value);
    // if (!newStatusId) {
    //   return;
    // }
    // const newStatus = this.orderStatuses.find(status => {
    //   return status.id === newStatusId;
    // });
    // newStatus['date'] = new Date().toString();
    // this.order.status.unshift(newStatus);
  }
}
