import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.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/bloc/smart_door_bloc/smart_door_state.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/features/devices/view/widgets/device_appbar.dart';
import 'package:syncrow_app/features/devices/view/widgets/smart_door/door_button.dart';
import 'package:syncrow_app/features/devices/view/widgets/smart_door/door_grid.dart';
import 'package:syncrow_app/features/devices/view/widgets/smart_door/door_status_bar.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
import 'package:syncrow_app/utils/resource_manager/constants.dart';

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

  final DeviceModel doorLock;
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => SmartDoorBloc(deviceId: doorLock.uuid ?? '')..add(InitialEvent()),
      child: BlocConsumer<SmartDoorBloc, SmartDoorState>(listener: (context, state) {
        if (state is FailedState) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text(state.errorMessage),
              backgroundColor: Colors.red,
            ),
          );
        }
      }, builder: (context, state) {
        SmartDoorModel smartDoorModel = SmartDoorModel(
            unlockFingerprint: 0,
            unlockPassword: 0,
            unlockTemporary: 0,
            unlockCard: 0,
            unlockAlarm: '',
            unlockRequest: 0,
            residualElectricity: 0,
            reverseLock: false,
            unlockApp: 0,
            hijack: false,
            doorbell: false,
            unlockOfflinePd: '',
            unlockOfflineClear: '',
            unlockDoubleKit: '',
            remoteNoPdSetkey: '',
            remoteNoDpKey: '',
            normalOpenSwitch: false);

        if (state is UpdateState) {
          smartDoorModel = state.smartDoorModel;
        } else if (state is LoadingNewSate) {
          smartDoorModel = state.smartDoorModel;
        }

        return AnnotatedRegion(
            value: SystemUiOverlayStyle(
              statusBarColor: ColorsManager.primaryColor.withOpacity(0.5),
              statusBarIconBrightness: Brightness.light,
            ),
            child: Scaffold(
              backgroundColor: ColorsManager.backgroundColor,
              extendBodyBehindAppBar: true,
              extendBody: true,
              appBar: DeviceAppbar(
                deviceName: doorLock.name!,
                deviceUuid: doorLock.uuid!,
              ),
              body: Container(
                width: MediaQuery.sizeOf(context).width,
                height: MediaQuery.sizeOf(context).height,
                decoration: const BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage(
                      Assets.assetsImagesBackground,
                    ),
                    fit: BoxFit.cover,
                    opacity: 0.4,
                  ),
                ),
                child: SafeArea(
                  child: Padding(
                    padding: EdgeInsets.only(
                      top: Constants.appBarHeight,
                      left: Constants.defaultPadding,
                      right: Constants.defaultPadding,
                    ),
                    child: state is LoadingInitialState
                        ? const Center(
                            child: RefreshProgressIndicator(),
                          )
                        : RefreshIndicator(
                            onRefresh: () async {
                              BlocProvider.of<SmartDoorBloc>(context).add(InitialEvent());
                            },
                            child: ListView(
                              children: [
                                DoorLockStatusBar(
                                  smartDoorModel: smartDoorModel,
                                ),
                                Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  crossAxisAlignment: CrossAxisAlignment.stretch,
                                  children: [
                                    DoorLockButton(
                                      doorLock: doorLock,
                                      smartDoorModel: smartDoorModel,
                                    ),
                                    DoorLockGrid(
                                      uuid: doorLock.uuid!,
                                    ),
                                  ],
                                )
                              ],
                            )),
                  ),
                ),
              ),
            ));
      }),
    );
  }
}
