import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
import 'package:syncrow_web/pages/device_managment/schedule_device/bloc/schedule_bloc.dart';
import 'package:syncrow_web/pages/device_managment/schedule_device/schedule_widgets/count_down_button.dart';
import 'package:syncrow_web/pages/device_managment/schedule_device/schedule_widgets/count_down_inching_view.dart';
import 'package:syncrow_web/pages/device_managment/schedule_device/schedule_widgets/inching_mode_buttons.dart';
import 'package:syncrow_web/pages/device_managment/schedule_device/schedule_widgets/schedule_header.dart';
import 'package:syncrow_web/pages/device_managment/schedule_device/schedule_widgets/schedule_managment_ui.dart';
import 'package:syncrow_web/pages/device_managment/schedule_device/schedule_widgets/schedule_mode_buttons.dart';
import 'package:syncrow_web/pages/device_managment/schedule_device/schedule_widgets/schedule_mode_selector.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/helper/add_schedule_dialog_helper.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/models/schedule_entry.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/models/water_heater_status_model.dart';

class BuildScheduleView extends StatelessWidget {
  const BuildScheduleView({
    super.key,
    required this.deviceUuid,
    required this.category,
    required this.countdownCode,
    this.code,
    required this.deviceType,
  });
  final String deviceUuid;
  final String category;
  final String? code;
  final String? countdownCode;
  final String deviceType;

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => ScheduleBloc(
        deviceId: deviceUuid,
      )
        ..add(ScheduleGetEvent(category: category))
        ..add(ScheduleFetchStatusEvent(
            deviceId: deviceUuid, countdownCode: countdownCode ?? '')),
      child: Dialog(
        backgroundColor: Colors.white,
        insetPadding: const EdgeInsets.all(20),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(20),
        ),
        child: SizedBox(
          width: 700,
          child: SingleChildScrollView(
            child: Padding(
              padding:
                  const EdgeInsets.symmetric(horizontal: 40.0, vertical: 20),
              child: BlocBuilder<ScheduleBloc, ScheduleState>(
                builder: (context, state) {
                  if (state is ScheduleLoaded) {
                    return Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const ScheduleHeader(),
                        const SizedBox(height: 20),
                        if (deviceType == 'CUR_2')
                          const SizedBox()
                        else
                          ScheduleModeSelector(
                            countdownCode: countdownCode ?? '',
                            currentMode: state.scheduleMode,
                          ),
                        const SizedBox(height: 20),
                        if (state.scheduleMode == ScheduleModes.schedule)
                          ScheduleManagementUI(
                            deviceType: deviceType,
                            category: category,
                            deviceUuid: deviceUuid,
                            onAddSchedule: () async {
                              final entry = await ScheduleDialogHelper
                                  .showAddScheduleDialog(context,
                                      schedule: ScheduleEntry(
                                        category: category,
                                        time: '',
                                        function: Status(
                                            code: code.toString(), value: null),
                                        days: [],
                                      ),
                                      isEdit: false,
                                      code: code,
                                      deviceType: deviceType);
                              if (entry != null) {
                                context.read<ScheduleBloc>().add(
                                      ScheduleAddEvent(
                                        category: category,
                                        code: entry.function.code,
                                        time: entry.time,
                                        functionOn: entry.function.value,
                                        selectedDays: entry.days,
                                      ),
                                    );
                              }
                            },
                          ),
                        if (deviceType != 'CUR_2')
                          if (state.scheduleMode == ScheduleModes.countdown ||
                              state.scheduleMode == ScheduleModes.inching)
                            CountdownInchingView(
                              deviceId: deviceUuid,
                            ),
                        const SizedBox(height: 20),
                        if (state.scheduleMode == ScheduleModes.countdown)
                          CountdownModeButtons(
                            countDownCode: countdownCode ?? '',
                            isActive: state.isCountdownActive,
                            deviceId: deviceUuid,
                            hours: state.countdownHours,
                            minutes: state.countdownMinutes,
                          ),
                        if (state.scheduleMode == ScheduleModes.inching)
                          InchingModeButtons(
                            isActive: state.isInchingActive,
                            deviceId: deviceUuid,
                            hours: state.inchingHours,
                            minutes: state.inchingMinutes,
                          ),
                        if (state.scheduleMode != ScheduleModes.countdown &&
                            state.scheduleMode != ScheduleModes.inching)
                          ScheduleModeButtons(
                            onSave: () => Navigator.pop(context),
                          ),
                      ],
                    );
                  }
                  return const Center(child: CircularProgressIndicator());
                },
              ),
            ),
          ),
        ),
      ),
    );
  }
}
