import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/bloc/water_heater_bloc.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/water_heater_status_model.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/widgets/count_down_button.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/widgets/count_down_inching_view.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/widgets/inching_mode_buttons.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/widgets/schedule_header.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/widgets/schedule_managment_ui.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/widgets/schedule_mode_buttons.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/widgets/schedule_mode_selector.dart';

class BuildScheduleView extends StatefulWidget {
  const BuildScheduleView({super.key, required this.status});

  final WaterHeaterStatusModel status;

  @override
  State<BuildScheduleView> createState() => _BuildScheduleViewState();
}

class _BuildScheduleViewState extends State<BuildScheduleView> {
  @override
  Widget build(BuildContext context) {
    final bloc = BlocProvider.of<WaterHeaterBloc>(context);

    return BlocProvider.value(
      value: bloc,
      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<WaterHeaterBloc, WaterHeaterState>(
                builder: (context, state) {
                  if (state is WaterHeaterDeviceStatusLoaded) {
                    return Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const ScheduleHeader(),
                        const SizedBox(height: 20),
                        ScheduleModeSelector(state: state),
                        const SizedBox(height: 20),
                        if (state.scheduleMode == ScheduleModes.schedule)
                          ScheduleManagementUI(
                            state: state,
                            onAddSchedule: () {
                              ScheduleDialogHelper.showAddScheduleDialog(
                                  context,
                                  schedule: null,
                                  index: null,
                                  isEdit: false);
                            },
                          ),
                        if (state.scheduleMode == ScheduleModes.countdown ||
                            state.scheduleMode == ScheduleModes.inching)
                          CountdownInchingView(state: state),
                        const SizedBox(height: 20),
                        if (state.scheduleMode == ScheduleModes.countdown)
                          CountdownModeButtons(
                            isActive: state.isCountdownActive ?? false,
                            deviceId: widget.status.uuid,
                            hours: state.countdownHours ?? 0,
                            minutes: state.countdownMinutes ?? 0,
                          ),
                        if (state.scheduleMode == ScheduleModes.inching)
                          InchingModeButtons(
                            isActive: state.isInchingActive ?? false,
                            deviceId: widget.status.uuid,
                            hours: state.inchingHours ?? 0,
                            minutes: state.inchingMinutes ?? 0,
                          ),
                        if (state.scheduleMode != ScheduleModes.countdown &&
                            state.scheduleMode != ScheduleModes.inching)
                          ScheduleModeButtons(
                            onSave: () {
                              Navigator.pop(context);
                            },
                          ),
                      ],
                    );
                  }
                  if (state is WaterHeaterLoadingState) {
                    return const SizedBox(
                        height: 200,
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            ScheduleHeader(),
                            SizedBox(
                              height: 20,
                            ),
                            Center(child: CircularProgressIndicator()),
                          ],
                        ));
                  }
                  return const SizedBox(
                    height: 200,
                    child: ScheduleHeader(),
                  );
                },
              ),
            ),
          ),
        ),
      ),
    );
  }
}
