import 'package:didvan/utils/action_sheet.dart';
import 'package:didvan/views/home/main/main_page_state.dart';
import 'package:didvan/views/widgets/didvan/slider.dart';
import 'package:didvan/views/widgets/skeleton_image.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher_string.dart';

class MainPageBanner extends StatelessWidget {
  final bool isFirst;
  const MainPageBanner({super.key, required this.isFirst});

  @override
  Widget build(BuildContext context) {
    final state = context.read<MainPageState>();
    return DidvanSlider(
      itemBuilder: (context, index, realIndex) {
        final item = state.content!.banners[isFirst ? 0 : 1][index];
        return Padding(
          padding: const EdgeInsets.symmetric(horizontal: 4),
          child: GestureDetector(
            onTap: () => item.link == null || item.link!.isEmpty
                ? ActionSheetUtils(context)
                    .openInteractiveViewer(context, item.image, false)
                : launchUrlString(item.link!, mode: LaunchMode.inAppWebView),
            child: SkeletonImage(
              imageUrl: item.image,
            ),
          ),
        );
      },
      itemCount: state.content!.banners[isFirst ? 0 : 1].length,
      viewportFraction: 1,
      enableIndicator: true,
      height: (MediaQuery.of(context).size.width - 8) * 9.0 / 16.0,
    );
  }
}
