import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/devices/model/schedule_model.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'empty_schedule.dart'; // for SVG icons

class ScheduleListView extends StatelessWidget {
  final List<ScheduleModel> listSchedule;
  final Function(String) onDismissed;
  final Function(String, bool) onToggleSchedule;
  const ScheduleListView({
    Key? key,
    required this.listSchedule,
    required this.onDismissed,
    required this.onToggleSchedule,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return listSchedule.isNotEmpty
        ? SizedBox(
            child: ListView.builder(
              shrinkWrap: true,
              itemCount: listSchedule.length,
              itemBuilder: (context, index) {
                return Dismissible(
                  key: Key(listSchedule[index].scheduleId),
                  background: Container(
                    padding: const EdgeInsets.only(right: 10),
                    alignment: AlignmentDirectional.centerEnd,
                    decoration: const ShapeDecoration(
                      color: Color(0xFFFF0000),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(20)),
                      ),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.only(bottom: 10, right: 10),
                      child: SvgPicture.asset(
                        Assets.assetsDeleteIcon,
                        width: 20,
                        height: 22,
                      ),
                    ),
                  ),
                  direction: DismissDirection.endToStart,
                  onDismissed: (direction) {
                    onDismissed(listSchedule[index].scheduleId);

                    ScaffoldMessenger.of(context).showSnackBar(
                      const SnackBar(content: Text('Schedule removed')),
                    );
                  },
                  child: InkWell(
                    onTap: () {},
                    child: DefaultContainer(
                      padding: const EdgeInsets.all(20),
                      child: Row(
                        children: [
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                BodyLarge(
                                  text: listSchedule[index].time,
                                  fontWeight: FontWeight.w500,
                                  fontColor: Colors.black,
                                  fontSize: 22,
                                ),
                                Text(listSchedule[index].days.join(' ')),
                                Text(
                                    'Function ${listSchedule[index].function.value ? "ON" : "OFF"}'),
                              ],
                            ),
                          ),
                          Expanded(
                            child: ListTile(
                              contentPadding: EdgeInsets.zero,
                              leading: const BodyMedium(
                                text: '',
                                fontWeight: FontWeight.normal,
                              ),
                              trailing: Transform.scale(
                                scale: .8,
                                child: CupertinoSwitch(
                                  value: listSchedule[index].enable,
                                  onChanged: (value) {
                                    onToggleSchedule(
                                      listSchedule[index].scheduleId,
                                      value,
                                    );
                                  },
                                  applyTheme: true,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              },
            ),
          )
        : const Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Center(child: EmptySchedule()),
            ],
          );
  }
}
