import {Component, OnInit} from '@angular/core';
import {BaseComponent} from '../../share/base.component';
import {Router} from '@angular/router';
import {MdSnackBar, MdDialog} from '@angular/material';
import {AdminService} from '../_service/admin.service';
import {UserInfoDialogComponent} from './userInfo.component';
import {Select} from '../../share/_model/select';
import {User} from '../_model/user';
import {DeleteUserComponent} from './delUser.component';
import {Search} from '../_model/search';


@Component({
  selector: 'app-admin',
  templateUrl: '../_views/admin.component.html',
  styleUrls: ['../_views/admin.component.scss', '../../share/e-home.scss']
})
export class AdminComponent extends BaseComponent implements OnInit {
  heightAdmin: string;
  roles: Array<Select>;
  isLoading: boolean;
  users: Array<User>;
  userIndex: number;
  user: User;
  isDeleted: boolean;
  search: Search;

  constructor(protected router: Router,
              protected mdSnackBar: MdSnackBar,
              private adminService: AdminService,
              public dialog: MdDialog) {
    super(router, mdSnackBar);
    this.heightAdmin = (this.height - 100) + 'px';
    this.userIndex = -1;
  }

  ngOnInit() {
    this.isLoading = false;
    this.search = new Search();
    this.initRole();
    this.loadListUser(null, null, null);
  }

  initRole() {
    this.roles = [];
    this.roles[0] = new Select('ADMIN', 'Administrator');
    this.roles[1] = new Select('CON_PRO', 'Content provider');
    this.roles[2] = new Select('USER', 'Viewer user');
  }


  loadListUser(role: string, email: string, name: string) {
    this.isLoading = true;
    this.adminService.getUsers(role, email, name).subscribe(res => {
      if (res.status === 'OK') {
        this.users = res.response;
        if (this.users.length > 0) {
          this.userIndex = 0;
          this.user = this.users[0];
        }
      } else {
        console.error('Server error');
        this.processStatusError(res.errors);
      }
      this.isLoading = false;
    });
  }

  chooseUser(index, user) {
    this.userIndex = index;
    this.user = user;
    if (this.user.deleted) {
      this.isDeleted = true;
    } else {
      this.isDeleted = false;
    }
  }

  openDialogUserInfo() {
    const dialogRef = this.dialog.open(UserInfoDialogComponent, {disableClose: true});
    dialogRef.afterClosed().subscribe(res => {
      if (res === 'OK') {
        this.loadListUser(null, null, null);
      }
    });
  }

  openDialogUpdateEdit() {
    if (this.user.userId) {
      const dialogUpdate = this.dialog.open(UserInfoDialogComponent, {disableClose: true, data: this.user});
      dialogUpdate.afterClosed().subscribe(res => {
        if (res === 'OK') {
          this.loadListUser(null, null, null);
        }
      });
    }
  }

  openDialogDelUser() {
    if (this.user.userId) {
      const dialogDelete = this.dialog.open(DeleteUserComponent, {disableClose: true, data: this.user});
      dialogDelete.afterClosed().subscribe(res => {
        if (res === 'OK') {
          this.loadListUser(null, null, null);
        }
      });
    }
  }

  filterListUser() {
    this.loadListUser(this.search.role, this.search.email, this.search.name);
  }
}
