import 'package:flutter/cupertino.dart';
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_event.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/bloc/garage_door_state.dart';

class TimeOutAlarmDialogBody extends StatefulWidget {
  TimeOutAlarmDialogBody(this.bloc);
  final GarageDoorBloc bloc;

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

class _TimeOutAlarmDialogBodyState extends State<TimeOutAlarmDialogBody> {
  int durationInSeconds = 0;

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

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

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 120,
      color: Colors.white,
      child: CupertinoTimerPicker(
        itemExtent: 120,
        mode: CupertinoTimerPickerMode.hm,
        initialTimerDuration: Duration(seconds: durationInSeconds),
        onTimerDurationChanged: (newDuration) {
          widget.bloc.add(
            UpdateCountdownAlarmEvent(newDuration.inSeconds),
          );
        },
      ),
    );
  }
}
