import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.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/bloc/devices_cubit.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_interface_controls.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/ac_interface_temp_unit.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/generated/assets.dart';
import 'package:syncrow_app/utils/helpers/snack_bar.dart';

class AcInterface extends StatelessWidget {
  const AcInterface({super.key, required this.ac});

  final DeviceModel ac;

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<ACsBloc, AcsState>(
      listener: (context, state) {
        if (state is AcsFailedState) {
          CustomSnackBar.displaySnackBar(state.errorMessage);
        }
      },
      builder: (context, state) {
        AcStatusModel statusModel = AcStatusModel(
            countdown1: 0,
            uuid: ac.uuid ?? '',
            acSwitch: true,
            modeString: 'hot',
            tempSet: 300,
            currentTemp: 315,
            fanSpeedsString: 'low',
            childLock: false);

        if (state is GetAcStatusState) {
          statusModel = state.acStatusModel;
        }
        if (state is AcChangeLoading) {
          statusModel = state.acStatusModel;
        }
        if (state is AcModifyingState) {
          statusModel = state.acStatusModel;
        }
        return
            // Scaffold(
            //   backgroundColor: ColorsManager.backgroundColor,
            //   extendBodyBehindAppBar: true,
            //   extendBody: true,
            //   body:
            ListView(
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                const SizedBox(
                  height: 20,
                ),
                DefaultContainer(
                  height: 65,
                  padding: const EdgeInsets.all(15),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      BodyLarge(text: statusModel.acSwitch ? 'On' : 'Off'),
                      GestureDetector(
                          onTap: () {
                            BlocProvider.of<ACsBloc>(context)
                                .add(AcSwitch(acSwitch: statusModel.acSwitch));

                          },
                          child: SvgPicture.asset(Assets.acSwitchIcon))
                    ],
                  ),
                ),
                const SizedBox(
                  height: 10,
                ),
                ConstrainedBox(
                  constraints: const BoxConstraints(
                    maxHeight: 380,
                  ),
                  child: AcInterfaceTempUnit(
                    acDevice: ac,
                  ),
                ),
                const SizedBox(
                  height: 10,
                ),
                ConstrainedBox(
                  constraints: const BoxConstraints(
                    maxHeight: 130,
                  ),
                  child: AcInterfaceControls(
                    deviceModel: ac,
                    deviceStatus: statusModel,
                  ),
                ),
              ],
            ),
          ],
        );
        // );
      },
    );
  }
}
