
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({Key? key, required this.details, this.crossedOut = false}) : super(key: key);

  @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;
    double headerHeight = 50;
    const double footerHeight = 5;
    final double infoHeight = appointmentHeight - (headerHeight + footerHeight);
    if (infoHeight < 0) headerHeight += infoHeight;

    return Stack(
      children: [
        Column(
          children: [
            Container(
              padding: const EdgeInsets.all(3),
              height: headerHeight,
              alignment: Alignment.topLeft,
              decoration: BoxDecoration(
                shape: BoxShape.rectangle,
                borderRadius: const BorderRadius.only(
                  topLeft: Radius.circular(5),
                  topRight: 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 ?? "?",
                        maxLines: 3,
                        overflow: TextOverflow.ellipsis,
                        softWrap: true,
                        style: const TextStyle(
                          color: Colors.white,
                          fontSize: 10,
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ),
            Visibility(
              visible: meeting.notes != null && infoHeight > 10,
              replacement: Container(
                color: meeting.color,
                height: infoHeight,
              ),
              child: Container(
                height: infoHeight,
                padding: const EdgeInsets.fromLTRB(3, 5, 3, 2),
                color: meeting.color.withOpacity(0.8),
                alignment: Alignment.topLeft,
                child: SingleChildScrollView(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        meeting.notes ?? "",
                        style: const TextStyle(
                          color: Colors.white,
                          fontSize: 10,
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ),
            Container(
              height: footerHeight,
              decoration: BoxDecoration(
                shape: BoxShape.rectangle,
                borderRadius: const BorderRadius.only(
                  bottomLeft: Radius.circular(5),
                  bottomRight: Radius.circular(5),
                ),
                color: meeting.color,
              ),
            ),
          ],
        ),
        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(),
              ),
            )
          ),
        ),
      ],
    );
  }
}
