import 'package:calendar_view/utils/calendar_util.dart';
import 'package:flutter/material.dart';

class SchedualItem extends StatefulWidget {
  const SchedualItem(
      {super.key, required this.scheduleData, required this.itemHeight});
  final Schedule scheduleData;
  final double itemHeight;

  @override
  State<SchedualItem> createState() => _SchedualItemState();
}

class _SchedualItemState extends State<SchedualItem> {
  ScheduleType get scheduleType => widget.scheduleData.type;
  Schedule get schedule => widget.scheduleData;
  bool _isSelected = false; // 是否选中状态
  ////TODO:[Gavin] 监听一个popupController的关闭事件,关闭时将选中状态置为false
  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        customBorder: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(5),
        ),
        onTap: () {
          setState(() {
            _isSelected = !_isSelected;
          });
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              backgroundColor: scheduleType.color,
              duration: const Duration(milliseconds: 500),
              content: Text('点击了${scheduleType.typeName}'),
            ),
          );
        },
        hoverColor: const Color.fromARGB(255, 227, 228, 228),
        child: Container(
          height: widget.itemHeight,
          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
          decoration: BoxDecoration(
            color: _isSelected ? scheduleType.color : Colors.transparent,
            borderRadius: BorderRadius.circular(5),
          ),
          child: Row(
            children: [
              Container(
                width: 6,
                height: 6,
                decoration: BoxDecoration(
                  color: scheduleType.color,
                  borderRadius: BorderRadius.circular(5),
                ),
              ),
              const SizedBox(width: 5),
              Text(
                _getStartTime(schedule.startTime),
                style: TextStyle(
                  fontSize: 12,
                  color: _isSelected
                      ? Colors.white
                      : const Color.fromARGB(167, 0, 0, 0),
                ),
                overflow: TextOverflow.ellipsis,
              ),
              Expanded(
                child: Text(
                  breakWord(schedule.title),
                  style: TextStyle(
                      fontSize: 12,
                      color: _isSelected ? Colors.white : Colors.black),
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  static String breakWord(String text) {
    if (text.isEmpty) {
      return text;
    }
    String breakWord = ' ';
    for (var element in text.runes) {
      breakWord += String.fromCharCode(element);
      breakWord += '\u200B';
    }
    return breakWord;
  }

  static String _getStartTime(DateTime? startTime) {
    if (startTime == null) {
      // TODO: i18n
      return '全天';
    }
    return '${startTime.hour}:${startTime.minute}';
  }
}