import 'package:commons_core/commons_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/i18n/strings.g.dart';
import 'package:tane/services/social_connection.dart';
import 'package:tane/services/social_service.dart';
import 'package:tane/services/social_settings.dart';
import 'package:tane/ui/market_offer_detail_screen.dart';
import 'package:tane/ui/market_widgets.dart';

import '../support/test_support.dart';

Widget _wrap(MarketOfferDetailScreen screen) {
  LocaleSettings.setLocaleSync(AppLocale.en);
  return TranslationProvider(
    child: MaterialApp(
      locale: AppLocale.en.flutterLocale,
      supportedLocales: AppLocaleUtils.supportedLocales,
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      home: screen,
    ),
  );
}

Offer _offer({bool organic = false, String? imageUrl}) => Offer(
      id: 'lot-1',
      authorPubkeyHex: 'ab' * 32,
      summary: 'Tomate rosa de Barbastro',
      type: OfferType.gift,
      category: 'Solanaceae',
      approxGeohash: 'sp3e9',
      isOrganic: organic,
      imageUrl: imageUrl,
    );

void main() {
  testWidgets('renders the offer and a message button for a stranger',
      (tester) async {
    final social = await SocialService.fromRootSeedHex('00' * 32);
    final settings = SocialSettings(InMemorySecretStore());
    await settings.setRelayUrls(const []); // offline: no network in the test
    final connection = SocialConnection(social: social, settings: settings);

    await tester.pumpWidget(_wrap(MarketOfferDetailScreen(
      offer: _offer(organic: true),
      connection: connection,
    )));
    await tester.pumpAndSettle();

    expect(find.text('Tomate rosa de Barbastro'), findsWidgets);
    expect(find.text('Solanaceae'), findsOneWidget);
    expect(find.text('Organic'), findsOneWidget); // eco badge
    expect(find.byKey(const Key('offerDetail.contact')), findsOneWidget);
  });

  testWidgets('hides the message button on my own listing', (tester) async {
    final social = await SocialService.fromRootSeedHex('00' * 32);
    final settings = SocialSettings(InMemorySecretStore());
    await settings.setRelayUrls(const []);
    final connection = SocialConnection(social: social, settings: settings);

    await tester.pumpWidget(_wrap(MarketOfferDetailScreen(
      offer: _offer(),
      connection: connection,
      mine: true,
    )));
    await tester.pumpAndSettle();

    expect(find.byKey(const Key('offerDetail.contact')), findsNothing);
  });

  testWidgets('shows a hero image when the offer has a photo', (tester) async {
    final social = await SocialService.fromRootSeedHex('00' * 32);
    final settings = SocialSettings(InMemorySecretStore());
    await settings.setRelayUrls(const []);
    final connection = SocialConnection(social: social, settings: settings);

    await tester.pumpWidget(_wrap(MarketOfferDetailScreen(
      offer: _offer(imageUrl: 'https://media.example/abc.jpg'),
      connection: connection,
    )));
    await tester.pumpAndSettle();

    expect(find.byType(OfferHeroImage), findsOneWidget);
  });

  testWidgets('shows no hero image when the offer has none', (tester) async {
    final social = await SocialService.fromRootSeedHex('00' * 32);
    final settings = SocialSettings(InMemorySecretStore());
    await settings.setRelayUrls(const []);
    final connection = SocialConnection(social: social, settings: settings);

    await tester.pumpWidget(_wrap(MarketOfferDetailScreen(
      offer: _offer(),
      connection: connection,
    )));
    await tester.pumpAndSettle();

    expect(find.byType(OfferHeroImage), findsNothing);
  });
}
