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/flush_sensor_bloc/flush_sensor_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/flush_sensor_bloc/flush_sensor_event.dart';
import 'package:syncrow_app/features/devices/bloc/flush_sensor_bloc/flush_sensor_state.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/flush_sensor_model.dart';
import 'package:syncrow_app/features/devices/view/device_settings/settings_page.dart';
import 'package:syncrow_app/features/devices/view/widgets/device_appbar.dart';
import 'package:syncrow_app/features/devices/view/widgets/flush_sensor/flush_persence_records.dart';
import 'package:syncrow_app/features/devices/view/widgets/flush_sensor/flush_sensor_option.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.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 "presence_indicator.dart";
part "flush_sensor_options_list.dart";
part 'flush_sensor_parameter_control_dialog.dart';

class FlushMountedInterface extends StatelessWidget {
  const FlushMountedInterface({super.key, required this.deviceModel});
  final DeviceModel deviceModel;
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => FlushSensorBloc(deviceId: deviceModel.uuid ?? '')
        ..add(FlushSensorInitialEvent()),
      child: BlocBuilder<FlushSensorBloc, FlushSensorState>(
          builder: (context, state) {
        final bloc = BlocProvider.of<FlushSensorBloc>(context);
        FlushSensorModel flushSensorModel = FlushSensorModel(
            presenceState: 'none',
            farDetection: 0,
            illuminance: 0,
            checkingResult: '',
            nearDetection: 0,
            noneDelay: 0,
            occurDistReduce: 0,
            presenceDelay: 0,
            sensiReduce: 0,
            sensitivity: 0);

        if (state is FlushSensorUpdateState) {
          flushSensorModel = state.flushSensorModel;
        } else if (state is FlushSensorLoadingNewSate) {
          flushSensorModel = state.flushSensorModel;
        }

        return AnnotatedRegion(
            value: SystemUiOverlayStyle(
              statusBarColor: ColorsManager.primaryColor.withOpacity(0.5),
              statusBarIconBrightness: Brightness.light,
            ),
            child: DefaultScaffold(
                title: FlushSensorBloc.deviceName,
                actions: [
                  InkWell(
                    onTap: () async {
                      var val = await Navigator.of(context).push(
                        MaterialPageRoute(
                          builder: (context) =>
                              SettingsPage(device: deviceModel),
                        ),
                      );
                      if (val == null) {
                        bloc.add(FlushSensorInitialDeviseInfo());
                        bloc.add(FlushSensorInitialEvent());
                      }
                    },
                    child: SvgPicture.asset(Assets.assetsIconsSettings),
                  ),
                  const SizedBox(width: 10),
                ],
                child: 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: state is FlushSensorLoadingInitialState
                        ? const Center(
                            child: RefreshProgressIndicator(),
                          )
                        : SafeArea(
                            child: RefreshIndicator(
                              onRefresh: () async {
                                bloc.add(FlushSensorInitialEvent());
                              },
                              child: ListView(children: [
                                SizedBox(
                                  height: MediaQuery.of(context).size.height,
                                  child: Column(children: [
                                    PresenceIndicator(
                                      state: flushSensorModel.presenceState,
                                    ),
                                    Expanded(
                                      flex: 2,
                                      child: FlushSensorOptionsList(
                                        bloc: bloc,
                                        flushSensorModel: flushSensorModel,
                                        deviceModel: deviceModel,
                                      ),
                                    )
                                  ]),
                                ),
                              ]),
                            ),
                          ))));
      }),
    );
  }
}
