import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:im_stepper/stepper.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:unit2/model/offline/offline_profile.dart';
import 'package:unit2/screens/offline/passo/building/add/building_and_structure.dart';
import 'package:unit2/screens/offline/passo/building/add/drawing_pad.dart';
import 'package:unit2/screens/offline/passo/building/add/flutter_painter.dart';
import 'package:unit2/screens/offline/passo/building/add/imagePicker.dart';
import 'package:unit2/screens/offline/passo/building/add/property_appraisal.dart';
import 'package:unit2/screens/offline/passo/building/add/property_assessment.dart';
import 'package:unit2/screens/offline/passo/building/add/property_owner_info.dart';
import 'package:unit2/screens/offline/passo/building/add/additional_items.dart';
import 'package:unit2/screens/offline/passo/building/add/general_description.dart';
import 'package:unit2/screens/offline/passo/building/add/landref_location.dart';
import 'package:unit2/screens/offline/passo/building/add/structural_material.dart';
import 'package:unit2/theme-data.dart/colors.dart';

import '../../../../../bloc/offline/offline_passo/building/owner_info_bloc/crud_bloc.dart';
import '../../../../../model/passo/property_info.dart';
import '../../../../../utils/alerts.dart';

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

class AddBuilding extends StatefulWidget {
  Function triggerBlocEvent;
  final OfflineProfile offlineProfile;
  AddBuilding(this.triggerBlocEvent, this.offlineProfile);
  @override
  _AddBuilding createState() => _AddBuilding();
}

class _AddBuilding extends State<AddBuilding> {
  int activeStep = 0; // Initial step set to 5.
  int upperBound = 7;

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

  bool foundationOthers = false;
  bool columOthers = false;
  bool beamsOthers = false;
  bool tfOthers = false;
  bool roofOthers = false;
  bool flooringOthers = false;
  bool wpOthers = false;

  String bldgType = "";
  String bldgKind = "Structure type";
  String unitValue = "";

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

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

  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;
    });
  }

  void updateFoundationOthers(bool updatedOthers) {
    setState(() {
      foundationOthers = updatedOthers;
    });
  }

  void updateColumOthers(bool updatedOthers) {
    setState(() {
      columOthers = updatedOthers;
    });
  }

  void updateBeamsOthers(bool updatedOthers) {
    setState(() {
      beamsOthers = updatedOthers;
    });
  }

  void updateTfOthers(bool updatedOthers) {
    setState(() {
      tfOthers = updatedOthers;
    });
  }

  void updateRoofOthers(bool updatedOthers) {
    setState(() {
      roofOthers = updatedOthers;
    });
  }

  void updateFlooringOthers(bool updatedOthers) {
    setState(() {
      flooringOthers = updatedOthers;
    });
  }

  void updateWpOthers(bool updatedOthers) {
    setState(() {
      wpOthers = updatedOthers;
    });
  }

  void updatedBldgType(dynamic updatedBldgType) {
    setState(() {
      bldgType = updatedBldgType;
    });
  }

  void updatedBldgKind(dynamic updatedBldgKind) {
    setState(() {
      bldgKind = updatedBldgKind;
    });
  }

  void updatedUnitValue(dynamic updatedUnitValue) {
    setState(() {
      unitValue = updatedUnitValue;
    });
  }

  @override
  Widget build(BuildContext context) {
    print(widget.offlineProfile.toJson());
    return WillPopScope(
      onWillPop: () async {
        confirmAlertWithCancel(
            context,
            onCloseTransaction,
            () => null,
            'Cancel transaction?',
            "Are you sure you want to cancel this transaction?"); // Action to perform on back pressed
        return false;
      },
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: primary,
          centerTitle: true,
          title: const Text("Offline Building FAAS"),
        ),
        body: Column(children: [
          NumberStepper(
            numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9],
            stepPadding: 5,
            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;
              });
            },
            enableStepTapping: false,
          ),
          Expanded(
            child: StatefulBuilder(
                builder: (BuildContext context, StateSetter setState) {
              return FormBuilder(
                key: offlineBldgKey,
                onChanged: () {
                  offlineBldgKey.currentState?.save();
                },
                autovalidateMode: AutovalidateMode.disabled,
                child: Container(
                  child: content(PrevBtn, NextBtn),
                ),
              );
            }),
          ),
        ]),
      ),
    );
  }

  Widget content(PrevBtn, NextBtn) {
    switch (activeStep) {
      case 0:
        return PropertyInfoOfflinePage(NextBtn, widget.offlineProfile);

      case 1:
        return LandRefLocationOfflinePage(
            PrevBtn, NextBtn, widget.offlineProfile);

      case 2:
        return GeneralDescriptionOfflinePage(
            NextBtn,
            PrevBtn,
            widget.offlineProfile,
            bldgKind,
            bldgType,
            unitValue,
            updatedUnitValue,
            updatedBldgKind,
            updatedBldgType);
      case 3:
        return FlutterDraw();

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

      case 5:
        return StatefulBuilder(
            builder: (context, StateSetter setInnerState) =>
                StructuralMaterialsOfflinePage(
                  PrevBtn,
                  NextBtn,
                  foundationOthers: foundationOthers,
                  columOthers: columOthers,
                  beamsOthers: beamsOthers,
                  tfOthers: tfOthers,
                  roofOthers: roofOthers,
                  flooringOthers: flooringOthers,
                  wpOthers: wpOthers,
                  foundation: foundation,
                  column: column,
                  beam: beam,
                  trussFraming: trussFraming,
                  roof: roof,
                  flooring: flooring,
                  walls: walls,
                  updateFoundation: updateFoundation,
                  updateColumn: updateColumn,
                  updateBeam: updateBeam,
                  updateFlooring: updateFlooring,
                  updateRoof: updateRoof,
                  updateTrussFraming: updateTrussFraming,
                  updateWalls: updateWalls,
                  updateFoundationOthers: updateFoundationOthers,
                  updateColumOthers: updateColumOthers,
                  updateBeamsOthers: updateBeamsOthers,
                  updateTfOthers: updateTfOthers,
                  updateRoofOthers: updateRoofOthers,
                  updateFlooringOthers: updateFlooringOthers,
                  updateWpOthers: updateWpOthers,
                ));

      case 6:
        return AdditionalItemOfflinePage(
            PrevBtn, NextBtn, widget.offlineProfile);

      case 7:
        return PropertyAppraisalOfflinePage(
            NextBtn, PrevBtn, widget.offlineProfile);

      case 8:
        return PropertyAssessmentOfflinePage(
            onCloseTransaction, widget.offlineProfile);

      default:
        return Text("Property Info");
    }
  }

  void onCloseTransaction() async {
    final tempID = await SharedPreferences.getInstance();
    Navigator.of(context).pop();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      widget.triggerBlocEvent();
    });
    await tempID.setBool('floorSketchSaved', false);
  }
}
