import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {TabsComponent} from '../../../components/tabs/tabs.component';
import {ICustomer} from '../../../model/entities/customer';
import {CustomerDataViewComponent} from './customer-data-view/customer-data-view.component';
import {CustomerContactPersonViewComponent} from './customer-contact-person-view/customer-contact-person-view.component';
import {CustomerNoteViewComponent} from './customer-note-view/customer-note-view.component';
import {Factory} from '../../../factory/factory';
import {ICustomerContact} from '../../../model/entities/customer-contact';
import {ICustomerNote} from '../../../model/entities/customer-note';
import {CustomerMeetingViewComponent} from './customer-meeting-view/customer-meeting-view.component';
import {ICustomerMeeting} from '../../../model/entities/customer-meeting';

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

export class CustomerViewComponent implements OnInit {

    @Output() editCustomer: EventEmitter<ICustomer> = new EventEmitter<ICustomer>();
    @Output() createCustomerContact: EventEmitter<ICustomerContact> = new EventEmitter<ICustomerContact>();
    @Output() editCustomerContact: EventEmitter<ICustomerContact> = new EventEmitter<ICustomerContact>();
    @Output() deleteCustomerContact: EventEmitter<number> = new EventEmitter<number>();
    @Output() createNote: EventEmitter<ICustomerNote> = new EventEmitter<ICustomerNote>();
    @Output() editNote: EventEmitter<ICustomerNote> = new EventEmitter<ICustomerNote>();
    @Output() deleteNote: EventEmitter<number> = new EventEmitter<number>();
    @Output() createMeeting: EventEmitter<ICustomerMeeting> = new EventEmitter<ICustomerMeeting>();
    @Output() editMeeting: EventEmitter<ICustomerMeeting> = new EventEmitter<ICustomerMeeting>();
    @Output() deleteMeeting: EventEmitter<number> = new EventEmitter<number>();
    @Output() editMeetingReport: EventEmitter<number> = new EventEmitter<number>();
    @Output() shortcut: EventEmitter<ICustomer> = new EventEmitter<ICustomer>();
    @ViewChild('tabsCMView', { static: true }) tabsCMView: TabsComponent;
    @ViewChild('customerDataView', { static: true }) customerDataView: CustomerDataViewComponent;
    @ViewChild('customerContactPersonView', { static: true }) customerContactPersonView: CustomerContactPersonViewComponent;
    @ViewChild('customerNoteView', { static: true }) customerNoteView: CustomerNoteViewComponent;
    @ViewChild('customerMeetingView', { static: true }) customerMeetingView: CustomerMeetingViewComponent;

    public customer: ICustomer;

    constructor() { }

    ngOnInit() {
        this.customer = Factory.getEmptyCustomer();
    }

    /**
     * Sets data
     * @param customer
     * @param isUpdate
     */
    public setData(customer: ICustomer, isUpdate: boolean = false): void {
        this.customer = customer;
        this.customerDataView.setData(this.customer);
        this.customerContactPersonView.setData(this.customer, isUpdate);
        this.customerNoteView.setData(this.customer, isUpdate);
        this.customerMeetingView.setData(this.customer, isUpdate);
    }

    /**
     * Saves customer
     * @param customer
     */
    public editCustomerFunction(customer: ICustomer): void {
        this.editCustomer.emit(customer);
    }

    /**
     * Creates customer contact
     * @param customerContact
     */
    public createCustomerContactFunction(customerContact: ICustomerContact): void {
        this.createCustomerContact.emit(customerContact);
    }

    /**
     * Edit customer contact
     * @param customerContact
     */
    public editCustomerContactFunction(customerContact: ICustomerContact): void {
        this.editCustomerContact.emit(customerContact);
    }

    /**
     * Creates customer note
     * @param customerNote
     */
    public createCustomerNoteFunction(customerNote: ICustomerNote): void {
        this.createNote.emit(customerNote);
    }

    /**
     * Edits note entry
     * @param customerNote
     */
    public editCustomerNoteFunction(customerNote: ICustomerNote) {
        this.editNote.emit(customerNote);
    }

    /**
     * Deletes customer note
     * @param noteId
     */
    public deleteCustomerNoteFunction(noteId: number): void {
        this.deleteNote.emit(noteId);
    }

    /**
     * Deletes customer contact
     * @param contactId
     */
    public deleteCustomerContactFunction(contactId: number): void {
        this.deleteCustomerContact.emit(contactId);
    }

    /**
     * Creates customer meeting
     * @param customerMeeting
     */
    public createCustomerMeetingFunction(customerMeeting: ICustomerMeeting): void {
        this.createMeeting.emit(customerMeeting);
    }

    /**
     * Edits meeting entry
     * @param customerMeeting
     */
    public editCustomerMeetingFunction(customerMeeting: ICustomerMeeting) {
        this.editMeeting.emit(customerMeeting);
    }

    /**
     * Deletes customer meeting
     * @param meetingId
     */
    public deleteCustomerMeetingFunction(meetingId: number): void {
        this.deleteMeeting.emit(meetingId);
    }

    /**
     * Edit report after meeting has started
     * @param reportValues
     */
    public editCustomerMeetingReportFunction(reportValues: any): void {
        this.editMeetingReport.emit(reportValues);
    }

    /**
     * Shortcut to customer detail
     * @param customer
     */
    public shortcutFunction(customer: ICustomer): void {
        this.shortcut.emit(customer);
    }
}
