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

// RxJs
import { catchError, Subscription, throwError } from 'rxjs';

// Services
import { ManageSupplierService } from 'src/app/services/manageSupplier/manage-supplier.service';

// Third party
import { NgbModal, NgbModalConfig, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';

@Component({
  standalone: false,
  selector: 'app-supplier-info',
  templateUrl: './supplier-info.component.html',
  styleUrls: ['./supplier-info.component.scss']
})
export class SupplierInfoComponent implements OnInit, OnDestroy {

  @Input() row !: number;
  @ViewChild('supplierInfoModal') supplierInfoModal!: ElementRef<any>;
  isLoading: boolean = false;
  isSuppInfoModalOpened: boolean = false;

  constructor(
    private manageSupplier: ManageSupplierService,
    private messageModal: NgbModal,
    private ngbModalconfig: NgbModalConfig,) { 
    // customize default values of modals used by this component tree
		this.ngbModalconfig.backdrop = 'static';
		this.ngbModalconfig.keyboard = false;
  }

  ngOnInit(): void {
  }

  /*
  * Private Property for base modal config for supplier info
  */
  supplierInfoConfig = {
    showHeader: false
  };
  supplierInfoModalConf!:NgbModalRef;
  supplierInfo!:any;
  pedigreeInformation!:any;
  shop_about!:any;
  terms_conditions!:any;
  return_policy!:any;
  activeTab!:number;
  deaText!:string;

  supplierInfoClose() {
    this.supplierInfoModalConf.close();
    this.isSuppInfoModalOpened = false;
  };

  getSupplierDataSubscribe!:Subscription;
  /**
   * To open Supplier Info Modal
   * @param {int} supplierId
   * @returns {undefined}
   */
  showSupplierInfo(supplierId:number | undefined) {
    if (!this.isSuppInfoModalOpened) {
        this.isSuppInfoModalOpened = true;
        var args = {
            supplierId : supplierId
        };
        this.activeTab = 1;
        this.getSupplierDataSubscribe = this.manageSupplier.getSupplierData(args)
        .pipe(
          catchError(() => {
            return throwError(() => new Error('ups sommething happend'));
          })
        )
        .subscribe({
          next: (res: any) => {
            this.supplierInfo = res.data;
            this.pedigreeInformation = this.supplierInfo.pedigree_information;
            this.shop_about = this.supplierInfo.shop_about;
            this.terms_conditions = this.supplierInfo.terms_conditions;
            this.return_policy = this.supplierInfo.return_policy;
                this.setRxAuthStateLicense(this.supplierInfo);
                if(this.supplierInfo.pedigree_information) {
                  this.activeTab = 0;
                }

                this.deaText = 'DEA Medical License';
                if (this.supplierInfo.is_hin) {
                    this.deaText = 'HIN Medical License';
                }
                this.supplierInfoModalConf = this.messageModal.open(this.supplierInfoModal, {size: 'lg'});
              },
              error: (err) => {
                this.manageSupplier.errorCallBack(err);
              },
              complete: () => { },
            })
    }
  };

    /**
    * Method used to override the auth_state_rx_license_document value inactive
    * based on validated date and expiration date
    * @param object supplierInfo
    * @returns void
    */
    setRxAuthStateLicense = function(supplierInfo: any) {
        if (supplierInfo.auth_state_rx_license_document !== 0
           && (supplierInfo.rx_license_validated_date === "0000-00-00"
           || new Date(supplierInfo.rx_license_expire_date) <= new Date())) {
          supplierInfo.auth_state_rx_license_document = 0;
        } 
    };

   ngOnDestroy(){
    this.getSupplierDataSubscribe?.unsubscribe();
   }

  }
