import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:hello_wallpaper/common/components/keep_alive_wrapper.dart';
import 'package:hello_wallpaper/common/components/navigation_bar/title_bar_widget.dart';
import 'package:hello_wallpaper/modules/home/home_controller.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Obx(() {
            return TitleBarWidget(
              controller.pages[controller.currentIndex.value].label,
              settingsOnTap: controller.onTapSettings,
            );
          }),
          SizedBox(height: 7.h),
          Expanded(
            child: PageView(
              physics: const NeverScrollableScrollPhysics(),
              controller: controller.pageController,
              onPageChanged: (index) => controller.onPageChanged(index),
              children: controller.pages.map((e) => KeepAliveWrapper(child: e.widget)).toList(),
            ),
          ),
        ],
      ),
      bottomNavigationBar: Obx(() {
        return BottomNavigationBar(
          currentIndex: controller.currentIndex.value,
          onTap: (index) => controller.onTapNavigationBar(index),
          items: _bottomNavigationBarItems(),
        );
      }),
    );
  }

  List<BottomNavigationBarItem> _bottomNavigationBarItems() {
    return controller.pages.map((e) {
      return BottomNavigationBarItem(
        icon: Image.asset(
          e.icons[0],
          width: 22.w,
          height: 22.w,
          gaplessPlayback: true,
        ),
        activeIcon: Image.asset(
          e.icons[1],
          width: 22.w,
          height: 22.w,
          gaplessPlayback: true,
        ),
        label: e.label,
      );
    }).toList();
  }
}
