import { EnumerationType } from './../model/common-model';
import { ServiceBase } from './../data/service-base';
import { Predicate, FilterQueryOp } from 'breeze-client';

export class EnumerationTypeService extends ServiceBase<EnumerationType> {
  public category: number;

  constructor(category: number) {
    super('EnumerationType');
    this.gridDataSource.queryParameters = { category: category, onlyActifs: true };
    this.category = category;
  }

  public createEntity(initialValues?: Partial<EnumerationType>, sync?: boolean) {
    initialValues ??= {};
    // Force category
    initialValues.category = this.category;
    // Active is true UNLESS set specifically to false
    initialValues.active = initialValues.active === false ? false : true;
    // Default is false UNLESS set specifically to true
    initialValues.default = initialValues.default === true ? true : false;
    // Set default colors (white text on gray background)
    initialValues.textColor = initialValues.textColor || '#ffffff';
    initialValues.backgroundColor = initialValues.backgroundColor || '#6d7277';
    return super.createEntity(initialValues, sync);
  }

  /**
   * Fetch the default enumeration by category
   */
  public async getDefault(): Promise<EnumerationType> {
    let predicate = this.gridDataSource.predicates;
    if (predicate == null) {
      predicate = new Predicate('default', FilterQueryOp.Equals, true);
    } else {
      predicate.and(new Predicate('default', FilterQueryOp.Equals, true));
    }
    return await this.firstEntity(predicate, this.gridDataSource.expands, this.gridDataSource.queryParameters);
  }

  /**
   * Get count of enumeration by category
   */
  public async getCount(predicate: Predicate = null): Promise<number> {
    if (predicate != null && this.gridDataSource.predicates != null) {
      predicate = predicate.and(this.gridDataSource.predicates);
    } else {
      predicate ??= this.gridDataSource.predicates;
    }
    return await super.getCount(predicate, this.gridDataSource.queryParameters);
  }

  /**
   * Fetch first enumeration by category
   */
  public async getFirst(): Promise<EnumerationType> {
    return await this.firstEntity(this.gridDataSource.predicates, this.gridDataSource.expands, this.gridDataSource.queryParameters);
  }

  /**
   * Fetch all actives enumerations by category
   */
  public async getAll(): Promise<Array<EnumerationType>> {
    return await this.getEntities(this.gridDataSource.predicates, this.gridDataSource.expands, this.gridDataSource.queryParameters);
  }

  /**
   * Fetch the enumeration by category and uniqueCode
   */
  public async getAnyByUniqueCode(uniqueCode: string): Promise<EnumerationType> {
    let predicate = this.gridDataSource.predicates;
    if (predicate == null) {
      predicate = new Predicate('uniqueCode', FilterQueryOp.Equals, uniqueCode);
    } else {
      predicate.and(new Predicate('uniqueCode', FilterQueryOp.Equals, uniqueCode));
    }
    return await this.firstEntity(predicate, this.gridDataSource.expands, this.gridDataSource.queryParameters);
  }
}
