import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
import 'package:syncrow_web/pages/device_managment/door_lock/bloc/door_lock_bloc.dart';
import 'package:syncrow_web/pages/device_managment/door_lock/bloc/door_lock_event.dart';
import 'package:syncrow_web/pages/device_managment/door_lock/models/door_lock_status_model.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';

class DoorLockButton extends StatefulWidget {
  const DoorLockButton({
    super.key,
    required this.doorLock,
    required this.smartDoorModel,
  });

  final AllDevicesModel doorLock;
  final DoorLockStatusModel smartDoorModel;

  @override
  State<DoorLockButton> createState() =>
      _DoorLockButtonState(smartDoorModel: smartDoorModel);
}

class _DoorLockButtonState extends State<DoorLockButton>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _animation;
  DoorLockStatusModel smartDoorModel;

  _DoorLockButtonState({required this.smartDoorModel});

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 500),
    );
    _animation = Tween<double>(begin: 0, end: 1).animate(_animationController)
      ..addListener(() {
        setState(() {});
      });

    if (smartDoorModel.unlockRequest > 0) {
      _animationController.reverse(from: 1);
    }
  }

  @override
  void didUpdateWidget(covariant DoorLockButton oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.smartDoorModel.normalOpenSwitch !=
        widget.smartDoorModel.normalOpenSwitch) {
      setState(() {
        smartDoorModel = widget.smartDoorModel;
      });

      if (smartDoorModel.unlockRequest > 0) {
        _animationController.forward(from: 0);
      } else {
        _animationController.reverse(from: 1);
      }
    }
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 255,
      height: 255,
      child: InkWell(
        onTap: () {
          _animationController.forward(from: 0);
          BlocProvider.of<DoorLockBloc>(context)
              .add(UpdateLockEvent(value: !smartDoorModel.normalOpenSwitch));
        },
        child: Container(
          width: 255,
          height: 255,
          decoration: BoxDecoration(
            color: const Color(0xFFEBECED),
            shape: BoxShape.circle,
            boxShadow: [
              BoxShadow(
                color: Colors.grey.withOpacity(0.5),
                blurRadius: 18,
                blurStyle: BlurStyle.outer,
              ),
            ],
          ),
          child: Stack(
            alignment: Alignment.center,
            children: [
              Container(
                margin: const EdgeInsets.all(30),
                decoration: const BoxDecoration(
                  color: Color(0xFFF9F9F9),
                  shape: BoxShape.circle,
                ),
                child: Center(
                  child: SvgPicture.asset(
                    smartDoorModel.normalOpenSwitch
                        ? Assets.doorUnlock
                        : Assets.lockIcon,
                    width: 60,
                    height: 60,
                  ),
                ),
              ),
              SizedBox.expand(
                child: CircularProgressIndicator(
                  value: _animation.value,
                  strokeWidth: 8,
                  backgroundColor: Colors.transparent,
                  valueColor: const AlwaysStoppedAnimation<Color>(
                      ColorsManager.primaryColor),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
