import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {AgGridComponent} from '../../../components/ag-grid-component';
import {ModalComponent} from '../../../components/modal/modal.component';
import {Utils} from '../../../utils/utils';
import {Const} from '../../../utils/const';
import {AppService} from '../../../services/app.service';
import {CommonService} from '../../../services/common.service';
import {IProduction} from '../../../model/entities/production';
import {IProductionContact} from '../../../model/entities/production-contact';
import {
    ProductionContactPersonDetailComponent
} from '../production-view/production-contact-person-detail/production-contact-person-detail.component';
import {ProductionService} from '../../../services/production.service';

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

export class ProductionsContactListComponent extends AgGridComponent implements OnInit {

    @Output() shortcut: EventEmitter<IProduction> = new EventEmitter<IProduction>();
    @ViewChild('modalProductionsContactPersonDetailList', { static: true }) modalProductionsContactPersonDetailList: ModalComponent;
    @ViewChild('productionContactPersonDetailList', { static: true }) productionContactPersonDetailList: ProductionContactPersonDetailComponent;

    public productions: IProduction[];
    public productionsById: {};
    private productionContacts: IProductionContact[];
    public productionContactsById: {};
    public hasAdminRights: boolean;
    public hasExportRights: boolean;

    constructor(public appService: AppService, private productionService: ProductionService, private commonService: CommonService) {
        super();
    }

    ngOnInit() {
        this.hasAdminRights = this.appService.userHasRole(Const.USER_ROLE_ADMIN);
        this.hasExportRights = this.appService.userHasRole(Const.USER_ROLE_ADMIN);
        this.productions = [];
        this.productionsById = {};
        this.productionContacts = [];
        this.productionContactsById = {};

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

        this.columnDefs = [
            {headerName: 'ID', field: 'id', width: 60 },
            {headerName: 'Produzent ID', field: 'production_id', width: 100 },
            {headerName: 'Produzent', field: 'production' },
            {headerName: 'Vorname', field: 'firstname' },
            {headerName: 'Nachname', field: 'lastname' },
            {headerName: 'Abteilung', field: 'department' },
            {headerName: 'E-Mail Adresse', field: 'email', cellRenderer: this.emailRenderer },
            {headerName: 'Telefon', field: 'phone_no', cellRenderer: this.phoneRenderer },
            {headerName: 'Mobil', field: 'mobile_no', cellRenderer: this.phoneRenderer },
            {headerName: 'Fax', field: 'fax_no' },
            {headerName: 'Bemerkungen', field: 'comment' }
        ];

        this.rowSelection = 'single';
    }

    /**
     * Sets data
     * @param productions
     * @param productionContacts
     */
    public setData(productions: IProduction[], productionContacts: IProductionContact[]): void {
        this.productions = productions;
        this.productionsById = Utils.getSortedObjFromArray(this.productions, 'id');
        this.productionContacts = productionContacts;
        this.productionContactsById = Utils.getSortedObjFromArray(this.productionContacts, 'id');
        this.generateEntries();
    }

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

        for (let i = 0; i < this.productionContacts.length; i++) {
            this.rowData.push({
                id: this.productionContacts[i].id,
                production_id: this.productionContacts[i].production_id,
                production: this.productionsById[this.productionContacts[i].production_id].name,
                firstname: this.productionContacts[i].firstname,
                lastname: this.productionContacts[i].lastname,
                department: this.productionContacts[i].department,
                email: this.productionContacts[i].email,
                phone_no: this.productionContacts[i].phone_no,
                mobile_no: this.productionContacts[i].mobile_no,
                fax_no: this.productionContacts[i].fax_no,
                comment: this.productionContacts[i].comment
            });
        }
    }

    /**
     * Row is clicked
     * @param e
     */
    public rowClicked(e: any) {
        this.modalProductionsContactPersonDetailList.openModal();
        this.productionContactPersonDetailList.setData(this.productionsById[e.data.production_id], Utils.deepClone(this.productionContactsById[e.data.id]) as IProductionContact);
    }

    /**
     * Edit production contact
     * @param productionContact
     */
    public editProductionContactFunction(productionContact: IProductionContact): void {
        this.productionService.apiEditProductionContact(productionContact).subscribe(
            data => {
                this.productionContactPersonDetailList.updateData(data.result_data as IProduction);
            },
            error => {}
        );
    }

    /**
     * Delete production contact
     * @param productionId
     */
    public deleteProductionContactFunction(productionId: number): void {
        this.productionService.apiDeleteProductionContact(productionId).subscribe(
            data => {
                this.modalProductionsContactPersonDetailList.closeModal();
            },
            error => {}
        );
    }

    /**
     * Shortcut to production detail
     * @param production
     */
    public shortcutFunction(production: IProduction): void {
        this.modalProductionsContactPersonDetailList.closeModal();
        this.shortcut.emit(production);
    }

    /**
     * Export production contacts pdf
     */
    public exportProductionContactsPdf(): void {
        this.commonService.apiExportContactsPdf('Produktion_Kontakte', 'production');
    }

    /**
     * Export production xmas pdf
     */
    public exportXmasPdf(): void {
        this.commonService.apiExportXmasPdf('Produzent_Weihnachten', 'production');
    }
}
