import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controllers/home_controller.dart';
import '../../../widgets/banner_slider.dart';
import '../../../widgets/monitoring_card.dart';

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

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

    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildHeader(context),
            BannerSlider(
              bannerImages: controller.bannerImages,
              currentCarouselIndex: controller.currentCarouselIndex,
              onPageChanged: controller.updateCarouselIndex,
            ),
            const SizedBox(height: 15),
            _buildSectionHeader(),
            Expanded(child: _buildMonitoringList()),
          ],
        ),
      ),
    );
  }

  Widget _buildHeader(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(20),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          const Text(
            'Halo Perawat',
            style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
          ),
          InkWell(
            onTap: () => controller.showLogoutDialog(context),
            child: Container(
              padding: const EdgeInsets.all(8),
              decoration: BoxDecoration(
                border: Border.all(color: Colors.grey[300]!),
                borderRadius: BorderRadius.circular(10),
              ),
              child: const Icon(Icons.logout, color: Colors.red),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildSectionHeader() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          const Text(
            'Monitoring Infus',
            style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
          ),
          TextButton(
            onPressed: () => Get.toNamed('/all-monitoring'),
            child: const Text(
              'Lihat Semua',
              style: TextStyle(color: Color(0xFF0091EA)),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildMonitoringList() {
    return Obx(() {
      if (controller.isLoading.value) {
        return const Center(
          child: CircularProgressIndicator(color: Color(0xFF0091EA)),
        );
      }

      if (controller.monitoringList.isEmpty) {
        return _buildEmptyState();
      }

      return ListView.builder(
        padding: const EdgeInsets.symmetric(horizontal: 20),
        itemCount: controller.monitoringList.length,
        itemBuilder: (context, index) {
          final item = controller.monitoringList[index];
          return MonitoringCard(
            monitoring: item,
            onTap: () => Get.toNamed(
              '/device-detail',
              arguments: {
                'deviceId': item.deviceId,
                'roomId': item.roomId,
              },
            ),
          );
        },
      );
    });
  }

  Widget _buildEmptyState() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(
            Icons.medical_services_outlined,
            size: 80,
            color: Colors.grey[400],
          ),
          const SizedBox(height: 16),
          Text(
            'Tidak ada data monitoring',
            style: TextStyle(fontSize: 16, color: Colors.grey[600]),
          ),
        ],
      ),
    );
  }
}