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 {Factory} from '../../factory/factory';
import {isNull} from 'util';
import {ProductionViewComponent} from './production-view/production-view.component';
import {ProductionsListComponent} from './productions-list/productions-list.component';
import {ProductionsContactListComponent} from './productions-contact-list/productions-contact-list.component';
import {ProductionDataEditComponent} from './production-view/production-data-edit/production-data-edit.component';
import {IProduction} from '../../model/entities/production';
import {IProductionContact} from '../../model/entities/production-contact';
import {ProductionService} from '../../services/production.service';
import {IProductionNote} from '../../model/entities/production-note';
import {IProductionMeeting} from '../../model/entities/production-meeting';

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

export class ProductionsComponent extends AgGridComponent implements OnInit, OnDestroy {

    @ViewChild('tabsCM', { static: true }) tabsCM: TabsComponent;
    @ViewChild('modalProduction', { static: true }) modalProduction: ModalComponent;
    @ViewChild('productionView', { static: true }) productionView: ProductionViewComponent;
    @ViewChild('productionsList', { static: true }) productionsList: ProductionsListComponent;
    @ViewChild('productionsContactList', { static: true }) productionsContactList: ProductionsContactListComponent;
    @ViewChild('modalProductionDataEdit', { static: true }) modalProductionDataEdit: ModalComponent;
    @ViewChild('productionDataEdit', { static: true }) productionDataEdit: ProductionDataEditComponent;
    @ViewChild('headline', { static: true }) headline: ElementRef;
    @ViewChild('content', { static: true }) content: ElementRef;

    private productions: IProduction[];
    private productionsSub: Subscription;
    private productionsContactsSub: Subscription;
    public productionContacts: IProductionContact[];

    constructor(private appService: AppService, private productionService: ProductionService, private meetingService: MeetingService) {
        super();
        this.productionService.apiGetProductionData();
    }

    ngOnInit() {
        this.productionContacts = [];

        this.productionsSub = this.productionService.getProductions$().subscribe(
            data => {
                this.productions = data;
                this.setComponentData();
            }
        );

        this.productionsContactsSub = this.productionService.getProductionContacts$().subscribe(
            data => {
                this.productionContacts = data;
                this.setComponentData();
            }
        );
    }

    /**
     * Set component data after all data has been received
     */
    private setComponentData() {
        if (!isNull(this.productions) && !isNull(this.productionContacts)) {
            this.productionsList.setData(this.productions);
            this.productionsContactList.setData(this.productions, this.productionContacts);
        }
    }

    /**
     * production row is clicked
     * @param e
     */
    public rowClickedProduction(e: any) {
        this.productionService.apiGetProductionFull(e.data.production_id).subscribe(
            data => {
                this.productionView.setData(data.result_data as IProduction);
                this.modalProduction.openModal();
            },
            error => {}
        );
    }

    /**
     * OnClick Create production
     */
    public createProduction(): void {
        this.modalProductionDataEdit.openModal();
        this.productionDataEdit.setData(Factory.getEmptyProduction());
    }

    /**
     * Creates production
     * @param production
     */
    public createProductionFunction(production: IProduction): void {
        this.productionService.apiCreateProduction(production).subscribe(
            data => {
                this.modalProductionDataEdit.closeModal();
            },
            error => {}
        );
    }

    /**
     * Saves production
     * @param production
     */
    public editProductionFunction(production: IProduction): void {
        this.productionService.apiEditProduction(production).subscribe(
            data => {
                this.productionView.setData(data.result_data as IProduction);
            },
            error => {}
        );
    }

    /**
     * Creates production contact
     * @param productionContact
     */
    public createProductionContactFunction(productionContact: IProductionContact): void {
        this.productionService.apiCreateProductionContact(productionContact).subscribe(
            data => {
                this.productionView.setData(data.result_data as IProduction);
            },
            error => {}
        );
    }

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

    /**
     * Deletes production contact
     * @param productionContactId
     */
    public deleteProductionContactFunction(productionContactId: number): void {
        this.productionService.apiDeleteProductionContact(productionContactId).subscribe(
            data => {
                this.productionView.setData(data.result_data as IProduction);
            },
            error => {}
        );
    }

    /**
     * Creates production note entry
     * @param productionNote
     */
    public createProductionNoteFunction(productionNote: IProductionNote): void {
        this.productionService.apiCreateProductionNote(productionNote).subscribe(
            data => {
                this.productionView.setData(data.result_data as IProduction);
            },
            error => {}
        );
    }

    /**
     * Edits production note entry
     * @param productionNote
     */
    public editProductionNoteFunction(productionNote: IProductionNote): void {
        this.productionService.apiEditProductionNote(productionNote).subscribe(
            data => {
                this.productionView.setData(data.result_data as IProduction, true);
            },
            error => {}
        );
    }

    /**
     * Deletes production note entry
     * @param productionNoteId
     */
    public deleteProductionNoteFunction(productionNoteId: number): void {
        this.productionService.apiDeleteProductionNote(productionNoteId).subscribe(
            data => {
                this.productionView.setData(data.result_data as IProduction);
            },
            error => {}
        );
    }

    /**
     * Creates production meeting entry
     * @param productionMeeting
     */
    public createProductionMeetingFunction(productionMeeting: IProductionMeeting): void {
        this.meetingService.apiCreateProductionMeeting(productionMeeting).subscribe(
            data => {
                this.productionView.setData(data.result_data as IProduction);
            },
            error => {}
        );
    }

    /**
     * Edits production meeting entry
     * @param productionMeeting
     */
    public editProductionMeetingFunction(productionMeeting: IProductionMeeting): void {
        this.meetingService.apiEditProductionMeeting(productionMeeting).subscribe(
            data => {
                this.productionView.setData(data.result_data as IProduction, true);
            },
            error => {}
        );
    }

    /**
     * Deletes production meeting entry
     * @param productionMeetingId
     */
    public deleteProductionMeetingFunction(productionMeetingId: number): void {
        this.meetingService.apiDeleteProductionMeeting(productionMeetingId).subscribe(
            data => {
                this.productionView.setData(data.result_data as IProduction);
            },
            error => {}
        );
    }

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

    /**
     * Shortcut to production detail
     * @param production
     */
    public shortcutFunction(production: IProduction): void {
        this.productionService.apiGetProductionFull(production.id).subscribe(
            data => {
                this.productionView.setData(data.result_data as IProduction);
                this.modalProduction.openModal();
            },
            error => {}
        );
    }

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

}
