import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tone_snap/components/circular_notch_clipper.dart';
import 'package:tone_snap/components/my_marquee_text.dart';
import 'package:tone_snap/modules/sidea/widgets/head_label.dart';
import 'package:tone_snap/data/models/voice_model.dart';
import 'package:tone_snap/generated/assets.dart';
import 'package:tone_snap/modules/sidea/home/home_controller.dart';
import 'package:tone_snap/utils/obj_util.dart';

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

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

    return Column(
      children: [
        HeadLabel(
          assets: Assets.sideATheMonster,
          width: 208.w,
          height: 43.h,
        ),
        _buildGrid(context),
      ],
    );
  }

  Widget _buildGrid(BuildContext context) {
    return Expanded(
      child: GridView.builder(
        padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 6.h),
        itemCount: controller.voiceList.length,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3,
          mainAxisSpacing: 20.h,
          crossAxisSpacing: 15.w,
          childAspectRatio: 102/130,
        ),
        itemBuilder: (context, index) {
          return _buildItem(controller.voiceList[index], index);
        },
      ),
    );
  }

  Widget _buildItem(VoiceModel item, int index) {
    return Column(
      children: [
        GestureDetector(
          onTap: () => controller.onTapItem(item),
          child: Stack(
            alignment: Alignment.center,
            children: [
              Obx(() {
                return Visibility(
                  visible: controller.isPlayItem(item),
                  maintainState: true,
                  maintainAnimation: true,
                  maintainSize: true,
                  child: Image.asset(
                    Assets.sideASubtract,
                    width: 100.w,
                    height: 100.w,
                    fit: BoxFit.fill,
                  ),
                );
              }),
              Stack(
                clipBehavior: Clip.none,
                children: [
                  Visibility(
                    visible: ObjUtil.isNotEmpty(item.cover),
                    child: ClipPath(
                      clipper: CircularNotchClipper(
                        notchRadius: 20,
                      ),
                      child: ClipRRect(
                        borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(85.w/2),
                          topRight: Radius.circular(85.w/2),
                          bottomLeft: Radius.circular(85.w/2),
                          bottomRight: const Radius.circular(2),
                        ),
                        child: Image.asset(
                          '${item.cover}',
                          width: 85.w,
                          height: 85.w,
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                  ),
                  Positioned(
                    right: -10.w,
                    bottom: -10.h,
                    child: Obx(() {
                      return GestureDetector(
                        onTap: () => controller.onTapPlayBarPlay(item),
                        child: Image.asset(
                          controller.isPlayItem(item) ? Assets.sideAPlaying : Assets.sideANotPlayed,
                          width: 30.w,
                          height: 30.w,
                        ),
                      );
                    }),
                  ),
                ],
              ),
            ],
          ),
        ),
        SizedBox(height: 8.h),
        Expanded(
          child: MyMarqueeText(
            text: item.name,
            textStyle: TextStyle(
              color: Colors.white,
              fontSize: 16.sp,
              fontWeight: FontWeight.w500,
            ),
          ),
        ),
      ],
    );
  }
}
