import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {AgGridComponent} from '../../../components/ag-grid-component';
import {AppService} from '../../../services/app.service';
import {Const} from '../../../utils/const';
import {isNull} from 'util';
import {IService} from '../../../model/entities/service';

@Component({
  selector: 'app-service-list',
  templateUrl: './services-list.component.html',
  styleUrls: ['./services-list.component.scss']
})

export class ServicesListComponent extends AgGridComponent implements OnInit {

    @Output() rowIsClicked: EventEmitter<any> = new EventEmitter<any>();
    @Output() createNewService: EventEmitter<boolean> = new EventEmitter<boolean>();

    private services: IService[];
    public rowClassRules: {};
    public hasEditRights: boolean;

    constructor(public appService: AppService) {
        super();
    }

    ngOnInit() {
        this.hasEditRights =
            this.appService.userHasRole(Const.USER_ROLE_TECHNIQUE) ||
            this.appService.userHasRole(Const.USER_ROLE_PRODUCTION) ||
            this.appService.userHasRole(Const.USER_ROLE_SERVICE);

        this.services = [];
        this.defaultColDef = {
            resizable: true,
            sortable: true
        };

        this.columnDefs = [
            {headerName: 'ID', field: 'service_id', width: 60 },
            {headerName: 'Status', field: 'active', width: 90 },
            {headerName: 'Name', field: 'service_name' },
            {headerName: 'Name Zusatz', field: 'service_name_additional' },
            {headerName: 'Alte PLP Kennung', field: 'old_plp_id' },
            {headerName: 'Straße', field: 'street' },
            {headerName: 'Hausnummer', field: 'street_no' },
            {headerName: 'PLZ', field: 'zip' },
            {headerName: 'Ort', field: 'city' },
            {headerName: 'Internetadresse', field: 'url', cellRenderer: this.urlRenderer },
            {headerName: 'Bemerkungen (Allgemein)', field: 'comment' }
        ];

        this.rowSelection = 'single';

        this.rowClassRules = {
            inactive(params) {
                return !params.data.active_state;
            }
        };
    }

    /**
     * Sets data
     * @param services
     */
    public setData(services: IService[]): void {
        this.services = services;
        this.generateEntries();
    }

    /**
     * Generates AG grid entries for service grid
     */
    public generateEntries(): void {
        this.rowData = [];

        if (!isNull(this.services)) {
            for (let i = 0; i < this.services.length; i++) {
                this.rowData.push({
                    service_id: this.services[i].id,
                    active_state: this.services[i].active,
                    active: this.services[i].active ? 'Aktiv' : 'Inaktiv',
                    service_name: this.services[i].name,
                    service_name_additional: this.services[i].name_additional,
                    old_plp_id: this.services[i].old_plp_id,
                    street: this.services[i].street,
                    street_no: this.services[i].street_no,
                    zip: this.services[i].zip,
                    city: this.services[i].city,
                    url: this.services[i].url,
                    comment: this.services[i].comment,
                });
            }
        }

    }

    /**
     * Row is clicked
     * @param e
     */
    public rowClicked(e: any) {
        this.rowIsClicked.emit(e);
    }

    /**
     * OnClick Create service
     */
    public createService(): void {
        this.createNewService.emit(true);
    }

}
