import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hallo_doctor_doctor_app/app/modules/widgets/empty_list_widget.dart';
import 'package:hallo_doctor_doctor_app/app/styles/styles.dart';
import '../controllers/order_controller.dart';
import 'package:intl/intl.dart';

class OrderView extends GetView<OrderController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            'Booking Appointment'.tr,
            style: Styles.appBarTextStyle,
          ),
          centerTitle: true,
          backgroundColor: Colors.white,
          actions: [
            IconButton(
              icon: Obx(() => Icon(
                    controller.isLatestFirst.value
                        ? Icons.arrow_downward
                        : Icons.arrow_upward,
                    color: Colors.black,
                  )),
              onPressed: () => controller.toggleSortOrder(),
              tooltip: 'Sort by time',
            ),
          ],
        ),
        body: Container(
          child: controller.obx(
            (categoryMap) {
              if (categoryMap == null || categoryMap.isEmpty) {
                return Center(child: EmptyList(msg: 'no order'.tr));
              }

              List<Widget> sections = [];
              ['Upcoming', 'Today', 'Past'].forEach((category) {
                final appointments = categoryMap[category] ?? [];
                if (appointments.isNotEmpty) {
                  sections.add(
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Padding(
                          padding: const EdgeInsets.all(16.0),
                          child: Text(
                            category.tr,
                            style: TextStyle(
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                        ...appointments
                            .map((appointment) => Card(
                                  child: ListTile(
                                    onTap: () {
                                      Get.toNamed('/order-detail',
                                          arguments: appointment);
                                    },
                                    leading: CircleAvatar(
                                      backgroundImage: appointment.bookByWho
                                                  ?.photoUrl?.isNotEmpty ==
                                              true
                                          ? NetworkImage(
                                              appointment.bookByWho!.photoUrl!)
                                          : AssetImage(
                                                  'assets/images/default-profile.png')
                                              as ImageProvider,
                                    ),
                                    title: Text(
                                      'Appointment with '.tr +
                                          (appointment.bookByWho?.displayName ??
                                              'Unknown'),
                                    ),
                                    subtitle: Text(
                                      'at '.tr +
                                          DateFormat('EEEE, dd, MMMM').format(
                                              appointment.timeSlot ??
                                                  DateTime.now()),
                                    ),
                                    trailing: Wrap(
                                      spacing: 5,
                                      children: [Icon(Icons.arrow_forward_ios)],
                                    ),
                                  ),
                                ))
                            .toList(),
                      ],
                    ),
                  );
                }
              });

              return ListView(
                children: sections,
              );
            },
            onEmpty: Center(child: EmptyList(msg: 'no order'.tr)),
          ),
        ));
  }
}
