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/sos_bloc/sos_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/sos_bloc/sos_event.dart';
import 'package:syncrow_app/features/devices/bloc/sos_bloc/sos_state.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/view/device_settings/settings_page.dart';
import 'package:syncrow_app/features/devices/view/widgets/sos/sos_alarm_management_page.dart';
import 'package:syncrow_app/features/devices/view/widgets/sos/sos_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 SosScreen extends StatelessWidget {
  final DeviceModel? device;

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

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => SosBloc(sosId: device?.uuid ?? '')
        ..add(const SosInitial())
        ..add(SosInitialDeviseInfo()),
      child: BlocBuilder<SosBloc, SosState>(
        builder: (context, state) {
          final sensor = BlocProvider.of<SosBloc>(context);
          return DefaultScaffold(
            title: sensor.deviceInfo.name,
            actions: [
              InkWell(
                onTap: () async {
                  var val = await Navigator.of(context).push(
                    MaterialPageRoute(
                      builder: (context) => SettingsPage(device: device!),
                    ),
                  );
                  if (val == null || val == true) {
                    sensor.add(SosInitialDeviseInfo());
                    sensor.add(const SosInitial());
                  }
                },
                child: SvgPicture.asset(Assets.assetsIconsSettings),
              ),
              const SizedBox(width: 10),
            ],
            child: state is SosLoadingState
                ? const Center(
                    child: DefaultContainer(
                      width: 50,
                      height: 50,
                      child: CircularProgressIndicator(),
                    ),
                  )
                : RefreshIndicator(
                    onRefresh: () async {
                      sensor.add(const SosInitial());
                    },
                    child: ListView(
                      children: [
                        SizedBox(
                          height: MediaQuery.sizeOf(context).height * 0.8,
                          child: Column(
                            children: [
                              BatteryBar(
                                batteryPercentage:
                                    sensor.deviceStatus.batteryPercentage,
                              ),
                              Expanded(
                                flex: 4,
                                child: InkWell(
                                  overlayColor: WidgetStateProperty.all(
                                      Colors.transparent),
                                  onTap: () {
                                    // Add functionality for the main SOS button here
                                  },
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      Container(
                                        decoration: BoxDecoration(
                                          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(
                                          sensor.deviceStatus.sosContactState !=
                                                  'sos'
                                              ? Assets.redSos
                                              : Assets.greenSos,
                                          fit: BoxFit.fill,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                              Flexible(
                                child: Row(
                                  children: [
                                    Expanded(
                                      child: DefaultContainer(
                                        onTap: () {
                                          Navigator.of(context).push(
                                            MaterialPageRoute(
                                              builder: (context) =>
                                                  SosRecordsScreen(
                                                      sosId: 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',
                                                  textAlign: TextAlign.center,
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                    const SizedBox(width: 10),
                                    Expanded(
                                      child: DefaultContainer(
                                        onTap: () {
                                          Navigator.of(context).push(
                                            MaterialPageRoute(
                                              builder: (context) =>
                                                  AlarmManagementPage(
                                                sosId: device!.uuid!,
                                              ),
                                            ),
                                          );
                                        },
                                        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: 'Alarm Settings',
                                                  textAlign: TextAlign.center,
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
          );
        },
      ),
    );
  }
}
