import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:get/get.dart';
import 'package:hello_wallpaper/common/components/button/base_textbutton.dart';
import 'package:hello_wallpaper/common/components/image_network_widget.dart';
import 'package:hello_wallpaper/common/components/view_state_widget.dart';
import 'package:hello_wallpaper/models/wallpaper_model.dart';
import 'package:hello_wallpaper/modules/favorite/favorite_controller.dart';

class FavoriteView extends GetView<FavoriteController> {
  const FavoriteView({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(FavoriteController());
    return GetBuilder<FavoriteController>(
      builder: (logic) {
        return Column(
          children: [
            Expanded(
              child: ViewStateWidget(
                viewState: controller.viewState,
                child: MediaQuery.removePadding(
                  context: context,
                  removeTop: true,
                  child: Scrollbar(
                    controller: controller.scrollController,
                    child: MasonryGridView.count(
                      controller: controller.scrollController,
                      itemCount: controller.favoriteList.length,
                      crossAxisCount: 3,
                      mainAxisSpacing: 7.w,
                      crossAxisSpacing: 7.w,
                      physics: const BouncingScrollPhysics(),
                      padding: const EdgeInsets.symmetric(horizontal: 20).w,
                      itemBuilder: (context, index) {
                        return _buildFavoriteItem(controller.favoriteList[index], index);
                      },
                    ),
                  ),
                ),
              ),
            ),
            if (controller.viewState == ViewState.empty && controller.todayHottestList.isNotEmpty) ...[
              _buildTodayHottest(),
            ],
          ],
        );
      },
    );
  }

  Widget _buildFavoriteItem(WallpaperData item, int index) {
    return GestureDetector(
      onTap: () => controller.onTapItem(index, true),
      onLongPress: () => controller.onLongPress(index),
      child: ImageNetworkWidget(
        url: item.previewThumb,
        radius: 8.r,
      ),
    );
  }

  /// 今日最热门壁纸
  Widget _buildTodayHottest() {
    return Column(
      children: [
        Text(
          'Today Hottest Wallpapers',
          style: TextStyle(
            color: Colors.white,
            fontSize: 18.sp,
            fontWeight: FontWeight.w600,
          ),
        ),
        SizedBox(height: 24.h),
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16.5).w,
          child: Row(
            children: controller.todayHottestList.asMap().entries.map((e) {
              return Expanded(
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 3.5).w,
                  child: GestureDetector(
                    onTap: () => controller.onTapItem(e.key, false),
                    child: ImageNetworkWidget(
                      url: e.value.previewThumb,
                      height: 183.h,
                      radius: 8.r,
                    ),
                  ),
                ),
              );
            }).toList(),
          ),
        ),
        SizedBox(height: 28.h),
        BaseTextButton(
          width: 159.w,
          height: 44.h,
          label: 'View more',
          onTap: controller.onTapViewMore,
        ),
        SizedBox(height: 35.h),
      ],
    );
  }
}
