import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:supplier_new/common/settings.dart';

class DeliveryCalendarScreen extends StatefulWidget {
  const DeliveryCalendarScreen({super.key});

  @override
  State<DeliveryCalendarScreen> createState() => _DeliveryCalendarScreenState();
}

class _DeliveryCalendarScreenState extends State<DeliveryCalendarScreen> {
  DateTime _focusedMonth = DateTime(2025, 10);
  late List<Map<String, dynamic>> _calendarData;

  @override
  void initState() {
    super.initState();
    _calendarData = _generateCalendarData();
  }

  List<Map<String, dynamic>> _generateCalendarData() {
    return [
      {"day": 1, "status": "Delivered"},
      {"day": 2, "status": "Delivered"},
      {"day": 3, "status": "Rescheduled"},
      {"day": 4, "status": "Delivered"},
      {"day": 5, "status": "Delivered"},
      {"day": 6, "status": "Delivered"},
      {"day": 7, "status": "Cancelled"},
      {"day": 8, "status": "Delivered"},
      {"day": 9, "status": "Delivered"},
      {"day": 10, "status": "Delivered"},
      {"day": 11, "status": "Cancelled"},
      {"day": 12, "status": "Delivery"},
      {"day": 13, "status": "Delivery"},
      {"day": 14, "status": "Delivery"},
      {"day": 15, "status": "Delivery"},
      {"day": 16, "status": "Delivery"},
      {"day": 17, "status": "Delivery"},
      {"day": 18, "status": "Delivery"},
      {"day": 19, "status": "Delivery"},
      {"day": 20, "status": "Delivery"},
      {"day": 21, "status": "Delivery"},
      {"day": 22, "status": "Delivery"},
      {"day": 23, "status": "Delivery"},
      {"day": 24, "status": "Delivery"},
      {"day": 25, "status": "Delivery"},
      {"day": 26, "status": "Delivery"},
    ];
  }

  Color _getBackgroundColor(String status) {
    switch (status) {
      case "Delivered":
        return const Color(0xFFE6F4EA);
      case "Cancelled":
        return const Color(0xFFFDE8E8);
      case "Rescheduled":
        return const Color(0xFFF2F2F2);
      case "Delivery":
        return const Color(0xFFEFF4FF);
      default:
        return Colors.white;
    }
  }

  Color _getTextColor(String status) {
    switch (status) {
      case "Delivered":
        return Colors.green;
      case "Cancelled":
        return Colors.red;
      case "Rescheduled":
        return Colors.black54;
      case "Delivery":
        return const Color(0xFF3B6FE0);
      default:
        return Colors.black87;
    }
  }

  Widget _getStatusIcon(String status) {
    switch (status) {
      case "Delivered":
        return const Icon(Icons.check, size: 16, color: Colors.green);
      case "Cancelled":
        return const Icon(Icons.close, size: 16, color: Colors.red);
      case "Rescheduled":
        return const Icon(Icons.access_time, size: 16, color: Colors.black54);
      case "Delivery":
        return const Icon(Icons.local_shipping, size: 16, color: Color(0xFF3B6FE0));
      default:
        return const SizedBox.shrink();
    }
  }

  String _getMonthYear() {
    return DateFormat('MMM yyyy').format(_focusedMonth).toUpperCase();
  }

  int _daysInMonth(DateTime date) {
    final firstDayThisMonth = DateTime(date.year, date.month, 1);
    final firstDayNextMonth = DateTime(date.year, date.month + 1, 1);
    return firstDayNextMonth.difference(firstDayThisMonth).inDays;
  }

  @override
  Widget build(BuildContext context) {
    final int totalDays = _daysInMonth(_focusedMonth);
    final int firstWeekday = DateTime(_focusedMonth.year, _focusedMonth.month, 1).weekday;
    final int totalSlots = totalDays + (firstWeekday - 1);

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new, color: Colors.black),
          onPressed: () => Navigator.pop(context),
        ),
        title: Text("Calendar", style: fontTextStyle(16, Colors.black, FontWeight.w600)),
        actions: const [
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 8),
            child: Icon(Icons.calendar_month_outlined, color: Color(0xFF8270DB)),
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 8),
            child: Icon(Icons.notifications_none_rounded, color: Colors.black87),
          ),
        ],
      ),
      body: Column(
        children: [
          const SizedBox(height: 8),
          Text(_getMonthYear(), style: fontTextStyle(16, Colors.black, FontWeight.w600)),
          const SizedBox(height: 8),

          // Weekdays Row
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 4),
            child: Row(
              children: const [
                Expanded(child: Center(child: Text("MON"))),
                Expanded(child: Center(child: Text("TUE"))),
                Expanded(child: Center(child: Text("WED"))),
                Expanded(child: Center(child: Text("THU"))),
                Expanded(child: Center(child: Text("FRI"))),
                Expanded(child: Center(child: Text("SAT"))),
                Expanded(child: Center(child: Text("SUN"))),
              ],
            ),
          ),
          const SizedBox(height: 8),

          // Calendar Grid
          Expanded(
            child: GridView.builder(
              padding: const EdgeInsets.symmetric(horizontal: 4),
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 7,
                crossAxisSpacing: 4,
                mainAxisSpacing: 4,
              ),
              itemCount: totalSlots,
              itemBuilder: (context, index) {
                if (index < firstWeekday - 1) {
                  return const SizedBox.shrink();
                }
                final day = index - (firstWeekday - 2);
                final status = _calendarData
                    .firstWhere(
                      (item) => item['day'] == day,
                  orElse: () => {"status": ""},
                )['status']
                    .toString();

                return Container(
                  decoration: BoxDecoration(
                    color: _getBackgroundColor(status),
                    borderRadius: BorderRadius.circular(8),
                    border: Border.all(color: Colors.grey.shade300),
                  ),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        "$day",
                        style: fontTextStyle(13, _getTextColor(status), FontWeight.w600),
                      ),
                      const SizedBox(height: 4),
                      _getStatusIcon(status),
                    ],
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
