import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/two_touch_bloc/two_touch_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/two_touch_bloc/two_touch_event.dart';
import 'package:syncrow_app/features/devices/bloc/two_touch_bloc/two_touch_state.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/two_touch_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/restart_status_dialog.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_large.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
import 'package:syncrow_app/utils/resource_manager/constants.dart';

class TwoTouchSetting extends StatelessWidget {
  const TwoTouchSetting({super.key, this.device});

  final DeviceModel? device;

  @override
  Widget build(BuildContext context) {
    return DefaultScaffold(
        title: 'Setting',
        child: BlocProvider(
          create: (context) =>
              TwoTouchBloc(switchCode: '', twoTouchId: device?.uuid ?? '')
                ..add(const InitialEvent()),
          child: BlocBuilder<TwoTouchBloc, TwoTouchState>(
            builder: (context, state) {
              final twoTouchBloc = BlocProvider.of<TwoTouchBloc>(context);
              TwoTouchModel? twoTouchModel = TwoTouchModel(
                  relay_status_2: status.off,
                  secondCountDown: 0,
                  secondSwitch: false,
                  firstSwitch: false,
                  firstCountDown: 0,
                  light_mode: lightStatus.off,
                  relay: status.off,
                  relay_status_1: status.off);

              // if (state is UpdateState) {
              //   TwoTouchModel = state.TwoTouchModel;
              // }
              if (state is UpdateState) {
                twoTouchModel = state.twoTouchModel;
              }
              return state is LoadingInitialState
                  ? const Center(
                      child: DefaultContainer(
                          width: 50,
                          height: 50,
                          child: CircularProgressIndicator()),
                    )
                  : Column(
                      children: [
                        Container(
                          child: DefaultContainer(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              mainAxisSize: MainAxisSize.min,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                InkWell(
                                  onTap: () async {
                                    twoTouchBloc.optionSelected =
                                        'relay_status';
                                    final result = await showDialog(
                                      context: context,
                                      builder: (context) {
                                        return RestartStatusDialog(
                                          initialSelectedLabel:
                                              twoTouchModel!.relay.value,
                                          cancelTab: () {
                                            Navigator.of(context).pop();
                                          },
                                          confirmTab: () {
                                            twoTouchBloc.add(ChangeStatusEvent(
                                                deviceId: device!.uuid!,
                                                context: context));
                                            Navigator.of(context).pop(true);
                                          },
                                          title: 'Restart Status',
                                          label1: 'Power Off',
                                          label2: 'Power On',
                                          label3: 'Restart Memory',
                                          onTapLabel1: (selected) {
                                            twoTouchBloc.statusSelected =
                                                selected;
                                          },
                                          onTapLabel2: (selected) {
                                            twoTouchBloc.statusSelected =
                                                selected;
                                          },
                                          onTapLabel3: (selected) {
                                            twoTouchBloc.statusSelected =
                                                selected;
                                          },
                                        );
                                      },
                                    );
                                    if (result == true) {
                                      Future.delayed(const Duration(seconds: 2),
                                          () async {
                                        twoTouchBloc.add(const InitialEvent());
                                      });
                                    }
                                  },
                                  child: Container(
                                      padding: const EdgeInsets.only(
                                          bottom: 10, top: 10),
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: [
                                          const BodyLarge(
                                            fontSize: 15,
                                            text: 'Restart Status',
                                            fontWeight: FontWeight.w400,
                                          ),
                                          Row(
                                            children: [
                                              BodyMedium(
                                                fontSize: 13,
                                                text: twoTouchModel.relay.value,
                                                fontColor:
                                                    ColorsManager.textGray,
                                              ),
                                              const Icon(
                                                Icons.keyboard_arrow_right,
                                                color: ColorsManager.textGray,
                                              ),
                                            ],
                                          )
                                        ],
                                      )),
                                ),
                                const Divider(
                                  color: ColorsManager.graysColor,
                                ),
                                Padding(
                                  padding: const EdgeInsets.only(
                                      bottom: 10, top: 10),
                                  child: InkWell(
                                    onTap: () async {
                                      twoTouchBloc.optionSelected =
                                          'light_mode';
                                      final result = await showDialog(
                                        context: context,
                                        builder: (context) {
                                          return RestartStatusDialog(
                                            initialSelectedLabel:
                                                twoTouchModel!.light_mode.value,
                                            cancelTab: () {
                                              Navigator.of(context).pop();
                                            },
                                            confirmTab: () {
                                              twoTouchBloc.add(
                                                  ChangeStatusEvent(
                                                      deviceId: device!.uuid!,
                                                      context: context));
                                              Navigator.of(context).pop(true);
                                            },
                                            title: 'Indicator Status',
                                            label1: 'Off',
                                            label2: 'On/Off Status',
                                            label3: 'Switch Position',
                                            onTapLabel1: (selected) {
                                              twoTouchBloc.statusSelected =
                                                  selected;
                                            },
                                            onTapLabel2: (selected) {
                                              twoTouchBloc.statusSelected =
                                                  selected;
                                            },
                                            onTapLabel3: (selected) {
                                              twoTouchBloc.statusSelected =
                                                  selected;
                                            },
                                          );
                                        },
                                      );
                                      if (result == true) {
                                        Future.delayed(
                                            const Duration(seconds: 2),
                                            () async {
                                          twoTouchBloc
                                              .add(const InitialEvent());
                                        });
                                      }
                                    },
                                    child: SizedBox(
                                        child: Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        const BodyLarge(
                                          fontSize: 15,
                                          text: 'Indicator Status',
                                          fontWeight: FontWeight.w400,
                                        ),
                                        Row(
                                          children: [
                                            BodyMedium(
                                              fontSize: 13,
                                              text: twoTouchBloc.deviceStatus
                                                  .light_mode.value,
                                              fontColor: ColorsManager.textGray,
                                            ),
                                            const Icon(
                                              Icons.keyboard_arrow_right,
                                              color: ColorsManager.textGray,
                                            ),
                                          ],
                                        )
                                      ],
                                    )),
                                  ),
                                ),
                                const Divider(
                                  color: ColorsManager.graysColor,
                                ),
                                Container(
                                    padding: const EdgeInsets.only(
                                        bottom: 10, top: 10),
                                    child: InkWell(
                                      onTap: () async {
                                        twoTouchBloc.optionSelected =
                                            'relay_status_1';
                                        final result = await showDialog(
                                          context: context,
                                          builder: (context) {
                                            return RestartStatusDialog(
                                              initialSelectedLabel:
                                                  twoTouchModel!
                                                      .relay_status_1.value,
                                              cancelTab: () {
                                                Navigator.of(context).pop();
                                              },
                                              confirmTab: () {
                                                twoTouchBloc.add(
                                                    ChangeStatusEvent(
                                                        deviceId: device!.uuid!,
                                                        context: context));
                                                Navigator.of(context).pop(true);
                                              },
                                              title: 'Restart Status 1',
                                              label1: 'Power Off',
                                              label2: 'Power On',
                                              label3: 'Restart Memory',
                                              onTapLabel1: (selected) {
                                                twoTouchBloc.statusSelected =
                                                    selected;
                                              },
                                              onTapLabel2: (selected) {
                                                twoTouchBloc.statusSelected =
                                                    selected;
                                              },
                                              onTapLabel3: (selected) {
                                                twoTouchBloc.statusSelected =
                                                    selected;
                                              },
                                            );
                                          },
                                        );
                                        if (result == true) {
                                          Future.delayed(
                                              const Duration(seconds: 2),
                                              () async {
                                            twoTouchBloc
                                                .add(const InitialEvent());
                                          });
                                        }
                                      },
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: [
                                          const BodyLarge(
                                            fontSize: 15,
                                            text: 'Restart Status 1',
                                            fontWeight: FontWeight.w400,
                                          ),
                                          Row(
                                            children: [
                                              BodyMedium(
                                                fontSize: 13,
                                                text: twoTouchBloc.deviceStatus
                                                    .relay_status_1.value,
                                                fontColor:
                                                    ColorsManager.textGray,
                                              ),
                                              const Icon(
                                                Icons.keyboard_arrow_right,
                                                color: ColorsManager.textGray,
                                              ),
                                            ],
                                          )
                                        ],
                                      ),
                                    )),
                                const Divider(
                                  color: ColorsManager.graysColor,
                                ),
                                Container(
                                    padding: const EdgeInsets.only(
                                        bottom: 10, top: 10),
                                    child: InkWell(
                                      onTap: () async {
                                        twoTouchBloc.optionSelected =
                                            'relay_status_2';
                                        final result = await showDialog(
                                          context: context,
                                          builder: (context) {
                                            return RestartStatusDialog(
                                              initialSelectedLabel:
                                                  twoTouchModel!
                                                      .relay_status_2.value,
                                              cancelTab: () {
                                                Navigator.of(context).pop();
                                              },
                                              confirmTab: () {
                                                twoTouchBloc.add(
                                                    ChangeStatusEvent(
                                                        deviceId: device!.uuid!,
                                                        context: context));
                                                Navigator.of(context).pop(true);
                                              },
                                              title: 'Restart Status 2',
                                              label1: 'Power Off',
                                              label2: 'Power On',
                                              label3: 'Restart Memory',
                                              onTapLabel1: (selected) {
                                                twoTouchBloc.statusSelected =
                                                    selected;
                                              },
                                              onTapLabel2: (selected) {
                                                twoTouchBloc.statusSelected =
                                                    selected;
                                              },
                                              onTapLabel3: (selected) {
                                                twoTouchBloc.statusSelected =
                                                    selected;
                                              },
                                            );
                                          },
                                        );
                                        if (result == true) {
                                          Future.delayed(
                                              const Duration(seconds: 2),
                                              () async {
                                            twoTouchBloc
                                                .add(const InitialEvent());
                                          });
                                        }
                                      },
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: [
                                          const BodyLarge(
                                            fontSize: 15,
                                            text: 'Restart Status 2',
                                            fontWeight: FontWeight.w400,
                                          ),
                                          Row(
                                            children: [
                                              BodyMedium(
                                                fontSize: 13,
                                                text: twoTouchBloc.deviceStatus
                                                    .relay_status_2.value,
                                                fontColor:
                                                    ColorsManager.textGray,
                                              ),
                                              const Icon(
                                                Icons.keyboard_arrow_right,
                                                color: ColorsManager.textGray,
                                              ),
                                            ],
                                          )
                                        ],
                                      ),
                                    )),
                              ],
                            ),
                          ),
                        ),
                      ],
                    );
            },
          ),
        ));
  }
}
