import 'package:flutter/material.dart';
import 'package:flutter/services.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/device_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/ac_interface.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/acs_list.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/utils/resource_manager/color_manager.dart';
import 'package:syncrow_app/utils/resource_manager/constants.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/resource_manager/font_manager.dart';

import '../device_appbar.dart';

class ACsView extends StatelessWidget {
  final DeviceModel? deviceModel;
  const ACsView({this.deviceModel, super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      
      create: (context) => ACsBloc(acId: deviceModel?.uuid ?? '')
        ..add(AcsInitial(allAcs: deviceModel != null ? false : true)),
      child: BlocBuilder<ACsBloc, AcsState>(
        builder: (context, state) {
          return AnnotatedRegion(
              value: SystemUiOverlayStyle(
                statusBarColor: ColorsManager.primaryColor.withOpacity(0.5),
                statusBarIconBrightness: Brightness.light,
              ),
              child: Scaffold(
                backgroundColor: ColorsManager.backgroundColor,
                extendBodyBehindAppBar: true,
                extendBody: true,
                appBar: deviceModel != null
                    ? DeviceAppbar(
                        value: true,
                        deviceName: deviceModel!.name!,
                        deviceUuid: deviceModel!.uuid!,
                      )
                    : AppBar(
                        backgroundColor: Colors.transparent,
                        centerTitle: true,
                        title: BodyLarge(
                          text: deviceModel?.name ?? "ACs",
                          fontColor: ColorsManager.primaryColor,
                          fontWeight: FontsManager.bold,
                        ),
                      ),
                body: Container(
                  width: MediaQuery.sizeOf(context).width,
                  height: MediaQuery.sizeOf(context).height,
                  padding: const EdgeInsets.all(Constants.defaultPadding),
                  decoration: const BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage(
                        Assets.assetsImagesBackground,
                      ),
                      fit: BoxFit.cover,
                      opacity: 0.4,
                    ),
                  ),
                  child: state is AcsLoadingState
                      ? const Center(
                          child: DefaultContainer(
                              width: 50,
                              height: 50,
                              child: CircularProgressIndicator()),
                        )
                      : RefreshIndicator(
                          onRefresh: () async {
                            BlocProvider.of<ACsBloc>(context).add(AcsInitial(
                                allAcs: deviceModel != null ? false : true));
                          },
                          child: Container(
                            padding: const EdgeInsets.only(top: 40),
                            alignment: AlignmentDirectional.center,
                            child: deviceModel != null
                                ? AcInterface(ac: deviceModel!)
                                : const ACsList(),
                          ),
                        ),
                ),
              ));
        },
      ),
    );
  }
}
