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/door_sensor_bloc/door_sensor_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/door_sensor_bloc/door_sensor_event.dart';
import 'package:syncrow_app/features/devices/bloc/door_sensor_bloc/door_sensor_state.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/door_sensor_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/door_sensor/door_notification_settings.dart';
import 'package:syncrow_app/features/devices/view/widgets/door_sensor/door_records_screen.dart';
import 'package:syncrow_app/features/shared_widgets/battery_bar.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart';
import 'package:syncrow_app/generated/assets.dart';

class DoorSensorScreen extends StatelessWidget {
  final DeviceModel? device;

  const DoorSensorScreen({super.key, this.device});

  @override
  Widget build(BuildContext context) {
    return DefaultScaffold(
      title: device?.name,
      child: BlocProvider(
        create: (context) =>
            DoorSensorBloc(DSId: device?.uuid ?? '')..add(const DoorSensorInitial()),
        child: BlocBuilder<DoorSensorBloc, DoorSensorState>(
          builder: (context, state) {
            final doorSensorBloc = BlocProvider.of<DoorSensorBloc>(context);
            DoorSensorModel model =
                DoorSensorModel(batteryPercentage: 0, doorContactState: false);
            if (state is LoadingNewSate) {
              model = state.doorSensor;
            } else if (state is UpdateState) {
              model = state.doorSensor;
            }
            return state is DoorSensorLoadingState
                ? const Center(
                    child: DefaultContainer(
                        width: 50, height: 50, child: CircularProgressIndicator()),
                  )
                : RefreshIndicator(
                    onRefresh: () async {
                      doorSensorBloc.add(const DoorSensorInitial());
                    },
                    child: ListView(
                      children: [
                        SizedBox(
                          height: MediaQuery.sizeOf(context).height * 0.8,
                          child: Column(
                            children: [
                              BatteryBar(
                                batteryPercentage: model.batteryPercentage,
                              ),
                              Expanded(
                                flex: 4,
                                child: InkWell(
                                  overlayColor:
                                      WidgetStateProperty.all(Colors.transparent),
                                  onTap: () {},
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Container(
                                        decoration: BoxDecoration(
                                          // color: Colors.white.withOpacity(0.1),
                                          borderRadius: BorderRadius.circular(890),
                                          boxShadow: [
                                            BoxShadow(
                                              color: Colors.white.withOpacity(0.1),
                                              blurRadius: 24,
                                              offset: const Offset(-5, -5),
                                              blurStyle: BlurStyle.outer,
                                            ),
                                            BoxShadow(
                                              color: Colors.black.withOpacity(0.11),
                                              blurRadius: 25,
                                              offset: const Offset(5, 5),
                                              blurStyle: BlurStyle.outer,
                                            ),
                                            BoxShadow(
                                              color: Colors.black.withOpacity(0.13),
                                              blurRadius: 30,
                                              offset: const Offset(5, 5),
                                              blurStyle: BlurStyle.inner,
                                            ),
                                          ],
                                        ),
                                        child: SvgPicture.asset(
                                          model.doorContactState
                                              ? Assets.doorOpen
                                              : Assets.doorClose,
                                          fit: BoxFit.fill,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                              Flexible(
                                child: Row(
                                  children: [
                                    Expanded(
                                      child: DefaultContainer(
                                        onTap: () {
                                          Navigator.of(context).push(
                                            MaterialPageRoute(
                                                builder: (context) =>
                                                    DoorRecordsScreen(
                                                        DSId: device!.uuid!)),
                                          );
                                        },
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.center,
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: [
                                            ConstrainedBox(
                                              constraints: const BoxConstraints(
                                                  maxHeight: 46, maxWidth: 50),
                                              child: SvgPicture.asset(
                                                  Assets.doorRecordsIcon),
                                            ),
                                            const SizedBox(
                                              height: 15,
                                            ),
                                            const Flexible(
                                              child: FittedBox(
                                                child: BodySmall(
                                                  text: 'Records',
                                                  // doorLockButtons.keys.elementAt(index),
                                                  textAlign: TextAlign.center,
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                    const SizedBox(
                                      width: 10,
                                    ),
                                    Expanded(
                                      child: DefaultContainer(
                                        onTap: () {
                                          Navigator.of(context).push(
                                            MaterialPageRoute(
                                                builder: (context) =>
                                                    NotificationSettingsPage()),
                                          );
                                        },
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.center,
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: [
                                            ConstrainedBox(
                                              constraints: const BoxConstraints(
                                                  maxHeight: 46, maxWidth: 50),
                                              child: SvgPicture.asset(
                                                  Assets.doorNotificationSetting),
                                            ),
                                            const SizedBox(
                                              height: 15,
                                            ),
                                            const Flexible(
                                              child: FittedBox(
                                                child: BodySmall(
                                                  text: 'Notification Settings',
                                                  // doorLockButtons.keys.elementAt(index),
                                                  textAlign: TextAlign.center,
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              )
                            ],
                          ),
                        ),
                      ],
                    ),
                  );
          },
        ),
      ),
    );
  }
}
