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/sideb/enum/browse_type.dart';
import 'package:tone_snap/modules/sideb/widgets/album_item.dart';
import 'package:tone_snap/modules/sideb/widgets/atv_item.dart';
import 'package:tone_snap/modules/sideb/widgets/music_appbar.dart';
import 'package:tone_snap/data/sideb/models/home_model.dart';
import 'package:tone_snap/generated/assets.dart';
import 'package:tone_snap/modules/sideb/widgets/omv_item.dart';
import 'package:tone_snap/utils/obj_util.dart';

import 'home_controller.dart';

class HomeView extends GetView<HomeController> {
  const HomeView({super.key});

  @override
  Widget build(BuildContext context) {
    Get.find<HomeController>();
    return Column(
      children: [
        _buildTitleWidget(),
        _buildListView(context),
      ],
    );
  }

  Widget _buildTitleWidget() {
    return MusicAppbar(
      title: 'Musicoo',
      showBackWidget: false,
      actionOnTap: () {},
      action: Image.asset(
        Assets.sideBLineMenu,
        width: 24.w,
        height: 24.w,
      ),
    );
  }

  Widget _buildListView(BuildContext context) {
    return Expanded(
      child: BaseScrollbar(
        child: Obx(() {
          return ListView.separated(
            itemCount: controller.data.length,
            padding: EdgeInsets.fromLTRB(16.w, 0.h, 16.w, 16.h),
            itemBuilder: (context, index) {
              return _buildColumnItem(controller.data[index]);
            },
            separatorBuilder: (context, index) {
              return SizedBox(height: 16.h);
            },
          );
        }),
      ),
    );
  }

  /// 模版标签
  Widget _labelWidget(String label) {
    return InkWell(
      onTap: () {},
      child: SizedBox(
        height: 40.h,
        child: Row(
          children: [
            Expanded(
              child: Text(
                label,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 18.sp,
                ),
              ),
            ),
            SizedBox(width: 16.w),
            Image.asset(
              Assets.sideBArrowRightItem,
              width: 24.w,
              height: 24.w,
            ),
          ],
        ),
      ),
    );
  }

  /// 垂直列表Item
  Widget _buildColumnItem(HomeModel model) {
    if (!BrowseTypeExtension.isThereAny(model.browseType)) {
      return Container();
    }
    double? itemHeight;
    if (model.browseType == BrowseType.musicVideoTypeAtv.name) {
      itemHeight = 174.h;
    } else if (model.browseType == BrowseType.musicPageTypeAlbum.name || model.browseType == BrowseType.musicPageTypePlaylist.name) {
      itemHeight = 195.h;
    } else if (model.browseType == BrowseType.musicVideoTypeOmv.name) {
      itemHeight = 277.h;
    }
    return SizedBox(
      height: itemHeight,
      child: Column(
        children: [
          _labelWidget(ObjUtil.getStr(model.headerTitle)),
          Expanded(
            child: ListView.separated(
              scrollDirection: Axis.horizontal,
              itemCount: model.contents != null ? model.contents!.length : 0,
              itemBuilder: (context, index) {
                final content = model.contents![index];
                if (model.browseType == BrowseType.musicVideoTypeAtv.name) {
                  return AtvItem(content: content, onTap: () => controller.goPlayMusic(content));
                } else if (model.browseType == BrowseType.musicPageTypeAlbum.name || model.browseType == BrowseType.musicPageTypePlaylist.name) {
                  return AlbumItem(content: content);
                } else if (model.browseType == BrowseType.musicVideoTypeOmv.name) {
                  return OmvItem(content: content);
                }
                return Container();
              },
              separatorBuilder: (context, index) {
                return SizedBox(width: 7.w);
              },
            ),
          ),
        ],
      ),
    );
  }
}
