import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.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/offline/offline_passo/admin/class_components_admin.dart/class_components_admin_bloc.dart';
import 'package:unit2/bloc/offline/offline_passo/admin/unit_construction/unit_construction_admin_bloc.dart';
import 'package:unit2/model/offline/offline_profile.dart';
import 'package:unit2/screens/offline/passo/building/edit/additional_items_edit.dart';
import 'package:unit2/screens/offline/passo/building/edit/building_and_structure.dart';
import 'package:unit2/screens/offline/passo/building/edit/flutter_painter_edit.dart';
import 'package:unit2/screens/offline/passo/building/edit/general_description_edit.dart';
import 'package:unit2/screens/offline/passo/building/edit/landref_location_edit.dart';
import 'package:unit2/screens/offline/passo/building/edit/property_appraisal_edit.dart';
import 'package:unit2/screens/offline/passo/building/edit/property_assessment_edit.dart';
import 'package:unit2/screens/offline/passo/building/edit/property_owner_info_edit.dart';
import 'package:unit2/screens/offline/passo/building/edit/structural_materials_edit.dart';
import 'package:unit2/utils/alerts.dart';

import '../../../../../bloc/offline/offline_passo/building/structural_materials_offline.dart/structural_material_offline_bloc.dart';
import '../../../../../model/passo/property_info.dart';
import '../../../../../theme-data.dart/colors.dart';

GlobalKey<FormBuilderState> offlineBldgEditKey = GlobalKey<FormBuilderState>();

class EditBuildingOffline extends StatefulWidget {
  final int index;
  final PropertyInfo faas;
  final String title;
  final OfflineProfile offlineProfile;
  final Function loadBldg;

  const EditBuildingOffline(
      {super.key,
      required this.title,
      required this.index,
      required this.faas,
      required this.offlineProfile,
      required this.loadBldg});
  @override
  _EditBuildingOffline createState() => _EditBuildingOffline();
}

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

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

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

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

  void onSAveAll() {
    Navigator.of(context).pop();
    widget.loadBldg();
  }

  List<String> foundation = [];
  List<String> column = [];
  List<String> beam = [];
  List<String> trussFraming = [];
  List<String> roof = [];
  List<String> flooring = [];
  List<String> walls = [];

  void updateFoundation(List<String> updatedList) {
    setState(() {
      foundation = updatedList;
    });
  }

  void updateColumn(List<String> updatedList) {
    setState(() {
      column = updatedList;
    });
  }

  void updateBeam(List<String> updatedList) {
    setState(() {
      beam = updatedList;
    });
  }

  void updateTrussFraming(List<String> updatedList) {
    setState(() {
      trussFraming = updatedList;
    });
  }

  void updateRoof(List<String> updatedList) {
    setState(() {
      roof = updatedList;
    });
  }

  void updateFlooring(List<String> updatedList) {
    setState(() {
      flooring = updatedList;
    });
  }

  void updateWalls(List<String> updatedList) {
    setState(() {
      walls = updatedList;
    });
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        confirmAlertWithCancel(
            context,
            onSAveAll,
            () => null,
            'Cancel Editing?',
            "Unsaved edited data will not be saved, are you sure you want to exit?"); // Action to perform on back pressed
        return false;
      },
      child: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            backgroundColor: primary,
            title: const Text('Building FAAS Edit'),
          ),
          body: ProgressHUD(
              padding: const EdgeInsets.all(24),
              backgroundColor: Colors.black87,
              indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
              child: BlocConsumer<StructuralMaterialOfflineBloc,
                  StructuralMaterialOfflineState>(
                listener: (context, state) {
                  // TODO: implement listener
                },
                builder: (context, state) {
                  if (state is SpecificStructuralMaterialLoaded) {
                    return BlocConsumer<UnitConstructionAdminBloc,
                        UnitConstructionAdminState>(
                      listener: (context, state) {
                        // TODO: implement listener
                      },
                      builder: (context, state) {
                        if (state is UnitConstructLoaded) {
                          final unit = state.unit;
                          return BlocConsumer<ClassComponentsAdminBloc,
                              ClassComponentsAdminState>(
                            listener: (context, state) {
                              // TODO: implement listener
                            },
                            builder: (context, state) {
                              if (state is ClassComponentsAdminLoaded) {
                                return Column(
                                  children: [
                                    NumberStepper(
                                      numbers: const [
                                        1,
                                        2,
                                        3,
                                        4,
                                        5,
                                        6,
                                        7,
                                        8,
                                        9
                                      ],
                                      activeStepColor: primary,
                                      numberStyle:
                                          const 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;
                                        });
                                      },
                                    ),
                                    Expanded(
                                      child: StatefulBuilder(builder:
                                          (BuildContext context,
                                              StateSetter setState) {
                                        return Container(
                                          child: content(PrevBtn, NextBtn, unit,
                                              state.classes),
                                        );
                                      }),
                                    ),
                                  ],
                                );
                              }
                              return Container();
                            },
                          );
                        }
                        return Container();
                      },
                    );
                  }
                  return Container();
                },
              ))),
    );
  }

  // Returns the header text based on the activeStep.
  Widget content(PrevBtn, NextBtn, unit, classes) {
    switch (activeStep) {
      case 0:
        return PropertyOwnerInfoEditOffline(
          widget.index,
          widget.faas,
          'Edit',
          PrevBtn,
          NextBtn,
        );

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

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

      case 4:
        return BuildingAndStructureOfflinePage(
            PrevBtn, NextBtn, widget.offlineProfile);

      case 3:
        return FlutterDrawEdit(widget.faas.id!);

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

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

      case 7:
        return PropertyAppraisalEditPageOffline(
            widget.faas.id!, NextBtn, PrevBtn);

      case 8:
        return PropertyAssessmentEditOfflinePage(
            widget.faas.id!, onSAveAll, widget.offlineProfile);

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