import 'dart:ffi';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/devices/bloc/wall_sensor_bloc/wall_sensor_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/wall_sensor_bloc/wall_sensor_state.dart';
import 'package:syncrow_app/features/devices/bloc/wall_sensor_bloc/wall_sensor_event.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/wall_sensor_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/device_appbar.dart';
import 'package:syncrow_app/features/devices/view/widgets/garage_door/garage_records_screen.dart';
import 'package:syncrow_app/features/devices/view/widgets/wall_sensor/persence_records.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/title_medium.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/helpers/misc_string_helpers.dart';
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
import 'package:syncrow_app/utils/resource_manager/constants.dart';
import 'package:syncrow_app/utils/resource_manager/font_manager.dart';
part "parameters_list.dart";
part "presence_indicator.dart";
part "../ceiling_sensor/parameter_control_dialog.dart";

class WallMountedInterface extends StatelessWidget {
  const WallMountedInterface({super.key, required this.deviceModel});
  final DeviceModel deviceModel;
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => WallSensorBloc(deviceId: deviceModel.uuid ?? '')..add(InitialEvent()),
      child: BlocBuilder<WallSensorBloc, WallSensorState>(builder: (context, state) {
        WallSensorModel wallSensorModel = WallSensorModel(
            presenceState: 'none',
            farDetection: 0,
            presenceTime: 0,
            motionSensitivity: 0,
            motionlessSensitivity: 0,
            currentDistance: 0,
            illuminance: 0,
            indicator: false,
            noOneTime: 0);

        if (state is UpdateState) {
          wallSensorModel = state.wallSensorModel;
        } else if (state is LoadingNewSate) {
          wallSensorModel = state.wallSensorModel;
        }
        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: deviceModel.name!,
                deviceUuid: deviceModel.uuid!,
              ),
              body: Container(
                width: MediaQuery.sizeOf(context).width,
                height: MediaQuery.sizeOf(context).height,
                padding: const EdgeInsets.all(Constants.defaultPadding),
                decoration: const BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage(
                      Assets.assetsImagesBackground,
                    ),
                    fit: BoxFit.cover,
                    opacity: 0.4,
                  ),
                ),
                child: state is LoadingInitialState
                    ? const Center(
                        child: RefreshProgressIndicator(),
                      )
                    : SafeArea(
                        child: RefreshIndicator(
                          onRefresh: () async {
                            BlocProvider.of<WallSensorBloc>(context).add(InitialEvent());
                          },
                          child: ListView(
                            children: [
                              Container(
                                height: MediaQuery.of(context).size.height,
                                child: Column(
                                  children: [
                                    PresenceIndicator(
                                      state: wallSensorModel.presenceState,
                                    ),
                                    ParametersList(
                                      wallSensor: deviceModel,
                                      presenceSensorsModel: wallSensorModel,
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
              ),
            ));
      }),
    );
  }
}
