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 StatelessWidget {
  const DoorLockButton({
    super.key,
    required this.doorLock,
    required this.smartDoorModel,
  });

  final AllDevicesModel doorLock;
  final DoorLockStatusModel 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<DoorLockBloc>(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.doorUnlock
                        : Assets.lockIcon,
                    width: 60,
                    height: 60,
                  ),
                ),
              ),
              if (progress > 0)
                SizedBox.expand(
                  child: CircularProgressIndicator(
                    value: progress,
                    strokeWidth: 8,
                    backgroundColor: Colors.transparent,
                    valueColor: const AlwaysStoppedAnimation<Color>(
                        ColorsManager.primaryColor),
                  ),
                ),
            ],
          ),
        ),
      ),
    );
  }
}
