import { Component, OnInit, Output, EventEmitter, ViewChild, Input, OnChanges, SimpleChanges, SimpleChange} from '@angular/core';
import { FinancingOptionsProperties } from '../../interfaces/financing-options-properties-interface';

import { Router, ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-car-summary-header',
  templateUrl: './car-summary-header.component.html',
  styleUrls: ['./car-summary-header.component.scss']
})
export class CarSummaryHeaderComponent implements OnInit {

  @ViewChild('basicModal') modal;
  @ViewChild('mailModal') mailModal;
  @ViewChild('quotationModal') quotationModal;
  @Output() public testDrive: EventEmitter<any> = new EventEmitter();
  @Output() public editInfo: EventEmitter<any> = new EventEmitter();
  @Output() public sendMail: EventEmitter<any> = new EventEmitter();
  @Output() public addToWishlist: EventEmitter<any> = new EventEmitter();
  @Output() public financeOptions: EventEmitter<any> = new EventEmitter();
  @Output() public submitFinanceOptions: EventEmitter<any> = new EventEmitter();
  @Output() public submitCustomerForm: EventEmitter<any> = new EventEmitter();
  @Output() public viewCustomerLeads: EventEmitter<any> = new EventEmitter();  
  @Output() public linkCustomerLeads: EventEmitter<any> = new EventEmitter();
  
  @ViewChild('financeOptions') financeOptions1;
  @ViewChild('otherOptions') otherOptions1;
  @ViewChild('customerTable') customerTableContainer;
  @ViewChild('leadTable') leadTableContainer;
  
  @Input() financingOptionsItems: FinancingOptionsProperties[];
  @Input() displayType: string = 'vehicle';
  @Input() vehicleData;
  @Input() customerData;
  @Input() leadsData;
  @Input() vehicleLabels;
  @Input() customerDetails;
  @Input() customerDetailData;
  @Input() quoteData;
  @Input() disableEdit: Boolean = false;

  public vehicleHeaderData;
  public vehicleLabelsData;
  public vehicleCustomerData;
  public customerLeadData;
  public pageType: boolean = false;
  
  customerModel: any = {};
  showSpinner: Boolean = false;
  showSpinnerLeadTable: Boolean = false;
  
  constructor(
    private router: Router,
  ) {
  }
  
  ngOnInit() {
    this.leadTableContainer.nativeElement.style.display = 'none';
  }

  ngOnChanges(changes: SimpleChanges) {
    if (changes.vehicleLabels) {
      this.vehicleLabelsData = changes.vehicleLabels.currentValue;
    }
    if (changes.vehicleData) {
      this.vehicleHeaderData = changes.vehicleData.currentValue;
    }
    if (changes.customerData) {
      this.showSpinner = false;
      this.vehicleCustomerData = changes.customerData.currentValue;
    }
    if (changes.leadsData) {
      this.showSpinnerLeadTable = false;
      this.leadTableContainer.nativeElement.style.display = 'block';
      this.customerLeadData = changes.leadsData.currentValue;
    }
    if (changes.customerDetailData) {
      this.customerDetailData = changes.customerDetailData.currentValue;
    }
    if (changes.displayType) {
      if (changes.displayType.currentValue === 'quotation') {
        this.pageType = true;
      }
    }
    if (changes.disableEdit) {
      this.disableEdit = changes.disableEdit.currentValue;
    }
    if (changes.quoteData) {
      this.quoteData = changes.quoteData.currentValue;
    }
  }

  drive() {
    this.testDrive.emit('');
  }

  editInformation() {
    this.editInfo.emit('');
  }

  mailToSelf() {
    this.mailModal.show();
  }

  wishList() {
    this.addToWishlist.emit('');
  }

  financingOptions(e?: any) {
    this.modal.show();
  }

  submitFinanceOption() {
    this.submitFinanceOptions.emit('');
  }

  closeDialog() {
    this.modal.nativeElement.style.display = `none`;
  }

  financialOptionsSelected(val) {
    if (val === 'Installments') {
      this.financeOptions1.nativeElement.style.display = 'block';
      this.otherOptions1.nativeElement.style.display = 'none';
    } else {
      this.financeOptions1.nativeElement.style.display = 'none';
      this.otherOptions1.nativeElement.style.display = 'block';
    }
  }

  createQuotationMethod() {
    this.customerTableContainer.nativeElement.style.display = 'block';
    this.leadTableContainer.nativeElement.style.display = 'none';
    this.vehicleCustomerData = null;
    this.customerModel.firstName = ' ';
    this.quotationModal.show();
  }

  public onCustomerFormSubmit() {
    this.showSpinner = true;
    this.submitCustomerForm.emit(this.customerModel);
  }

  viewLeads(obj) {
    this.showSpinnerLeadTable = true;
    this.customerTableContainer.nativeElement.style.display = 'none';
    this.viewCustomerLeads.emit(obj);
  }

  goBackToCustomer() {
    this.customerTableContainer.nativeElement.style.display = 'block';
    this.leadTableContainer.nativeElement.style.display = 'none';
  }

  navigateToCustomerCreate() {
    this.router.navigate(['./customersearch/create']);
  }

  navigateToCreateLead() {
    this.router.navigate(['./stocks/lead/create']);
  }

  linkLead(obj) {
    this.linkCustomerLeads.emit(obj);
  }

  optionsMenu() {
    alert("options");
  }

  saveChanges() {
    alert("save changes");
  }

}
