import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/acs_bloc/acs_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/acs_bloc/acs_event.dart';
import 'package:syncrow_app/features/devices/bloc/acs_bloc/acs_state.dart';
import 'package:syncrow_app/features/devices/model/ac_model.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/ac_mode_control_unit.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/ac_temp_widget.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/universal_ac_temp.dart';
import 'package:syncrow_app/features/devices/view/widgets/universal_switch.dart';
import 'package:syncrow_app/features/shared_widgets/devices_default_switch.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart';
import 'package:syncrow_app/utils/helpers/snack_bar.dart';

class ACsList extends StatelessWidget {
  const ACsList({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<ACsBloc, AcsState>(
      listener: (context, state) {
        if (state is AcsFailedState) {
          CustomSnackBar.displaySnackBar(state.errorMessage);
        }
      },
      builder: (context, state) {
        List<AcStatusModel> devicesStatuesList = [];
        List<DeviceModel> devicesList = [];
        bool allOn = false;
        bool allTempSame = false;
        int temperature = 250;
        if (state is GetAllAcsStatusState) {
          devicesStatuesList = state.allAcsStatues;
          devicesList = state.allAcs;
          allOn = state.allOn;
          allTempSame = state.allTempSame;
          temperature = state.temp;
        }
        return SingleChildScrollView(
          child:
              //  state is AcChangeLoading || state is AcsLoadingState
              //     ? const Center(
              //         child:
              //             DefaultContainer(width: 50, height: 50, child: CircularProgressIndicator()),
              //       )
              //     :
              Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              // universal AC controller
              const SizedBox(height: 10),
              const BodySmall(text: "All ACs"),
              const SizedBox(height: 5),
              UniversalSwitch(
                allOn: allOn,
              ),
              const SizedBox(height: 10),
              UniversalACTemp(
                allTempSame: allTempSame,
                temp: temperature,
              ),
              const SizedBox(height: 10),

              // other ACs controls
              ListView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                padding: const EdgeInsets.all(0),
                itemCount: devicesList.length,
                itemBuilder: (context, index) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const SizedBox(
                        height: 10,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          BodySmall(text: devicesList[index].name ?? ''),
                        ],
                      ),
                      const SizedBox(height: 5),
                      DevicesDefaultSwitch(
                        off: 'OFF',
                        on: 'ON',
                        switchValue: devicesStatuesList[index].acSwitch,
                        action: () {
                          BlocProvider.of<ACsBloc>(context).add(AcSwitch(
                              acSwitch: devicesStatuesList[index].acSwitch,
                              deviceId: devicesList[index].uuid ?? '',
                              productId: devicesList[index].productUuid ?? ''));
                        },
                      ),
                      const SizedBox(height: 10),
                      ACTempWidget(
                        deviceModel: devicesList[index],
                        temp: devicesStatuesList[index].tempSet,
                      ),
                      const SizedBox(height: 10),
                      ACModeControlUnit(
                        acStatus: devicesStatuesList[index],
                        deviceId: devicesList[index].uuid ?? '',
                        productId: devicesList[index].productUuid ?? '',
                      ),
                      const SizedBox(height: 10),
                    ],
                  );
                },
              ),
            ],
          ),
        );
      },
    );
  }
}
