import 'package:injectable/injectable.dart'; import 'package:mnemo_cards_backend/api/ads/ads_manager.dart'; import 'package:mnemo_cards_backend/extensions.dart'; import 'package:mnemo_cards_backend/promo_codes/promo_codes_manager.dart'; import 'package:mnemo_cards_common_backend/mnemo_cards_common_backend.dart'; import 'package:mnemo_cards_backend/packs/card_model_extension.dart'; import 'package:mnemo_cards_backend/packs/pack_manager.dart'; import 'package:mnemo_cards_common/mnemo_cards_common.dart'; import 'products_price_resolver.dart'; bool canOpenForAd(int? id) => id != null; @LazySingleton() class PackDtoConverter { final ProductsPriceResolver _productsPriceResolver; final AdsManager _adsManager; const PackDtoConverter( this._productsPriceResolver, this._adsManager, ); Future toCardPackPreviewDto( CardPackModel model, UserModel? userModel, ) async { bool available = model.users.contains(userModel); if (userModel != null && !available) { await userModel.subscriptionModel.load(); available = userModel.subscriptionModel.value?.features .contains(SubscriptionFeatureEnum.packs) == true; } String? price = model.price; if (userModel != null && !available) { price = await _productsPriceResolver.userPackPrice(userModel, model); } final canOpenForAdVal = !available && canOpenForAd(model.id); return CardPackPreviewDto( id: model.id.toString(), title: model.title, subtitle: model.subtitle, cards: model.cards.length, tests: model.tests.length, price: price, imageBase64: await model.cover?.smallBase64Image, color: model.color, isAvailable: available, // trail: 'asset:icons/gift.png', tip: available ? null : canOpenForAdVal ? TextPackTip( 'πŸ“Ί', position: PackTipPosition.bottomRight, ) : TextPackTip('πŸ”’'), version: model.version, ); } Future toCardPackBuyDto( CardPackModel model, UserModel? userModel, ) async { final price = userModel == null ? model.price : await _productsPriceResolver.userPackPrice(userModel, model); final canOpenForAdVal = userModel != null && canOpenForAd(model.id); String? _adsKey; if (canOpenForAdVal) { _adsKey = _adsManager.getAdsKey([model], userModel); } return CardPackBuyDto( id: model.id.toString(), title: model.title, subtitle: model.subtitle, appStoreId: model.appStoreId, rustoreId: model.rustoreId, googlePlayId: model.googlePlayId, cards: await Future.wait( (model.previewCards.isNotEmpty ? model.previewCards : model.cards.take(3)) .toDtosList(model.cardsOrder) .map( (dto) async => dto.copyWith( image: await dto.image?.smallBase64Image, ), ), ), items: [ if (model.description != null) TextItem( title: model.description, ), if (_adsKey == null) ...[ SpacerItem(), TextItem( subtitle: 'ΠžΡ‚ΠΊΡ€Ρ‹Ρ‚ΡŒ всС Ρ‚Π΅ΠΌΡ‹ с подпиской!', deeplink: SubscriptionOpenDeeplink().uri.toString(), ), ], if (_adsKey != null) ...[ SpacerItem(), TextItem( subtitle: 'Π­Ρ‚Ρƒ Ρ‚Π΅ΠΌΡƒ ΠΌΠΎΠΆΠ½ΠΎ ΠΎΡ‚ΠΊΡ€Ρ‹Ρ‚ΡŒ, посмотрСв Ρ€Π΅ΠΊΠ»Π°ΠΌΡƒ', deeplink: ProductForAdDeeplink( model.toDto(), _adsKey, ).uri.toString(), ), ], SpacerItem( flex: 0, height: 10, ), if (_adsKey == null) ...[ TextItem( subtitle: 'ΠžΠΏΠ»Π°Ρ‚ΠΈΡ‚ΡŒ российской ΠΊΠ°Ρ€Ρ‚ΠΎΠΉ', deeplink: PurchaseInitDeeplink( id: model.id.toString(), productType: MnemoCardsProductType.pack, paymentId: model.id.toString(), paymentSystem: PaymentSystem.yookassa, ).uri.toString(), ), ], if (model.googlePlayId != null) ButtonItem( height: 110, children: [ TextItem( title: 'ΠžΠΏΠ»Π°Ρ‚ΠΈΡ‚ΡŒ Ρ‡Π΅Ρ€Π΅Π·', subtitle: 'Google Pay', ) ], deeplink: PurchaseInitDeeplink( id: model.id.toString(), productType: MnemoCardsProductType.pack, paymentId: model.googlePlayId!, paymentSystem: PaymentSystem.google, ).uri.toString(), ) else SpacerItem(height: 20, flex: 0) ], color: model.color, version: model.version, price: price, ); } Future toEditDto( CardPackModel model, { bool withCards = true, bool withTests = false, bool withUsers = false, }) async { await model.cards.load(); await model.tests.load(); await model.previewCards.load(); return EditCardPackDto( id: model.id.toString(), title: model.title, subtitle: model.subtitle, color: model.color, version: model.version, cover: await model.cover?.base64Image, size: model.size, googlePlayId: model.googlePlayId, rustoreId: model.rustoreId, appStoreId: model.appStoreId, price: model.price, description: model.description, enabled: model.enabled, addCardIds: withCards ? model.cards.map((c) => c.id.toString()).toList() : null, addTestIds: withTests ? model.tests.map((t) => t.id.toString()).toList() : null, removeCardIds: null, removeTestIds: null, previewCards: model.previewCards .map((card) => card.id?.toString()) .whereNotNull() .toList(), order: model.order, cardsOrder: model.cardsOrder, ); } CardPackDto toDto(CardPackModel model) { return CardPackDto( id: model.id.toString(), title: model.title, subtitle: model.subtitle, cards: model.cards.toDtosList(model.cardsOrder), color: model.color, version: model.version, ); } CardPackModel toModel(EditCardPackDto dto, CardPackModel? model) { return CardPackModel( title: dto.title ?? model?.title ?? '', subtitle: dto.subtitle ?? model?.subtitle ?? '', size: dto.size ?? model?.size ?? 0, color: dto.color ?? model?.color, cover: dto.cover ?? model?.cover, description: dto.description ?? model?.description, version: dto.version ?? model?.version, id: model?.id == null ? int.tryParse(dto.id ?? '') : model!.id, googlePlayId: dto.googlePlayId ?? model?.googlePlayId, rustoreId: dto.rustoreId ?? model?.rustoreId, appStoreId: dto.appStoreId ?? model?.appStoreId, price: dto.price ?? model?.price, enabled: dto.enabled ?? model?.enabled ?? false, order: dto.order ?? model?.order ?? 0, cardsOrder: dto.cardsOrder ?? model?.cardsOrder ?? const [], ); } }