import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { AbstractControl } from '@angular/forms';
import { AccessControl } from 'accesscontrol';
import { firstValueFrom, Observable, Subject, timer } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class SysService {


  constructor(private http: HttpClient) {

  }

  /**
   * 校验用户名
   * @param id 
   * @returns 
   */
  validateUsername(id: string = null) {
    return async (control: AbstractControl | string): Promise<any> => {
      let username = control instanceof AbstractControl ? control.value : control;
      try {
        await firstValueFrom(this.http.post('/napi/web/user/check/username', { username, id }));
        return null;
      } catch (err) {
        return { error: err.error?.message || err.message };
      }
    }
  }

  validatePhone(id: string = null) {
    return async (control: AbstractControl | string): Promise<any> => {
      const phone = control instanceof AbstractControl ? control.value : control;
      try {
        await firstValueFrom(this.http.post('/napi/web/user/check/phone', { phone, id }));
        return null;
      } catch (err) {
        return { error: err.error?.message || err.message };
      }
    }
  }

  validateEmail(id: string = null) {
    return async (control: AbstractControl | string): Promise<any> => {
      const email = control instanceof AbstractControl ? control.value : control;
      try {
        await firstValueFrom(this.http.post('/napi/web/user/check/email', { email, id }));
        return null;
      } catch (err) {
        return { error: err.error?.message || err.message };
      }
    }
  }


  roleOptions(): Promise<any> {
    return firstValueFrom(this.http.get(`/napi/web/role/options`));
  }

  roleOptions2(): Promise<any> {
    return firstValueFrom(this.http.get(`/napi/web/role/options2`));
  }

  epgOptions(): Promise<any> {
    return firstValueFrom(this.http.get(`/napi/web/epg/options`));
  }

  createUser(data: any): Promise<any> {
    return firstValueFrom(this.http.post('/napi/web/user/', data));
  }


  getUser(id: string): Promise<any> {
    return firstValueFrom(this.http.get(`/napi/web/user/${id}`));
  }

  updateUser(id: string, data: any): Observable<any> {
    return this.http.patch(`/napi/web/user/${id}`, data);
  }

  /**
   * disable user operation
   * @param id 
   * @returns 
   */
  disableUser(id: string) {
    return (() => {
      return this.updateUser(id, { disabled: true });
    }).bind(this);
  }

  /**
   * enable user operaiton
   * @param id 
   * @returns 
   */
  enableUser(id: string) {
    return (() => {
      return this.updateUser(id, { disabled: false });
    }).bind(this);
  }


  /**
   * delete session operation
   * @param sesid 
   * @returns 
   */
  deleteSession(sesid: string) {
    return (() => {
      return this.http.delete(`/napi/web/user/session/${sesid}`);
    }).bind(this);
  }

  /**
   * reset user's password.
   * @param id 
   * @returns 
   */
  resetPassword(id: string) {
    return (() => {
      return this.http.patch(`/napi/web/user/${id}/password`, {});
    }).bind(this);
  }


  //////////role-related//////////

  getRole(roleId: string) {
    return this.http.get(`/napi/web/role/${roleId}`);
  }

  createRole(data: any) {
    return this.http.post(`/napi/web/role/`, data);
  }

  updateRole(roleId: string, data: any) {
    return this.http.patch(`/napi/web/role/${roleId}`, data);
  }

  deleteRole(roleId: string) {
    return this.http.delete(`/napi/web/role/${roleId}`);
  }

  /**
   * For op button
   * @param roleId 
   * @returns 
   */
  deleteRoleOp(roleId: string) {
    return (() => {
      return this.deleteRole(roleId);
    }).bind(this);
  }

  getGrants() {
    return this.http.get(`/napi/web/auth/grants`);
  }

  getAuthConfig() {
    return this.http.get(`/napi/web/role/auth/config`);
  }

}
