
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_calendar/calendar.dart';

import 'CrossPainter.dart';

class AppointmentComponent extends StatefulWidget {
  final CalendarAppointmentDetails details;
  final bool crossedOut;
  const AppointmentComponent({super.key, required this.details, this.crossedOut = false});

  @override
  State<AppointmentComponent> createState() => _AppointmentComponentState();
}

class _AppointmentComponentState extends State<AppointmentComponent> {
  @override
  Widget build(BuildContext context) {
    final Appointment meeting = widget.details.appointments.first;
    final appointmentHeight = widget.details.bounds.height;

    return Stack(
      children: [
        Column(
          children: [
            Container(
              padding: const EdgeInsets.all(3),
              height: appointmentHeight,
              alignment: Alignment.topLeft,
              decoration: BoxDecoration(
                shape: BoxShape.rectangle,
                borderRadius: const BorderRadius.all(Radius.circular(5)),
                color: meeting.color,
              ),
              child: SingleChildScrollView(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    FittedBox(
                      fit: BoxFit.fitWidth,
                      child: Text(
                        meeting.subject,
                        style: const TextStyle(
                          color: Colors.white,
                          fontSize: 15,
                          fontWeight: FontWeight.w500,
                        ),
                        maxLines: 1,
                        softWrap: false,
                      ),
                    ),
                    FittedBox(
                      fit: BoxFit.fitWidth,
                      child: Text(
                        (meeting.location == null || meeting.location!.isEmpty ? " " : meeting.location!),
                        maxLines: 3,
                        overflow: TextOverflow.ellipsis,
                        softWrap: true,
                        style: const TextStyle(
                          color: Colors.white,
                          fontSize: 10,
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ),
          ],
        ),
        Visibility(
          visible: widget.crossedOut,
          child: Positioned.fill(
            child: Container(
              decoration: BoxDecoration(
                border: Border.all(
                  width: 2,
                  color: Colors.red.withAlpha(200),
                ),
                borderRadius: const BorderRadius.all(Radius.circular(5)),
              ),
              child: CustomPaint(
                painter: CrossPainter(),
              ),
            )
          ),
        ),
      ],
    );
  }
}
