import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/devices/bloc/smart_door_bloc/smart_door_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/smart_door_bloc/smart_door_event.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/smart_door_model.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';

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

  final DeviceModel doorLock;
  final SmartDoorModel smartDoorModel;
  @override
  State<DoorLockButton> createState() => _DoorLockButtonState(smartDoorModel: smartDoorModel);
}

class _DoorLockButtonState extends State<DoorLockButton> with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _animation;
  SmartDoorModel smartDoorModel;
  _DoorLockButtonState({required this.smartDoorModel});

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

    _animationController = AnimationController(
      vsync: this,
      value: context.read<SmartDoorBloc>().unlockRequest > 0 ? 1 : 0,
      duration: Duration(seconds: context.read<SmartDoorBloc>().unlockRequest),
    );
    if (context.read<SmartDoorBloc>().unlockRequest > 0) {
      _animationController.reverse();
    }

    _animation = Tween<double>(begin: 0, end: 1).animate(_animationController)
      ..addListener(() {
        setState(() {});
      });
  }

  @override
  void didUpdateWidget(DoorLockButton oldWidget) {
    super.didUpdateWidget(oldWidget);

    if (_animationController.status == AnimationStatus.dismissed) {
      if (context.read<SmartDoorBloc>().unlockRequest > 0) {
        _animationController.value = 1;
        _animationController.duration =
            Duration(seconds: context.read<SmartDoorBloc>().unlockRequest);
        _animationController.reverse();
      }
    }
  }

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

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(
        right: context.width * 0.25 / 2,
        left: context.width * 0.25 / 2,
        bottom: context.width * 0.2 / 2,
      ),
      child: InkWell(
        overlayColor:
            WidgetStateProperty.all(ColorsManager.primaryColorWithOpacity.withOpacity(0.1)),
        borderRadius: BorderRadius.circular(999),
        onTapDown: (details) {
          // if (_animationController.status == AnimationStatus.dismissed) {
          //   _animationController.forward();
          // } else if (_animationController.status == AnimationStatus.completed) {
          //   _animationController.reverse();
          // } else if (_animationController.status == AnimationStatus.forward) {
          //   _animationController.reverse();
          // } else if (_animationController.status == AnimationStatus.reverse) {
          //   _animationController.forward();
          // }
          if (context.read<SmartDoorBloc>().unlockRequest > 0) {
            BlocProvider.of<SmartDoorBloc>(context)
                .add(UpdateLockEvent(value: smartDoorModel.normalOpenSwitch));
          }
        },
        onTapUp: (details) {
          // if (_animationController.status == AnimationStatus.forward) {
          //   _animationController.reverse();
          // } else if (_animationController.status == AnimationStatus.reverse) {
          //   _animationController.forward();
          // }
        },
        child: Container(
          width: context.width * 06,
          height: context.width * 0.6,
          margin: const EdgeInsets.all(10),
          decoration: const BoxDecoration(
            boxShadow: [
              BoxShadow(
                color: Colors.grey,
                blurRadius: 18,
                // offset: Offset(6, 7),
                blurStyle: BlurStyle.outer,
              ),
            ],
            color: Color(0xFFEBECED),
            borderRadius: BorderRadius.all(Radius.circular(999)),
          ),
          child: Padding(
            padding: const EdgeInsets.all(25),
            child: Stack(
              alignment: Alignment.center,
              children: [
                Container(
                  margin: const EdgeInsets.all(8),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(999),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.white.withOpacity(0.5),
                        blurRadius: 30,
                        offset: const Offset(-5, -5),
                        blurStyle: BlurStyle.outer,
                      ),
                      BoxShadow(
                        color: Colors.black.withOpacity(0.14),
                        blurRadius: 25,
                        offset: const Offset(5, 5),
                        blurStyle: BlurStyle.outer,
                      ),
                      BoxShadow(
                        color: Colors.black.withOpacity(0.14),
                        blurRadius: 30,
                        offset: const Offset(5, 5),
                        blurStyle: BlurStyle.inner,
                      ),
                    ],
                  ),
                  child: Center(
                    child: SvgPicture.asset(
                      smartDoorModel.normalOpenSwitch
                          ? Assets.doorUnlockIcon
                          : Assets.assetsIconsDoorlockAssetsLockIcon,
                    ),
                  ),
                ),
                SizedBox.expand(
                  child: CircularProgressIndicator(
                    value: _animation.value,
                    strokeWidth: 15,
                    backgroundColor: Colors.transparent,
                    valueColor: const AlwaysStoppedAnimation<Color>(ColorsManager.primaryColor),
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
