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/resource_manager/color_manager.dart';

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

  final DeviceModel doorLock;
  final SmartDoorModel smartDoorModel;

  double _calculateProgress() {
    final value = smartDoorModel.unlockRequest;
    if (value <= 0 || value > 30) return 0;
    return value / 30.0;
  }

  @override
  Widget build(BuildContext context) {
    final progress = _calculateProgress();
    final isEnabled = smartDoorModel.unlockRequest > 0;

    return SizedBox(
      width: 255,
      height: 255,
      child: InkWell(
        onTap: isEnabled
            ? () {
                BlocProvider.of<SmartDoorBloc>(context).add(
                  UpdateLockEvent(value: !smartDoorModel.normalOpenSwitch),
                );
              }
            : null,
        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.doorUnlockIcon
                        : Assets.assetsIconsDoorlockAssetsLockIcon,
                    width: 60,
                    height: 60,
                  ),
                ),
              ),
              if (progress > 0)
                Container(
                  decoration: BoxDecoration(shape: BoxShape.circle),
                  height: 250,
                  width: 250,
                  child: CircularProgressIndicator(
                    value: progress,
                    strokeWidth: 8,
                    backgroundColor: Colors.transparent,
                    valueColor: const AlwaysStoppedAnimation<Color>(
                        ColorsManager.primaryColor),
                  ),
                ),
            ],
          ),
        ),
      ),
    );
  }
}