import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

import 'package:im_stepper/stepper.dart';
import 'package:unit2/bloc/passo/bulding/class_components/class_components_bloc.dart';
import 'package:unit2/bloc/passo/bulding/unit_construct/unit_construct_bloc.dart';
import 'package:unit2/model/passo/class_components.dart';
import 'package:unit2/model/passo/property_info.dart';
import 'package:unit2/model/passo/unit_construct.dart';
import 'package:unit2/screens/passo/Building/edit_building/additional_items.dart';
import 'package:unit2/screens/passo/Building/edit_building/bldgloc_landref.dart';
import 'package:unit2/screens/passo/Building/edit_building/general_description.dart';
import 'package:unit2/screens/passo/Building/edit_building/property_appraisal.dart';
import 'package:unit2/screens/passo/Building/edit_building/property_assessement_edit.dart';
import 'package:unit2/screens/passo/Building/edit_building/property_owner_info.dart';
import 'package:unit2/screens/passo/Building/edit_building/structural_materials.dart';
import 'package:unit2/theme-data.dart/colors.dart';
import 'package:unit2/utils/text_container.dart';
import 'package:unit2/widgets/error_state.dart';

class EditBuilding extends StatefulWidget {
  final int index;
  final PropertyInfo faas;
  final String title;

  const EditBuilding(
      {super.key,
      required this.title,
      required this.index,
      required this.faas});
  @override
  _EditBuilding createState() => _EditBuilding();
}

class _EditBuilding extends State<EditBuilding> {
  // THE FOLLOWING TWO VARIABLES ARE REQUIRED TO CONTROL THE STEPPER.
  int activeStep = 0; // Initial step set to 5.

  int upperBound = 6; // upperBound MUST BE total number of icons minus 1.

  void PrevBtn() {
    setState(() {
      activeStep--;
    });
  }

  void NextBtn() {
    setState(() {
      activeStep++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          backgroundColor: primary,
          title: Text('Building FAAS Edit'),
        ),
        body: ProgressHUD(
          padding: const EdgeInsets.all(24),
          backgroundColor: Colors.black87,
          indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
          child: BlocConsumer<UnitConstructBloc, UnitConstructState>(
            listener: (context, state) {
              if (state is UnitConstructLoading) {
                final progress = ProgressHUD.of(context);
                progress!.showWithText("Please wait...");
              }
              if (state is UnitConstructLoaded) {
                final progress = ProgressHUD.of(context);
                progress?.dismiss();
              }
              if (state is UnitConstructErrorState) {
                final progress = ProgressHUD.of(context);
                progress?.dismiss();
                // Fluttertoast.showToast(
                //     msg: onError,
                //     fontSize: 24,
                //     toastLength: Toast.LENGTH_LONG,
                //     gravity: ToastGravity.CENTER,
                //     backgroundColor: Colors.black,
                //     textColor: Colors.white);
              }
            },
            builder: (context, state) {
              if (state is UnitConstructLoaded) {
                final unit = state.unit;
                return BlocConsumer<ClassComponentsBloc, ClassComponentsState>(
                  listener: (context, state) {
                    if (state is ClassComponentLoading) {
                      final progress = ProgressHUD.of(context);
                      progress!.showWithText("Please wait...");
                    }
                    if (state is ClassComponentLoaded) {
                      final progress = ProgressHUD.of(context);
                      progress?.dismiss();
                    }
                    if (state is ClassComponentErrorState) {
                      final progress = ProgressHUD.of(context);
                      progress?.dismiss();
                      // Fluttertoast.showToast(
                      //     msg: onError,
                      //     fontSize: 24,
                      //     toastLength: Toast.LENGTH_LONG,
                      //     gravity: ToastGravity.CENTER,
                      //     backgroundColor: Colors.black,
                      //     textColor: Colors.white);
                    }
                  },
                  builder: (context, state) {
                    if (state is ClassComponentLoaded) {
                      return Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Column(
                          children: [
                            NumberStepper(
                              numbers: [1, 2, 3, 4, 5, 6, 7],
                              activeStepColor: primary,
                              numberStyle: TextStyle(color: Colors.white),
                              lineColor: primary,
                              // activeStep property set to activeStep variable defined above.
                              activeStep: activeStep,
                              activeStepBorderColor: Colors.white,
                              activeStepBorderWidth: 1,
                              // This ensures step-tapping updates the activeStep.
                              onStepReached: (index) {
                                setState(() {
                                  activeStep = index;
                                });
                              },
                            ),
                            content(unit, state.classes),
                          ],
                        ),
                      );
                    }
                    if (state is ClassComponentErrorState) {
                      return SomethingWentWrong(
                        message: onError,
                        onpressed: () {
                          context
                              .read<ClassComponentsBloc>()
                              .add(LoadClassComponents());
                        },
                      );
                    }
                    return Container();
                  },
                );
              }
              if (state is UnitConstructErrorState) {
                return SomethingWentWrong(
                  message: onError,
                  onpressed: () {
                    context.read<UnitConstructBloc>().add(LoadUnitConstruct());
                  },
                );
              }
              return Container();
            },
          ),
        ),
      ),
    );
  }

  // Returns the header text based on the activeStep.
  Widget content(List<UnitConstruct> unit, classes) {
    switch (activeStep) {
      case 0:
        return PropertyOwnerInfoEdit(
            widget.index, widget.faas, widget.title, NextBtn, PrevBtn);

      case 1:
        return BldgLocLandRefEdit(widget.faas.id!, NextBtn, PrevBtn);

      case 2:
        return GeneralDescriptionEdit(unit, widget.faas.id!, NextBtn, PrevBtn);

      case 3:
        return StructuralMaterialsPageEdit(widget.faas.id!, NextBtn, PrevBtn);

      case 4:
        return AdditionalItemEditPage(
            unit, classes, widget.faas.id!, NextBtn, PrevBtn);

      case 5:
        return PropertyAppraisalEditPage(widget.faas.id!, NextBtn, PrevBtn);

      case 6:
        return PropertyAssessmentEditPage(widget.faas.id!);

      default:
        return PropertyOwnerInfoEdit(
            widget.index, widget.faas, widget.title, NextBtn, PrevBtn);
    }
  }
}
