import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:wallpaperx/common/components/image_network_widget.dart';
import 'package:wallpaperx/common/components/keep_alive_wrapper.dart';
import 'package:wallpaperx/common/components/navigation_bar/nested_bottom_bar.dart';
import 'package:wallpaperx/entity/image_model.dart';
import 'package:wallpaperx/page/category/category_controller.dart';

class CategoryView extends GetView<CategoryController> {
  const CategoryView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Obx(
        () => Column(
          children: [
            NestedBottomBar(
              controller.title.value,
              titleStyle: TextStyle(
                color: Colors.white,
                fontSize: 24.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
            Expanded(
              child: MediaQuery.removePadding(
                context: context,
                removeTop: true,
                child: Scrollbar(
                  controller: controller.scrollController,
                  child: ListView.separated(
                    controller: controller.scrollController,
                    physics: const BouncingScrollPhysics(),
                    padding: const EdgeInsets.only(top: 10).w,
                    itemCount: controller.categoryData.length,
                    itemBuilder: (context, index) {
                      List keys = controller.categoryData.keys.toList();
                      List list = controller.categoryData[keys[index]] ?? [];
                      List<ImageModel> imageModels =
                          list.map((e) => ImageModel.fromJson(e)).toList();
                      return _buildCatalogItem(keys[index], imageModels);
                    },
                    separatorBuilder: (context, index) {
                      return SizedBox(height: 24.h);
                    },
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildCatalogItem(title, List<ImageModel> imageModels) {
    return Column(
      children: [
        Row(
          children: [
            16.horizontalSpace,
            Expanded(
              child: Text(
                '$title',
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 20.sp,
                ),
              ),
            ),
            if (imageModels.isNotEmpty) ...[
              SizedBox(width: 10.w),
              GestureDetector(
                onTap: () => controller.onTapSingleCls(title),
                child: Row(
                  children: [
                    Text(
                      'More',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 14.sp,
                      ),
                    ),
                    Icon(Icons.keyboard_arrow_right,
                        color: Colors.white, size: 18.w),
                  ],
                ),
              ),
            ],
          ],
        ),
        SizedBox(height: 7.h),
        SizedBox(
          height: 233.h,
          child: ListView.separated(
            physics: const BouncingScrollPhysics(),
            scrollDirection: Axis.horizontal,
            padding: const EdgeInsets.symmetric(horizontal: 16).w,
            itemCount: imageModels.length,
            itemBuilder: (context, index) {
              return _buildImgWidget(imageModels[index], imageModels, index);
            },
            separatorBuilder: (context, index) {
              return SizedBox(width: 8.w);
            },
          ),
        ),
      ],
    );
  }

  Widget _buildImgWidget(
      ImageModel item, List<ImageModel> wallpaperList, int index) {
    return GestureDetector(
      onTap: () => controller.onTapItem(wallpaperList, index),
      child: KeepAliveWrapper(
        child: ImageNetworkWidget(
          url: item.imageUrl,
          width: 126.w,
          height: double.infinity,
          aspectRatio: 126 / 180,
          radius: 12.r,
        ),
      ),
    );
  }
}
