import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tone_snap/components/keep_alive_wrapper.dart';
import 'package:tone_snap/generated/assets.dart';
import 'package:tone_snap/modules/sideb/initial/initial_controller.dart';

class InitialView extends StatelessWidget {
  InitialView({super.key});

  final controller = Get.find<InitialController>();

  @override
  Widget build(BuildContext context) {
    double bottomPadding = MediaQuery.of(context).padding.bottom;
    return Stack(
      children: [
        _buildPageBg(),
        Scaffold(
          backgroundColor: Colors.transparent,
          body: _buildBody(),
          bottomNavigationBar: _buildBottomAppBar(bottomPadding),
        ),
      ],
    );
  }

  Widget _buildPageBg() {
    return Obx(() {
      return Visibility(
        visible: controller.currentIndex.value != 2,
        child: Image.asset(
          Assets.sideBHomeBg,
          width: 1.sw,
          height: 1.sh,
          fit: BoxFit.cover,
        ),
      );
    });
  }

  Widget _buildBody() {
    return PageView(
      physics: const NeverScrollableScrollPhysics(),
      controller: controller.pageController,
      children: controller.pages.map((e) => KeepAliveWrapper(child: e.widget)).toList(),
    );
  }

  Widget _buildBottomAppBar(double bottomPadding) {
    return Container(
      height: kBottomNavigationBarHeight + bottomPadding,
      padding: EdgeInsets.only(bottom: bottomPadding),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(topLeft: Radius.circular(24.r), topRight: Radius.circular(24.r)),
        image: const DecorationImage(
          image: AssetImage(Assets.sideBBnbBg),
          fit: BoxFit.cover,
        ),
      ),
      child: Row(
        children: controller.pages.asMap().entries.map((e) {
          return Expanded(
            child: GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () => controller.onBottomAppBarItemChanged(e.key),
              child: SizedBox(
                height: double.infinity,
                child: FittedBox(
                  fit: BoxFit.none,
                  child: Obx(() {
                    return Visibility(
                      visible: controller.currentIndex.value == e.key,
                      replacement: Image.asset(
                        e.value.icons[0],
                        width: 28.w,
                        height: 28.w,
                      ),
                      child: Image.asset(
                        e.value.icons[1],
                        width: 36.w,
                        height: 36.w,
                        gaplessPlayback: true,
                      ),
                    );
                  }),
                ),
              ),
            ),
          );
        }).toList(),
      ),
    );
  }
}
