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

class HomePatientView extends GetView<HomePatientController> {
  const HomePatientView({super.key});

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

    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: RefreshIndicator(
          onRefresh: controller.refreshData,
          child: SingleChildScrollView(
            physics: const AlwaysScrollableScrollPhysics(),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // Header
                Padding(
                  padding: const EdgeInsets.all(20),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Expanded(
                        child: Obx(
                          () => Text(
                            'Halo ${controller.patientName.value}',
                            style: const TextStyle(
                              fontSize: 24,
                              fontWeight: FontWeight.bold,
                            ),
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                      ),
                      const SizedBox(width: 12),
                      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),
                        ),
                      ),
                    ],
                  ),
                ),

                // Banner Slider
                BannerSlider(
                  bannerImages: controller.bannerImages,
                  currentCarouselIndex: controller.currentCarouselIndex,
                  onPageChanged: controller.updateCarouselIndex,
                ),

                const SizedBox(height: 15),

                // Monitoring Infus Section
                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: () {
                          // TODO: Navigate to monitoring detail
                        },
                        child: const Text(
                          'See all',
                          style: TextStyle(
                            color: Color(0xFF2196F3),
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),

                // Monitoring Card - Gunakan MonitoringCard dari widgets
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 20),
                  child: Obx(() {
                    if (controller.isLoading.value) {
                      return Container(
                        padding: const EdgeInsets.all(32),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(16),
                          border: Border.all(color: Colors.grey[200]!),
                        ),
                        child: const Center(child: CircularProgressIndicator()),
                      );
                    }

                    final infusData = controller.currentInfusData.value;

                    // Konversi ke InfusMonitoring untuk MonitoringCard
                    final monitoring = InfusMonitoring(
                      id: controller.deviceId.value,
                      patientName: controller.patientName.value,
                      deviceId: infusData.deviceId,
                      room: infusData.room,
                      roomId: '', // Not needed for display
                      dropsPerMinute: infusData.dropsPerMinute,
                      lastUpdate:
                          DateTime.now(), // Will be calculated from updateTime
                      deviceStatus: infusData.deviceStatus,
                    );

                    return MonitoringCard(
                      monitoring: monitoring,
                      onTap: () {
                        // TODO: Navigate to detail
                      },
                    );
                  }),
                ),

                const SizedBox(height: 15),

                // History Data Section
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 20),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      const Text(
                        'History Data',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      TextButton(
                        onPressed: () {
                          // TODO: Navigate to history detail
                        },
                        child: const Text(
                          'See all',
                          style: TextStyle(
                            color: Color(0xFF2196F3),
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),

                // Chart Card - Gunakan HistoryGrafikCard dari widgets
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 20),
                  child: Obx(() {
                    if (controller.isLoadingHistory.value) {
                      return Container(
                        padding: const EdgeInsets.all(32),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(12),
                          border: Border.all(color: Colors.grey[200]!),
                        ),
                        child: const Center(child: CircularProgressIndicator()),
                      );
                    }

                    return HistoryGrafikCard(
                      dataPoints: controller.chartDataPoints.toList(),
                      onBookmarkTap: () {
                        // TODO: Bookmark functionality
                      },
                    );
                  }),
                ),

                const SizedBox(height: 15),

                // History Notifikasi Section
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 20),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      const Text(
                        'History Notifikasi',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      TextButton(
                        onPressed: () {
                          // TODO: Navigate to notifications
                        },
                        child: const Text(
                          'See all',
                          style: TextStyle(
                            color: Color(0xFF2196F3),
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),

                // Notifications List
                Obx(() {
                  if (controller.isLoadingNotifications.value) {
                    return const Padding(
                      padding: EdgeInsets.all(32),
                      child: Center(child: CircularProgressIndicator()),
                    );
                  }

                  if (controller.notificationsList.isEmpty) {
                    return Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 20),
                      child: Container(
                        padding: const EdgeInsets.all(32),
                        decoration: BoxDecoration(
                          color: Colors.grey[50],
                          borderRadius: BorderRadius.circular(12),
                          border: Border.all(color: Colors.grey[200]!),
                        ),
                        child: Center(
                          child: Column(
                            children: [
                              Icon(
                                Icons.notifications_none,
                                size: 48,
                                color: Colors.grey[400],
                              ),
                              const SizedBox(height: 12),
                              Text(
                                'Belum ada notifikasi',
                                style: TextStyle(
                                  fontSize: 14,
                                  color: Colors.grey[600],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    );
                  }

                  return ListView.builder(
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    padding: const EdgeInsets.symmetric(horizontal: 20),
                    itemCount: controller.notificationsList.length,
                    itemBuilder: (context, index) {
                      final notification = controller.notificationsList[index];
                      return NotificationPatientCard(
                        title: notification.title,
                        message: notification.message,
                        room: notification.room,
                        deviceId: notification.deviceId,
                        timeAgo: notification.timeAgo,
                        onTap: () {
                          // TODO: Handle notification tap
                        },
                      );
                    },
                  );
                }),

                const SizedBox(height: 20),
              ],
            ),
          ),
        ),
      ),

      // Floating Action Button - Call Nurse
      floatingActionButton: FloatingActionButton(
        onPressed: () => controller.showCallDialog(context),
        backgroundColor: const Color(0xFF2196F3),
        child: const Icon(Icons.phone, color: Colors.white),
      ),
    );
  }
}
