import {Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {AgGridComponent} from '../../components/ag-grid-component';
import {AppService} from '../../services/app.service';
import {MeetingService} from '../../services/meeting.service';
import {TabsComponent} from '../../components/tabs/tabs.component';
import {ModalComponent} from '../../components/modal/modal.component';
import {Subscription} from 'rxjs';
import {OperatorViewComponent} from './operator-view/operator-view.component';
import {OperatorsListComponent} from './operators-list/operators-list.component';
import {OperatorsContactListComponent} from './operators-contact-list/operators-contact-list.component';
import {IOperator} from '../../model/entities/operator';
import {IOperatorContact} from '../../model/entities/operator-contact';
import {Factory} from '../../factory/factory';
import {OperatorDataEditComponent} from './operator-view/operator-data-edit/operator-data-edit.component';
import {OperatorService} from '../../services/operator.service';
import {IOperatorNote} from '../../model/entities/operator-note';
import {IOperatorMeeting} from '../../model/entities/operator-meeting';
import {isNull} from 'util';

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

export class OperatorsComponent extends AgGridComponent implements OnInit, OnDestroy {

    @ViewChild('tabsCM', { static: true }) tabsCM: TabsComponent;
    @ViewChild('modalOperator', { static: true }) modalOperator: ModalComponent;
    @ViewChild('operatorView', { static: true }) operatorView: OperatorViewComponent;
    @ViewChild('operatorsList', { static: true }) operatorsList: OperatorsListComponent;
    @ViewChild('operatorsContactList', { static: true }) operatorsContactList: OperatorsContactListComponent;
    @ViewChild('modalOperatorDataEdit', { static: true }) modalOperatorDataEdit: ModalComponent;
    @ViewChild('operatorDataEdit', { static: true }) operatorDataEdit: OperatorDataEditComponent;
    @ViewChild('headline', { static: true }) headline: ElementRef;
    @ViewChild('content', { static: true }) content: ElementRef;

    private operators: IOperator[];
    private operatorsSub: Subscription;
    private operatorsContactsSub: Subscription;
    public operatorContacts: IOperatorContact[];

    constructor(private appService: AppService, private operatorService: OperatorService, private meetingService: MeetingService) {
        super();
        this.operatorService.apiGetOperatorData();
    }

    ngOnInit() {
        this.operatorContacts = [];

        this.operatorsSub = this.operatorService.getOperators$().subscribe(
            data => {
                this.operators = data;
                this.setComponentData();
            }
        );

        this.operatorsContactsSub = this.operatorService.getOperatorContacts$().subscribe(
            data => {
                this.operatorContacts = data;
                this.setComponentData();
            }
        );
    }

    /**
     * Set component data after all data has been received
     */
    private setComponentData() {
        if (!isNull(this.operators) && !isNull(this.operatorContacts)) {
            this.operatorsList.setData(this.operators);
            this.operatorsContactList.setData(this.operators, this.operatorContacts);
        }
    }

    /**
     * Operator row is clicked
     * @param e
     */
    public rowClickedOperator(e: any) {
        this.operatorService.apiGetOperatorFull(e.data.operator_id).subscribe(
            data => {
                this.operatorView.setData(data.result_data as IOperator);
                this.modalOperator.openModal();
            },
            error => {}
        );
    }

    /**
     * OnClick Create operator
     */
    public createOperator(): void {
        this.modalOperatorDataEdit.openModal();
        this.operatorDataEdit.setData(Factory.getEmptyOperator());
    }

    /**
     * Creates operator
     * @param operator
     */
    public createOperatorFunction(operator: IOperator): void {
        this.operatorService.apiCreateOperator(operator).subscribe(
            data => {
                this.modalOperatorDataEdit.closeModal();
            },
            error => {}
        );
    }

    /**
     * Saves operator
     * @param operator
     */
    public editOperatorFunction(operator: IOperator): void {
        this.operatorService.apiEditOperator(operator).subscribe(
            data => {
                this.operatorView.setData(data.result_data as IOperator);
            },
            error => {}
        );
    }

    /**
     * Creates operator contact
     * @param operatorContact
     */
    public createOperatorContactFunction(operatorContact: IOperatorContact): void {
        this.operatorService.apiCreateOperatorContact(operatorContact).subscribe(
            data => {
                this.operatorView.setData(data.result_data as IOperator);
            },
            error => {}
        );
    }

    /**
     * Edit operator contact
     * @param operatorContact
     */
    public editOperatorContactFunction(operatorContact: IOperatorContact): void {
        this.operatorService.apiEditOperatorContact(operatorContact).subscribe(
            data => {
                this.operatorView.setData(data.result_data as IOperator, true);
            },
            error => {}
        );
    }

    /**
     * Deletes operator contact
     * @param operatorId
     */
    public deleteOperatorContactFunction(operatorId: number): void {
        this.operatorService.apiDeleteOperatorContact(operatorId).subscribe(
            data => {
                this.operatorView.setData(data.result_data as IOperator);
            },
            error => {}
        );
    }

    /**
     * Creates operator note entry
     * @param operatorNote
     */
    public createOperatorNoteFunction(operatorNote: IOperatorNote): void {
        this.operatorService.apiCreateOperatorNote(operatorNote).subscribe(
            data => {
                this.operatorView.setData(data.result_data as IOperator);
            },
            error => {}
        );
    }

    /**
     * Edits operator note entry
     * @param operatorNote
     */
    public editOperatorNoteFunction(operatorNote: IOperatorNote): void {
        this.operatorService.apiEditOperatorNote(operatorNote).subscribe(
            data => {
                this.operatorView.setData(data.result_data as IOperator, true);
            },
            error => {}
        );
    }

    /**
     * Deletes operator note entry
     * @param operatorNoteId
     */
    public deleteOperatorNoteFunction(operatorNoteId: number): void {
        this.operatorService.apiDeleteOperatorNote(operatorNoteId).subscribe(
            data => {
                this.operatorView.setData(data.result_data as IOperator);
            },
            error => {}
        );
    }

    /**
     * Creates operator meeting entry
     * @param operatorMeeting
     */
    public createOperatorMeetingFunction(operatorMeeting: IOperatorMeeting): void {
        this.meetingService.apiCreateOperatorMeeting(operatorMeeting).subscribe(
            data => {
                this.operatorView.setData(data.result_data as IOperator);
            },
            error => {}
        );
    }

    /**
     * Edits operator meeting entry
     * @param operatorMeeting
     */
    public editOperatorMeetingFunction(operatorMeeting: IOperatorMeeting): void {
        this.meetingService.apiEditOperatorMeeting(operatorMeeting).subscribe(
            data => {
                this.operatorView.setData(data.result_data as IOperator, true);
            },
            error => {}
        );
    }

    /**
     * Deletes operator meeting entry
     * @param operatorMeetingId
     */
    public deleteOperatorMeetingFunction(operatorMeetingId: number): void {
        this.meetingService.apiDeleteOperatorMeeting(operatorMeetingId).subscribe(
            data => {
                this.operatorView.setData(data.result_data as IOperator);
            },
            error => {}
        );
    }

    /**
     * Edit report after meeting has started
     * @param reportValues
     */
    public editOperatorMeetingReportFunction(reportValues: any): void {
        const meetingId: number = reportValues.id;
        const report: string = reportValues.report;
        this.meetingService.apiEditOperatorMeetingReport(meetingId, report).subscribe(
            data => {
                this.operatorView.setData(data.result_data as IOperator, true);
            },
            error => {}
        );
    }

    /**
     * Shortcut to operator detail
     * @param operator
     */
    public shortcutFunction(operator: IOperator): void {
        this.operatorService.apiGetOperatorFull(operator.id).subscribe(
            data => {
                this.operatorView.setData(data.result_data as IOperator);
                this.modalOperator.openModal();
            },
            error => {}
        );
    }

    /**
     * Destroy
     */
    ngOnDestroy(): void {
        if (this.operatorsSub !== null && this.operatorsSub !== undefined) {
            this.operatorsSub.unsubscribe();
        }
    }

}
