import { Component, computed, input } from '@angular/core';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { DatePipe } from '@angular/common';
import { NgIcon, provideIcons } from '@ng-icons/core';
import {
  isaDeliveryVersand,
  isaDeliveryRuecklage2,
  isaDeliveryRuecklage1,
  isaDeliveryDownload,
} from '@isa/icons';
import { InlineAddressComponent } from '@isa/shared/address';
import { logger } from '@isa/core/logging';
import { OrderTypeFeature } from '@isa/common/data-access';

export type Address = {
  apartment?: string;
  careOf?: string;
  city?: string;
  country?: string;
  district?: string;
  info?: string;
  po?: string;
  region?: string;
  state?: string;
  street?: string;
  streetNumber?: string;
  zipCode?: string;
};

export type Branch = {
  name?: string;
  address?: Address;
};

export type ShippingAddress = {
  firstName?: string;
  lastName?: string;
  address?: Address;
};

export type EstimatedDelivery = {
  start: string;
  stop: string | null;
};

@Component({
  selector: 'shared-order-destination',
  templateUrl: './order-destination.component.html',
  styleUrls: ['./order-destination.component.css'],
  imports: [NgIcon, InlineAddressComponent, DatePipe],
  providers: [
    provideIcons({
      isaDeliveryVersand,
      isaDeliveryRuecklage2,
      isaDeliveryRuecklage1,
      isaDeliveryDownload,
    }),
  ],
})
export class OrderDestinationComponent {
  #logger = logger({ component: 'OrderDestinationComponent' });

  underline = input<boolean, unknown>(false, {
    transform: coerceBooleanProperty,
  });

  branch = input<Branch>();
  shippingAddress = input<ShippingAddress>();
  orderType = input.required<OrderTypeFeature>();
  estimatedDelivery = input<EstimatedDelivery | null>();

  destinationIcon = computed(() => {
    const orderType = this.orderType();
    this.#logger.debug('Computing destination icon', () => ({ orderType }));

    if (OrderTypeFeature.Delivery === orderType) {
      return 'isaDeliveryVersand';
    }

    if (OrderTypeFeature.Pickup === orderType) {
      return 'isaDeliveryRuecklage2';
    }

    if (OrderTypeFeature.InStore === orderType) {
      return 'isaDeliveryRuecklage1';
    }

    if (OrderTypeFeature.Download === orderType) {
      return 'isaDeliveryDownload';
    }

    return 'isaDeliveryVersand';
  });

  name = computed(() => {
    const orderType = this.orderType();

    if (OrderTypeFeature.Download === orderType) {
      return 'Hugendubel Digital';
    }

    if (
      OrderTypeFeature.Delivery === orderType ||
      OrderTypeFeature.B2BShipping === orderType ||
      OrderTypeFeature.DigitalShipping === orderType
    ) {
      const address = this.shippingAddress();
      return `${address?.firstName || ''} ${address?.lastName || ''}`.trim();
    }

    const branch = this.branch();
    return branch?.name || 'Filiale nicht gefunden';
  });

  address = computed(() => {
    const orderType = this.orderType();

    if (OrderTypeFeature.Download === orderType) {
      return undefined;
    }

    if (
      OrderTypeFeature.Delivery === orderType ||
      OrderTypeFeature.B2BShipping === orderType ||
      OrderTypeFeature.DigitalShipping === orderType
    ) {
      const shipping = this.shippingAddress();
      return shipping?.address;
    }

    const branch = this.branch();
    return branch?.address;
  });
}
