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/6_scene_switch_bloc/6_scene_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/6_scene_switch_bloc/6_scene_event.dart';
import 'package:syncrow_app/features/devices/bloc/6_scene_switch_bloc/6_scene_state.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/six_scene_model.dart';
import 'package:syncrow_app/features/devices/view/device_settings/settings_page.dart';
import 'package:syncrow_app/features/devices/view/widgets/6_scene_switch/select_scene_page.dart';
import 'package:syncrow_app/features/devices/view/widgets/6_scene_switch/select_switch_dialog.dart';
import 'package:syncrow_app/features/devices/view/widgets/6_scene_switch/six_switches_card.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 SixSceneScreen extends StatelessWidget {
  final DeviceModel? device;

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

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => SixSceneBloc(sixSceneId: device?.uuid ?? '')
        ..add(const SixSceneInitial())
        ..add(const SixSceneInitialInfo())
        ..add(const SexSceneSwitchInitial()),
      child: BlocBuilder<SixSceneBloc, SixSceneState>(
        builder: (context, state) {
          final bloc = BlocProvider.of<SixSceneBloc>(context);
          SixSceneModel model = SixSceneModel(
            scene_1: '',
            scene_2: '',
            scene_3: '',
            scene_4: '',
            scene_5: '',
            scene_6: '',
            scene_id_group_id: '',
            switch_backlight: '',
          );
          if (state is LoadingNewSate) {
            model = state.device;
          } else if (state is UpdateState) {
            model = state.device;
          }
          return DefaultScaffold(
            title: bloc.deviceInfo.name ,
            actions: [
              InkWell(
                onTap: () async {
                  var val = await Navigator.of(context).push(
                    MaterialPageRoute(
                      builder: (context) => SettingsPage(device: device!),
                    ),
                  );
                  if (val == null || val == true) {
                    bloc.add(const SixSceneInitialInfo());
                    bloc.add(const SixSceneInitial());
                    bloc.add(const SexSceneSwitchInitial());
                  }
                },
                child: SvgPicture.asset(Assets.assetsIconsSettings),
              ),
              const SizedBox(width: 10),
            ],
            child: state is SixSceneLoadingState
                ? const Center(
                    child: DefaultContainer(
                      width: 50,
                      height: 50,
                      child: CircularProgressIndicator(),
                    ),
                  )
                : RefreshIndicator(
                    onRefresh: () async {
                      bloc.add(const SixSceneInitial());
                    },
                    child: ListView(
                      children: [
                        SizedBox(
                          height: MediaQuery.sizeOf(context).height * 0.8,
                          child: Column(
                            children: [
                              SixSwitchsCardDialog(
                                switch1Title: model.scene_1,
                                switch2Title: model.scene_2,
                                switch3Title: model.scene_3,
                                switch4Title: model.scene_4,
                                switch5Title: model.scene_5,
                                switch6Title: model.scene_6,
                                switch1Down: bloc.deviceStatus.switch_backlight
                                    ? Assets.switchOn
                                    : Assets.switchOff,
                                switch1Up: bloc.deviceStatus.switch_backlight
                                    ? Assets.switchOn
                                    : Assets.switchOff,
                                switch2Down: bloc.deviceStatus.switch_backlight
                                    ? Assets.switchOn
                                    : Assets.switchOff,
                                switch2Up: bloc.deviceStatus.switch_backlight
                                    ? Assets.switchOn
                                    : Assets.switchOff,
                                switch3Up: bloc.deviceStatus.switch_backlight
                                    ? Assets.switchOn
                                    : Assets.switchOff,
                                switch3Down: bloc.deviceStatus.switch_backlight
                                    ? Assets.switchOn
                                    : Assets.switchOff,
                                onSwitch3DownTap: () {
                                  debugPrint("onSwitch3DownTap");
                                },
                                onSwitch3UpTap: () {
                                  debugPrint("onSwitch3UpTap");
                                },
                                onSwitch1UpTap: () {
                                  debugPrint("Switch 1 Up tapped");
                                },
                                onSwitch1DownTap: () {
                                  debugPrint("Switch 1 Down tapped");
                                },
                                onSwitch2UpTap: () {
                                  debugPrint("Switch 2 Up tapped");
                                },
                                onSwitch2DownTap: () {
                                  debugPrint("Switch 2 Down tapped");
                                },
                              ),
                              Flexible(
                                child: Row(
                                  children: [
                                    Expanded(
                                      child: DefaultContainer(
                                        onTap: () {
                                          bloc.add(ChangeSwitchStatusEvent());
                                        },
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.center,
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: [
                                            ConstrainedBox(
                                              constraints: const BoxConstraints(
                                                maxHeight: 46,
                                                maxWidth: 50,
                                              ),
                                              child: SvgPicture.asset(
                                                  Assets.backlightIcon),
                                            ),
                                            const SizedBox(height: 15),
                                            const Flexible(
                                              child: FittedBox(
                                                child: BodySmall(
                                                  text: 'Backlight',
                                                  textAlign: TextAlign.center,
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                    const SizedBox(width: 10),
                                    Expanded(
                                      child: DefaultContainer(
                                        onTap: () async {
                                          var value = await showDialog(
                                            context: context,
                                            builder: (context) {
                                              return SelectSwitchDialog(
                                                switch1Title: model.scene_1,
                                                switch2Title: model.scene_2,
                                                switch3Title: model.scene_3,
                                                switch4Title: model.scene_4,
                                                switch5Title: model.scene_5,
                                                switch6Title: model.scene_6,
                                                cancelTab: () {
                                                  Navigator.of(context).pop();
                                                },
                                                confirmTab: (v) {
                                                  Navigator.of(context).push(
                                                    MaterialPageRoute(
                                                      builder: (context) =>
                                                          SixSelectSceneFourPage(
                                                        deviceId: device!.uuid,
                                                        switchSelected: v,
                                                      ),
                                                    ),
                                                  );
                                                },
                                              );
                                            },
                                          );
                                          if (value == true) {
                                            Future.delayed(
                                                const Duration(
                                                    milliseconds: 200), () {
                                              bloc.add(
                                                  const SexSceneSwitchInitial());
                                            });
                                          }
                                        },
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.center,
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: [
                                            ConstrainedBox(
                                              constraints: const BoxConstraints(
                                                maxHeight: 46,
                                                maxWidth: 50,
                                              ),
                                              child: SvgPicture.asset(
                                                  Assets.addSceneIcon),
                                            ),
                                            const SizedBox(height: 15),
                                            const Flexible(
                                              child: FittedBox(
                                                child: BodySmall(
                                                  text: 'Add Scene',
                                                  textAlign: TextAlign.center,
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
          );
        },
      ),
    );
  }
}
