// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/scene/bloc/create_scene/create_scene_bloc.dart';
import 'package:syncrow_app/features/scene/model/scene_static_function.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/title_medium.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';

class AlertDialogTemperatureBody extends StatefulWidget {
  AlertDialogTemperatureBody({
    super.key,
    this.functionValue,
    required this.taskItem,
  });

  final SceneStaticFunction taskItem;
  dynamic functionValue;

  @override
  State<AlertDialogTemperatureBody> createState() =>
      _AlertDialogTemperatureBodyState();
}

class _AlertDialogTemperatureBodyState
    extends State<AlertDialogTemperatureBody> {
  int temperature = 24;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final tempTaskList = context.read<CreateSceneBloc>().tempTasksList;

    for (var element in tempTaskList) {
      if (element.code == widget.taskItem.code) {
        temperature = _normalizeTemperature(element.functionValue);
      } else {
        context
            .read<CreateSceneBloc>()
            .add(RemoveFromSelectedValueById(code: widget.taskItem.code));
      }
    }
    if (widget.functionValue != null) {
      setState(() {
        temperature = _normalizeTemperature(widget.functionValue);
      });
    }

    context.read<CreateSceneBloc>().add(SelectedValueEvent(
          value: temperature * 10,
          code: widget.taskItem.code,
        ));
  }

  int _normalizeTemperature(dynamic value) {
    if (value >= 100) {
      return value ~/ 10;
    }
    return value ?? 24;
  }

  @override
  Widget build(BuildContext context) {
    return ListTile(
      contentPadding: const EdgeInsets.symmetric(horizontal: 32),
      leading: IconButton(
          onPressed: () {
            setState(() {
              if (temperature > 20) {
                temperature--;
              }
            });
            context.read<CreateSceneBloc>().add(SelectedValueEvent(
                  value: temperature * 10,
                  code: widget.taskItem.code,
                ));
          },
          icon: const Icon(
            Icons.remove,
            size: 32,
            color: ColorsManager.greyColor,
          )),
      title: Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          TitleMedium(
              text: temperature.toString(),
              style: context.titleMedium.copyWith(
                color: ColorsManager.primaryColorWithOpacity,
                fontSize: 30,
              )),
          const SizedBox(width: 4),
          Padding(
            padding: const EdgeInsets.only(bottom: 8.0),
            child: SvgPicture.asset(
              Assets.assetsCelsiusDegrees,
              alignment: Alignment.topCenter,
              width: 32,
            ),
          ),
        ],
      ),
      subtitle: BodyLarge(
        text: widget.taskItem.operationalValues[0].description.toString(),
        textAlign: TextAlign.center,
      ),
      trailing: IconButton(
          onPressed: () {
            setState(() {
              if (temperature < 30) {
                temperature++;
              }
            });
            context.read<CreateSceneBloc>().add(SelectedValueEvent(
                value: temperature * 10, code: widget.taskItem.code));
          },
          icon: const Icon(
            Icons.add,
            size: 32,
            color: ColorsManager.greyColor,
          )),
    );
  }
}
