import 'package:pshared/data/dto/payment/card.dart';
import 'package:pshared/data/dto/payment/card_token.dart';
import 'package:pshared/data/dto/payment/external_chain.dart';
import 'package:pshared/data/dto/payment/iban.dart';
import 'package:pshared/data/dto/payment/ledger.dart';
import 'package:pshared/data/dto/payment/managed_wallet.dart';
import 'package:pshared/data/dto/payment/method.dart';
import 'package:pshared/data/dto/payment/russian_bank.dart';
import 'package:pshared/data/dto/payment/wallet.dart';
import 'package:pshared/data/mapper/payment/card_token.dart';
import 'package:pshared/data/mapper/payment/crypto_address.dart';
import 'package:pshared/data/mapper/payment/card.dart';
import 'package:pshared/data/mapper/payment/iban.dart';
import 'package:pshared/data/mapper/payment/ledger.dart';
import 'package:pshared/data/mapper/payment/managed_wallet.dart';
import 'package:pshared/data/mapper/payment/russian_bank.dart';
import 'package:pshared/data/mapper/payment/type.dart';
import 'package:pshared/data/mapper/payment/wallet.dart';
import 'package:pshared/models/describable.dart';
import 'package:pshared/models/organization/bound.dart';
import 'package:pshared/models/payment/methods/card.dart';
import 'package:pshared/models/payment/methods/card_token.dart';
import 'package:pshared/models/payment/methods/crypto_address.dart';
import 'package:pshared/models/payment/methods/data.dart';
import 'package:pshared/models/payment/methods/iban.dart';
import 'package:pshared/models/payment/methods/ledger.dart';
import 'package:pshared/models/payment/methods/managed_wallet.dart';
import 'package:pshared/models/payment/methods/russian_bank.dart';
import 'package:pshared/models/payment/methods/type.dart';
import 'package:pshared/models/payment/methods/wallet.dart';
import 'package:pshared/models/payment/type.dart';
import 'package:pshared/models/permissions/bound.dart';
import 'package:pshared/models/storable.dart';


extension PaymentMethodDataJsonMapper on PaymentMethodData {
  Map<String, dynamic> toJsonMap() => switch (this) {
    CardPaymentMethod card => card.toDTO().toJson(),
    CardTokenPaymentMethod cardToken => cardToken.toDTO().toJson(),
    IbanPaymentMethod iban => iban.toDTO().toJson(),
    RussianBankAccountPaymentMethod bankAccount => bankAccount.toDTO().toJson(),
    WalletPaymentMethod wallet => wallet.toDTO().toJson(),
    CryptoAddressPaymentMethod crypto => crypto.toDTO().toJson(),
    LedgerPaymentMethod ledger => ledger.toDTO().toJson(),
    ManagedWalletPaymentMethod managedWallet => managedWallet.toDTO().toJson(),
    _ => throw UnsupportedError('Unsupported payment method data: $runtimeType'),
  };
}

extension PaymentMethodMapper on PaymentMethod {
  PaymentMethodDTO toDTO() => PaymentMethodDTO(
    id: storable.id,
    createdAt: storable.createdAt,
    updatedAt: storable.updatedAt,
    permissionRef: permissionBound.permissionRef,
    organizationRef: permissionBound.organizationRef,
    recipientRef: recipientRef,
    type: paymentTypeToValue(type),
    data: _dataToJson(data),
    isArchived: isArchived,
    name: describable.name,
    description: describable.description,
    isMain: isMain,
  );

  Map<String, dynamic> _dataToJson(PaymentMethodData data) => data.toJsonMap();
}

extension PaymentMethodDTOMapper on PaymentMethodDTO {
  PaymentMethod toDomain() => PaymentMethod(
    storable: newStorable(id: id, createdAt: createdAt, updatedAt: updatedAt),
    permissionBound: newPermissionBound(
      organizationBound: newOrganizationBound(organizationRef: organizationRef),
      permissionRef: permissionRef,
    ),
    recipientRef: recipientRef,
    data: _dataToDomain(paymentTypeFromValue(type), data),
    isArchived: isArchived,
    describable: newDescribable(name: name, description: description),
    isMain: isMain,
  );

  PaymentMethodData _dataToDomain(PaymentType paymentType, Map<String, dynamic> payload) {
    switch (paymentType) {
      case PaymentType.card:
        return CardEndpointDTO.fromJson(payload).toDomain();
      case PaymentType.cardToken:
        return CardTokenEndpointDTO.fromJson(payload).toDomain();
      case PaymentType.iban:
        return IbanPaymentDataDTO.fromJson(payload).toDomain();
      case PaymentType.bankAccount:
        return RussianBankAccountPaymentDataDTO.fromJson(payload).toDomain();
      case PaymentType.wallet:
        return WalletPaymentDataDTO.fromJson(payload).toDomain();
      case PaymentType.externalChain:
        return ExternalChainEndpointDTO.fromJson(payload).toDomain();
      case PaymentType.ledger:
        return LedgerEndpointDTO.fromJson(payload).toDomain();
      case PaymentType.managedWallet:
        return ManagedWalletEndpointDTO.fromJson(payload).toDomain();
    }
  }
}
