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/image_network_widget.dart';
import 'package:hello_wallpaper/common/components/refresh/base_easyrefresh.dart';
import 'package:hello_wallpaper/models/wallpaper_model.dart';
import 'package:hello_wallpaper/modules/discover/discover_controller.dart';
import 'package:hello_wallpaper/res/themes/app_sizes.dart';

class DiscoverView extends GetView<DiscoverController> {
  const DiscoverView({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(DiscoverController());
    return GetBuilder<DiscoverController>(
      builder: (controller) {
        return Column(
          children: [
            if (controller.todayNewestList.isNotEmpty) ...[
              _buildTodayNewest(),
              SizedBox(height: 7.h),
            ],
            Expanded(
              child: MediaQuery.removePadding(
                context: context,
                removeTop: true,
                child: Scrollbar(
                  child: BaseEasyRefresh(
                    controller: controller.refreshController,
                    onLoad: controller.onLoad,
                    viewState: controller.viewState,
                    height: discoverListHeight,
                    child: MasonryGridView.count(
                      itemCount: controller.wallpaperDataList.length,
                      crossAxisCount: 2,
                      mainAxisSpacing: 10.w,
                      crossAxisSpacing: 10.w,
                      padding: const EdgeInsets.symmetric(horizontal: 20).w,
                      itemBuilder: (context, index) {
                        return _buildDiscoverItem(controller.wallpaperDataList[index], index);
                      },
                    ),
                  ),
                ),
              ),
            ),
          ],
        );
      },
    );
  }

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

  /// 今日最新
  Widget _buildTodayNewest() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20).w,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(12).r,
        child: Material(
          color: Colors.white,
          child: InkWell(
            onTap: controller.onTapTodayNewest,
            child: Container(
              height: todayNewestHeight,
              padding: const EdgeInsets.symmetric(horizontal: 16).w,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Expanded(
                    child: RichText(
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      text: TextSpan(
                        text: 'Today',
                        style: TextStyle(
                          color: const Color(0xFF666666),
                          fontSize: 12.sp,
                          fontWeight: FontWeight.w600,
                        ),
                        children: [
                          TextSpan(
                            text: '\n${controller.clsName}',
                            style: TextStyle(
                              color: const Color(0xFF333333),
                              fontSize: 16.sp,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  ...List.generate(controller.todayNewestList.length, (index) {
                    final item = controller.todayNewestList[index];
                    return Row(
                      children: [
                        ImageNetworkWidget(
                          url: item.previewThumb,
                          width: 42.w,
                          height: 42.w,
                          radius: 4.r,
                        ),
                        if (index != controller.todayNewestList.length - 1) ...[
                          SizedBox(width: 8.w),
                        ],
                      ],
                    );
                  }),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
