import 'package:flutter/material.dart';
import 'package:prosapp_web_app/models/schedules_entity.dart';

class ScheduleDayTile extends StatelessWidget {
  final String day;
  final ScheduleEntity schedule;
  final Function(bool) onEnableChanged;
  final Function(bool) onContinuousDayChanged;
  final Function(TimeOfDay) onRange1Hour1Pick;
  final Function(TimeOfDay) onRange1Hour2Pick;
  final Function(TimeOfDay) onRange2Hour1Pick;
  final Function(TimeOfDay) onRange2Hour2Pick;

  const ScheduleDayTile({
    super.key,
    required this.day,
    required this.schedule,
    required this.onEnableChanged,
    required this.onContinuousDayChanged,
    required this.onRange1Hour1Pick,
    required this.onRange1Hour2Pick,
    required this.onRange2Hour1Pick,
    required this.onRange2Hour2Pick,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        SwitchListTile(
          title: Text(day),
          value: schedule.enabled,
          onChanged: onEnableChanged,
        ),
        if (schedule.enabled) ...[
          SwitchListTile(
            title: const Text('Jornada continua'),
            value: schedule.continuousDay,
            onChanged: onContinuousDayChanged,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              timePickerField(
                context: context,
                text: ScheduleEntity.getFormatTime(schedule.range1Hour1),
                onPick: onRange1Hour1Pick,
              ),
              Center(
                child: Container(
                  width: 10,
                  height: 1,
                  color: Colors.black,
                ),
              ),
              if (!schedule.continuousDay) ...[
                timePickerField(
                  context: context,
                  text: ScheduleEntity.getFormatTime(schedule.range1Hour2),
                  onPick: onRange1Hour2Pick,
                ),
                const SizedBox(width: 10),
                timePickerField(
                  context: context,
                  text: ScheduleEntity.getFormatTime(schedule.range2Hour1),
                  onPick: onRange2Hour1Pick,
                ),
                Center(
                  child: Container(
                    width: 10,
                    height: 1,
                    color: Colors.black,
                  ),
                ),
              ],
              timePickerField(
                context: context,
                text: ScheduleEntity.getFormatTime(schedule.range2Hour2),
                onPick: onRange2Hour2Pick,
              ),
            ],
          ),
        ],
      ],
    );
  }

  Widget timePickerField({
    required BuildContext context,
    required String? text,
    required Function(TimeOfDay) onPick,
  }) {
    return Expanded(
      child: TextField(
        readOnly: true,
        controller: TextEditingController(
          text: text,
        ),
        textAlign: TextAlign.center,
        onTap: () async {
          final TimeOfDay? pickedTime = await showTimePicker(
            context: context,
            initialTime: TimeOfDay.now(),
          );
          if (pickedTime != null) {
            onPick.call(pickedTime);
          }
        },
        decoration: const InputDecoration(
          hintText: 'Hora',
          border: UnderlineInputBorder(),
        ),
      ),
    );
  }
}
