import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { OptionFilter } from '../../core';

@Component({
    selector: 'app-option-filter',
    templateUrl: './option-filter.component.html',
    styleUrls: [ './option-filter.component.scss' ]
})
export class OptionFilterComponent implements OnInit {

    @Input() private options: string[];
    @Input() private title: string;
    @Input() filter: OptionFilter;
    @Output() filterChange = new EventEmitter();

    constructor() {
        this.options = [];
    }

    ngOnInit() { }

    onChangeOptionFilter($event: any, option: string) {
        let selectedOption = this.filter.selectedOption ? this.filter.selectedOption : [];
        if ($event.checked) {
            if (selectedOption.indexOf(option.toLowerCase()) === -1) {
                selectedOption.push(option.toLowerCase());
            }
        } else {
            if (selectedOption.indexOf(option.toLowerCase()) !== -1) {
                selectedOption.splice(selectedOption.indexOf(option.toLowerCase()), 1);
            }
        }
        this.filter.selectedOption = selectedOption;
        this.filterChange.emit(this.filter);
    }

}
