/*
 * Copyright (C) 2019 Robert Bosch GmbH Copyright (C) 2019 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { Component, OnInit } from '@angular/core';
import { MatDialogRef } from '@angular/material/dialog';
import { NotificationTypes } from '../../notification/notification.models';
import { BciMessagesFilterService } from '../bci-messages.filter.service';

@Component({
  selector: 'bci-bci-messages-filter',
  templateUrl: './bci-messages-filter.component.html',
  styleUrls: ['./bci-messages-filter.component.scss'],
})
export class BciMessagesFilterComponent implements OnInit {
  filter = [];

  filterTypes = {
    label: 'Type',
    active: () => this.filter.length > 0,
    deactivate: () => (this.filter.length = 0),
  };
  filters = [this.filterTypes];

  chosenFilter = this.filters[0];

  types = [
    NotificationTypes.Success,
    NotificationTypes.Information,
    NotificationTypes.Warning,
    NotificationTypes.Error,
  ];

  constructor(
    private dialogRef: MatDialogRef<BciMessagesFilterComponent, boolean>,
    private filterService: BciMessagesFilterService
  ) {}

  ngOnInit() {
    this.filter = this.filterService.filter;
  }

  applyFilters() {
    this.filterService.filter = this.filter;
    this.dialogRef.close(true);
  }

  cancel() {
    this.dialogRef.close(false);
  }

  isTypeSelected(type): boolean {
    return this.filter.indexOf(type) !== -1;
  }

  toggleType(type): void {
    const index = this.filter.indexOf(type);
    if (index > -1) {
      this.filter.splice(index, 1);
      return;
    }
    this.filter.push(type);
  }
}
