import {Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {AgGridComponent} from '../../components/ag-grid-component';
import {TabsComponent} from '../../components/tabs/tabs.component';
import {ModalComponent} from '../../components/modal/modal.component';
import {ServiceViewComponent} from '../services/service-view/service-view.component';
import {ServicesListComponent} from '../services/services-list/services-list.component';
import {
    ServicesContactListComponent
} from '../services/services-contact-list/services-contact-list.component';
import {
    ServiceDataEditComponent
} from '../services/service-view/service-data-edit/service-data-edit.component';
import {IService} from '../../model/entities/service';
import {Subscription} from 'rxjs';
import {IServiceContact} from '../../model/entities/service-contact';
import {AppService} from '../../services/app.service';
import {ServiceService} from '../../services/service.service';
import {MeetingService} from '../../services/meeting.service';
import {isNull} from 'util';
import {Factory} from '../../factory/factory';
import {IServiceNote} from '../../model/entities/service-note';
import {IServiceMeeting} from '../../model/entities/service-meeting';

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

export class ServicesComponent extends AgGridComponent implements OnInit, OnDestroy {

    @ViewChild('tabsCM', { static: true }) tabsCM: TabsComponent;
    @ViewChild('modalService', { static: true }) modalService: ModalComponent;
    @ViewChild('serviceView', { static: true }) serviceView: ServiceViewComponent;
    @ViewChild('servicesList', { static: true }) servicesList: ServicesListComponent;
    @ViewChild('servicesContactList', { static: true }) servicesContactList: ServicesContactListComponent;
    @ViewChild('modalServiceDataEdit', { static: true }) modalServiceDataEdit: ModalComponent;
    @ViewChild('serviceDataEdit', { static: true }) serviceDataEdit: ServiceDataEditComponent;
    @ViewChild('headline', { static: true }) headline: ElementRef;
    @ViewChild('content', { static: true }) content: ElementRef;

    private services: IService[];
    private servicesSub: Subscription;
    private servicesContactsSub: Subscription;
    public serviceContacts: IServiceContact[];

    constructor(private appService: AppService, private serviceService: ServiceService, private meetingService: MeetingService) {
        super();
        this.serviceService.apiGetServiceData();
    }

    ngOnInit() {
        this.serviceContacts = [];

        this.servicesSub = this.serviceService.getServices$().subscribe(
            data => {
                this.services = data;
                this.setComponentData();
            }
        );

        this.servicesContactsSub = this.serviceService.getServiceContacts$().subscribe(
            data => {
                this.serviceContacts = data;
                this.setComponentData();
            }
        );
    }

    /**
     * Set component data after all data has been received
     */
    private setComponentData() {
        if (!isNull(this.services) && !isNull(this.serviceContacts)) {
            this.servicesList.setData(this.services);
            this.servicesContactList.setData(this.services, this.serviceContacts);
        }
    }

    /**
     * service row is clicked
     * @param e
     */
    public rowClickedService(e: any) {
        this.serviceService.apiGetServiceFull(e.data.service_id).subscribe(
            data => {
                this.serviceView.setData(data.result_data as IService);
                this.modalService.openModal();
            },
            error => {}
        );
    }

    /**
     * OnClick Create service
     */
    public createService(): void {
        this.modalServiceDataEdit.openModal();
        this.serviceDataEdit.setData(Factory.getEmptyService());
    }

    /**
     * Creates service
     * @param service
     */
    public createServiceFunction(service: IService): void {
        this.serviceService.apiCreateService(service).subscribe(
            data => {
                this.modalServiceDataEdit.closeModal();
            },
            error => {}
        );
    }

    /**
     * Saves service
     * @param service
     */
    public editServiceFunction(service: IService): void {
        this.serviceService.apiEditService(service).subscribe(
            data => {
                this.serviceView.setData(data.result_data as IService);
            },
            error => {}
        );
    }

    /**
     * Creates service contact
     * @param serviceContact
     */
    public createServiceContactFunction(serviceContact: IServiceContact): void {
        this.serviceService.apiCreateServiceContact(serviceContact).subscribe(
            data => {
                this.serviceView.setData(data.result_data as IService);
            },
            error => {}
        );
    }

    /**
     * Edit service contact
     * @param serviceContact
     */
    public editServiceContactFunction(serviceContact: IServiceContact): void {
        this.serviceService.apiEditServiceContact(serviceContact).subscribe(
            data => {
                this.serviceView.setData(data.result_data as IService, true);
            },
            error => {}
        );
    }

    /**
     * Deletes service contact
     * @param serviceContactId
     */
    public deleteServiceContactFunction(serviceContactId: number): void {
        this.serviceService.apiDeleteServiceContact(serviceContactId).subscribe(
            data => {
                this.serviceView.setData(data.result_data as IService);
            },
            error => {}
        );
    }

    /**
     * Creates service note entry
     * @param serviceNote
     */
    public createServiceNoteFunction(serviceNote: IServiceNote): void {
        this.serviceService.apiCreateServiceNote(serviceNote).subscribe(
            data => {
                this.serviceView.setData(data.result_data as IService);
            },
            error => {}
        );
    }

    /**
     * Edits service note entry
     * @param serviceNote
     */
    public editServiceNoteFunction(serviceNote: IServiceNote): void {
        this.serviceService.apiEditServiceNote(serviceNote).subscribe(
            data => {
                this.serviceView.setData(data.result_data as IService, true);
            },
            error => {}
        );
    }

    /**
     * Deletes service note entry
     * @param serviceNoteId
     */
    public deleteServiceNoteFunction(serviceNoteId: number): void {
        this.serviceService.apiDeleteServiceNote(serviceNoteId).subscribe(
            data => {
                this.serviceView.setData(data.result_data as IService);
            },
            error => {}
        );
    }

    /**
     * Creates service meeting entry
     * @param serviceMeeting
     */
    public createServiceMeetingFunction(serviceMeeting: IServiceMeeting): void {
        this.meetingService.apiCreateServiceMeeting(serviceMeeting).subscribe(
            data => {
                this.serviceView.setData(data.result_data as IService);
            },
            error => {}
        );
    }

    /**
     * Edits service meeting entry
     * @param serviceMeeting
     */
    public editServiceMeetingFunction(serviceMeeting: IServiceMeeting): void {
        this.meetingService.apiEditServiceMeeting(serviceMeeting).subscribe(
            data => {
                this.serviceView.setData(data.result_data as IService, true);
            },
            error => {}
        );
    }

    /**
     * Deletes service meeting entry
     * @param serviceMeetingId
     */
    public deleteServiceMeetingFunction(serviceMeetingId: number): void {
        this.meetingService.apiDeleteServiceMeeting(serviceMeetingId).subscribe(
            data => {
                this.serviceView.setData(data.result_data as IService);
            },
            error => {}
        );
    }

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

    /**
     * Shortcut to service detail
     * @param service
     */
    public shortcutFunction(service: IService): void {
        this.serviceService.apiGetServiceFull(service.id).subscribe(
            data => {
                this.serviceView.setData(data.result_data as IService);
                this.modalService.openModal();
            },
            error => {}
        );
    }

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

}
