import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/bloc/garage_door_bloc.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/bloc/garage_door_state.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/schedule_view/seconds_picker.dart';

class OpeningAndClosingTimeDialogBody extends StatefulWidget {
  final ValueChanged<int> onDurationChanged;
  final GarageDoorBloc bloc;

  const OpeningAndClosingTimeDialogBody({
    required this.onDurationChanged,
    required this.bloc,
  });

  @override
  _OpeningAndClosingTimeDialogBodyState createState() =>
      _OpeningAndClosingTimeDialogBodyState();
}

class _OpeningAndClosingTimeDialogBodyState
    extends State<OpeningAndClosingTimeDialogBody> {
  late int durationInSeconds;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();

    final currentState = widget.bloc.state;
    if (currentState is GarageDoorLoadedState) {
      setState(() {
        durationInSeconds = currentState.status.trTimeCon;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 120,
      color: Colors.white,
      child: SecondsPicker(
        initialSeconds: durationInSeconds,
        onSecondsChanged: (newSeconds) {
          setState(() {
            durationInSeconds = newSeconds;
          });
          widget.onDurationChanged(newSeconds);
        },
      ),
    );
  }
}
