import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';

import 'package:im_stepper/stepper.dart';
import 'package:unit2/screens/offline/passo/land/add/land_appraisal.dart';
import 'package:unit2/screens/offline/passo/land/add/land_property_assessment.dart';
import 'package:unit2/screens/offline/passo/land/add/land_property_owner_offline.dart';
import 'package:unit2/screens/offline/passo/land/add/land_property_signatories.dart';
import 'package:unit2/screens/offline/passo/land/add/location_and_boundaries_offline.dart';
import 'package:unit2/screens/offline/passo/land/add/other_improvement.dart';
import 'package:unit2/screens/offline/passo/land/add/value_adjustment_offline.dart';

import '../../../../../theme-data.dart/colors.dart';
import '../../../../../utils/alerts.dart';

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

class AddLandOffline extends StatefulWidget {
  Function loadLandFass;
  AddLandOffline(this.loadLandFass);
  @override
  _AddLandOffline createState() => _AddLandOffline();
}

class _AddLandOffline extends State<AddLandOffline> {
  // 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++;
    });
  }

  void onCloseTransaction() {
    Navigator.of(context).pop();
    widget.loadLandFass();
  }

  @override
  Widget build(BuildContext context) {
    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(
          centerTitle: true,
          backgroundColor: primary,
          title: const Text('Land FAAS'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: [
              NumberStepper(
                numbers: const [1, 2, 3, 4, 5, 6, 7],
                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: FormBuilder(
                  key: landKeyOffline,

                  // enabled: false,
                  onChanged: () {
                    landKeyOffline.currentState?.save();

                    print(landKeyOffline.currentState?.value.toString());
                  },
                  autovalidateMode: AutovalidateMode.disabled,
                  skipDisabled: true,
                  child: Container(
                    child: content(PrevBtn, NextBtn, onCloseTransaction),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// Returns the next button.

  // Returns the content widget based on the activeStep.
  Widget content(PrevBtn, NextBtn, onSAveAll) {
    switch (activeStep) {
      case 0:
        return LandPropertyOwnerInfoOffline(NextBtn);
      case 1:
        return LandLocationAndBoundariesOffline(PrevBtn, NextBtn);
      case 2:
        return LandAppraisalOffline(PrevBtn, NextBtn);
      case 3:
        return OtherImprovementOfflinePage(PrevBtn, NextBtn);
      case 4:
        return ValueAdjustmentOfflinePage(PrevBtn, NextBtn);
      case 5:
        return LandPropertyAssessmentOfflinePage(PrevBtn, NextBtn);
      case 6:
        return LandSignatoriesOfflinePage(onSAveAll);

      default:
        return Container();
    }
  }
}
