// Angular Core
import { Component, OnInit, Input } from '@angular/core';

// Third Party
import { NgbActiveModal, NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { ModalConfig } from 'src/app/interfaces/common-interfaces';
import { ModalInterface } from 'src/app/interfaces/modal-interface';
import { AccountInfoService } from 'src/app/services/accountInfo/account-info.service';

@Component({
  standalone: false,
  selector: 'app-tax-exempt-message',
  templateUrl: './tax-exempt-message.component.html',
  styleUrls: ['./tax-exempt-message.component.scss']
})
export class TaxExemptMessageComponent implements OnInit, ModalInterface {

  /**
   * Header for TaxExemptPopup Message Modal
   */
  showTaxExemptPopupMessageConfig: ModalConfig = {
    showHeader: true,
    title: 'Resale Certificate Message'
  };

  @Input() taxExemptWarningCount:any

  modalComponent!: NgbModalRef;

  constructor(public activeModal: NgbActiveModal, public modalService: NgbModal, private accountInfoService: AccountInfoService) {
    this.modalService = modalService;
  }

  /**
   * Open current modal
   * @returns void
   */
  openModal(): void {
    this.modalComponent = this.modalService?.open(TaxExemptMessageComponent, { size: 'sm' });
  }

  /**
   * Close current modal
   * @returns void
   */
  closeModal(): void {
    this.modalService.dismissAll();
    this.updatePopUpDisplayCount();
  }
  
  /**
   * Sets data required to display the modal if any.
   * @param data
   * @returns void
   */
  setData(data: any): void {
    this.modalComponent.componentInstance.taxExemptWarningCount = data.taxExemptWarningCount;
  }

  /**
   * Decrease the current pop up to be displayed count by 1 and update it.
   * @returns void
   */
  updatePopUpDisplayCount(): void {
    this.accountInfoService.updateTaxExmptWrngCount(--this.taxExemptWarningCount)
    .subscribe({
        error: (err) => {
            this.accountInfoService.errorCallBack(err);
        },
        complete: () => { },
    });
    localStorage.setItem("taxExempt", '1');
  }

  ngOnInit(): void {
  }

}
