import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tone_snap/components/base_scrollbar.dart';
import 'package:tone_snap/data/enum/music_type.dart';
import 'package:tone_snap/modules/sideb/search_result/search_result_controller.dart';
import 'package:tone_snap/modules/sideb/search_result_child_optimum/search_result_child_optimum_controller.dart';
import 'package:tone_snap/modules/sideb/widgets/music_item.dart';
import 'package:tone_snap/utils/obj_util.dart';

class SearchResultChildOptimumView extends GetView<SearchResultChildOptimumController> {
  const SearchResultChildOptimumView({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(SearchResultChildOptimumController());

    return BaseScrollbar(
      child: ListView.builder(
        itemCount: SearchResultController.to.tabs.length,
        itemBuilder: (context, index) {
          final searchResultTabBarModel = SearchResultController.to.tabs[index];
          final musicList = searchResultTabBarModel.musicList?.map((e) => e.obs).toList();
          if (index == 0) {
            return Container();
          }
          return Padding(
            padding: const EdgeInsets.symmetric(vertical: 14).h,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 18).w,
                  child: Text(
                    ObjUtil.getStr(searchResultTabBarModel.title),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 16.sp,
                    ),
                  ),
                ),
                if (musicList != null) ...[
                  SizedBox(height: 12.h),
                  SizedBox(
                    child: ListView.builder(
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      itemCount: musicList.length,
                      itemBuilder: (context, index) {
                        return MusicItem(
                          musicModel: musicList[index],
                          showDownload: musicList[index].value.musicType == MusicType.musicVideoTypeAtv.name,
                          showMore: musicList[index].value.musicType == MusicType.musicVideoTypeAtv.name,
                          onTapItem: () => controller.onTapItem(musicList[index].value),
                        );
                      },
                    ),
                  ),
                ],
              ],
            ),
          );
        },
      ),
    );
  }
}
