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

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

// Thired Party
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';

import { UtilitiesService } from 'src/app/services/utilities/utilities.service';


@Component({
  standalone: false,
  selector: 'app-review-order',
  templateUrl: './review-order.component.html',
  styleUrls: ['./review-order.component.scss']
})
export class ReviewOrderComponent implements OnInit {
    @Input() suppliers!: any;
    @Input() closeReviewOrder!: (suppliers: any) => void;
    @Input() completeOrder!: (suppliers: any) => void;
    @Input() paymentMethodInfo!: any;
    @Input() imgUrl!: string;
    @Input() sitename!: string;
    @Input() isStandSuppExists!: boolean;
    @Input() paymentSuppIndex!: any;
    @Input() surchargeAmount!: any;
    isCollapsed:boolean[] = [];
    constructor(
      private modalService: NgbModal,
      private utilities: UtilitiesService,
    ) { }

    ngOnInit(): void {
        // Initialize all input objects as empty objects if not provided
        this.paymentSuppIndex = this.paymentSuppIndex || {};
        this.paymentMethodInfo = this.paymentMethodInfo || {};
        this.surchargeAmount = this.surchargeAmount || {};
        this.suppliers = this.suppliers || {};
        
        // Ensure isCollapsed array is properly initialized
        this.isCollapsed = [];
    }

    /**
     * Calculating totals for each supplier
     * @param {type} supplier
     * @return {total|fee}
     */
    getSuppOrderTotal(supplier: any, surchargeAmnt: any) {
       if (!supplier) {
           return 0;
       }
       let totalAmount = this.utilities.getSuppOrderTotal(supplier);
       let surcharge = (surchargeAmnt && !isNaN(parseFloat(surchargeAmnt))) ? parseFloat(surchargeAmnt) : 0;
       return totalAmount + surcharge;
    };

    /**
     * Calculating complete items total amount for standard suppliers
     * @param object orderData
     * @return {Number}
     */
    getStandardSuppTotal(orderData: any) {
        if (!orderData) {
            return 0;
        }
        return this.utilities.getStandardSuppTotal(orderData);
    };

    /**
     * Calculating complete shipping fee amount for cart
     * @param {type} orderData
     * @return {Number}
     */
    getShipphingFeeTotal(orderData: any) {
      if (!orderData) {
          return 0;
      }
      return this.utilities.getShipphingFeeTotal(orderData);
    };

    /**
     * Method to calculate order subtotal for each payment setup enabled supplier
     * @param supplier
     * @return number
    */
    getSuppSubTotal(supplier: any): number{
      if (!supplier) {
          return 0;
      }
      return this.utilities.getSuppSubTotal(supplier);
    };

    /**
     * Safely get surcharge amount for a supplier
     * @param supplierId
     * @return number
     */
    getSurchargeAmount(supplierId: any): number {
        if (!this.surchargeAmount || !supplierId) {
            return 0;
        }
        const amount = this.surchargeAmount[supplierId];
        return (amount && !isNaN(parseFloat(amount))) ? parseFloat(amount) : 0;
    };

    /**
     * Safely get payment method info for a supplier
     * @param supplierId
     * @param field
     * @return string
     */
    getPaymentMethodInfo(supplierId: any, field: string): string {
        if (!this.paymentMethodInfo || !supplierId || !this.paymentMethodInfo[supplierId]) {
            return '';
        }
        return this.paymentMethodInfo[supplierId][field] || '';
    };

    /**
     * Safely get payment supplier index
     * @param supplierId
     * @return string
     */
    getPaymentSuppIndex(supplierId: any): string {
        if (!this.paymentSuppIndex || !supplierId || this.paymentSuppIndex[supplierId] === undefined) {
            return '';
        }
        return this.paymentSuppIndex[supplierId].toString();
    };

    /**
     * TrackBy function for suppliers to optimize *ngFor performance
     * @param index - The index of the item
     * @param supplier - The supplier object
     * @return string - Unique identifier for the supplier
     */
    trackBySupplier(index: number, supplier: any): string {
        return supplier?.value?.supplierData?.id || supplier?.key || index.toString();
    }
}
