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:unit2/bloc/passo/land/land_boundaries_edit/land_boundaries_edit_bloc.dart';
import 'package:unit2/bloc/passo/land/land_location_edit/land_location_edit_bloc.dart';
import 'package:unit2/model/passo/land_property_boundaries.dart';
import 'package:unit2/model/passo/land_property_loc.dart';
import 'package:unit2/model/passo/land_property_owner.dart';
import 'package:unit2/screens/passo/Land/edit_land/property_owner_info_edit.dart';
import 'package:unit2/utils/text_container.dart';
import 'package:unit2/widgets/error_state.dart';
import 'package:unit2/widgets/passo/custom_button.dart';
import 'package:unit2/widgets/passo/custom_formBuilder_fields.dart';

class LandLocationAndBoundariesEdit extends StatefulWidget {
  Function PrevBtn;
  Function NextBtn;
  LandPropertyOwner land;
  LandLocationAndBoundariesEdit(this.PrevBtn, this.NextBtn, this.land);
  @override
  _LandLocationAndBoundariesEdit createState() =>
      _LandLocationAndBoundariesEdit();
}

class _LandLocationAndBoundariesEdit
    extends State<LandLocationAndBoundariesEdit> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ProgressHUD(
        padding: const EdgeInsets.all(24),
        backgroundColor: Colors.black87,
        indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
        child: BlocConsumer<LandLocationEditBloc, LandLocationEditState>(
          listener: (context, state) {
            if (state is LandLocationEditLoading) {
              final progress = ProgressHUD.of(context);
              progress!.showWithText("Please wait...");
            }

            if (state is LandLocationEditErrorState) {
              final progress = ProgressHUD.of(context);
              progress?.dismiss();
            }
          },
          builder: (context, state) {
            if (state is LandLocationEditLoaded) {
              final landLoc = state.land_loc_edit;
              return BlocConsumer<LandBoundariesEditBloc,
                  LandBoundariesEditState>(
                listener: (context, state) {
                  if (state is LandBoundariesEditLoaded) {
                    final progress = ProgressHUD.of(context);
                    progress?.dismiss();
                  }
                  if (state is LandBoundariesEditErrorState) {
                    final progress = ProgressHUD.of(context);
                    progress?.dismiss();
                  }
                },
                builder: (context, state) {
                  if (state is LandBoundariesEditLoaded) {
                    return FormBuilder(
                      key: landEditKey,
                      initialValue: {
                        'street': landLoc.street,
                        'brgy': landLoc.barangay,
                        'municipality': landLoc.municipality,
                        'province': landLoc.province,
                        'north': state.land_boundaries_edit.north,
                        'south': state.land_boundaries_edit.south,
                        'east': state.land_boundaries_edit.east,
                        'west': state.land_boundaries_edit.west
                      },
                      enabled: true,
                      onChanged: () {
                        landEditKey.currentState!.save();
                        debugPrint(landEditKey.currentState!.value.toString());
                      },
                      autovalidateMode: AutovalidateMode.disabled,
                      skipDisabled: true,
                      child: SingleChildScrollView(
                        child: Padding(
                          padding: const EdgeInsets.all(15.0),
                          child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Container(
                                  margin: const EdgeInsets.only(
                                      left: 0, top: 20, right: 0, bottom: 10),
                                  child: const Text('PROPERTY LOCATION',
                                      style: TextStyle(
                                          fontWeight: FontWeight.bold,
                                          fontSize: 18),
                                      textAlign: TextAlign.left),
                                ),
                                const SizedBox(height: 15),
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceEvenly,
                                  children: <Widget>[
                                    Expanded(
                                        // optional flex property if flex is 1 because the default flex is 1
                                        flex: 1,
                                        child: customTextField(
                                            "No. / Street", "", "street")),
                                    const SizedBox(width: 10.0),
                                    Expanded(
                                        // optional flex property if flex is 1 because the default flex is 1
                                        flex: 1,
                                        child: customTextField(
                                            "Brgy./District", "", "brgy")),
                                  ],
                                ),
                                Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceEvenly,
                                    children: <Widget>[
                                      Expanded(
                                        flex: 1,
                                        child: customTextField(
                                            "Municipality", "", "municipality"),
                                      ),
                                      const SizedBox(width: 10.0),
                                      Expanded(
                                          // optional flex property if flex is 1 because the default flex is 1
                                          flex: 1,
                                          child: customTextField(
                                              "Province/City", "", "province"))
                                    ]),
                                Container(
                                  margin: const EdgeInsets.only(
                                      left: 0, top: 20, right: 0, bottom: 10),
                                  child: const Text('PROPERTY BOUNDARIES',
                                      style: TextStyle(
                                          fontWeight: FontWeight.bold,
                                          fontSize: 18),
                                      textAlign: TextAlign.left),
                                ),
                                Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceEvenly,
                                    children: <Widget>[
                                      Expanded(
                                        flex: 1,
                                        child: customTextField(
                                            "North", "", "north"),
                                      ),
                                      const SizedBox(width: 10.0),
                                      Expanded(
                                          // optional flex property if flex is 1 because the default flex is 1
                                          flex: 1,
                                          child: customTextField(
                                              "East", "", "east"))
                                    ]),
                                Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceEvenly,
                                    children: <Widget>[
                                      Expanded(
                                        flex: 1,
                                        child: customTextField(
                                            "South", "", "south"),
                                      ),
                                      const SizedBox(width: 10.0),
                                      Expanded(
                                          // optional flex property if flex is 1 because the default flex is 1
                                          flex: 1,
                                          child: customTextField(
                                              "West", "", "west"))
                                    ]),
                                const SizedBox(
                                  height: 50,
                                ),
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceEvenly,
                                  children: [
                                    CustomButton(
                                        icon: const Icon(Icons.chevron_left,
                                            color: Colors.white),
                                        onPressed: () {
                                          widget.PrevBtn();
                                        }),
                                    CustomButton(
                                        icon: const Icon(Icons.chevron_right,
                                            color: Colors.white),
                                        onPressed: () {
                                          print(widget.land.id);
                                          var boundaries =
                                              LandPropertyBoundaries(
                                            id: widget.land.id,
                                            north: landEditKey
                                                .currentState?.value['north'],
                                            east: landEditKey
                                                .currentState?.value['east'],
                                            west: landEditKey
                                                .currentState?.value['west'],
                                            south: landEditKey
                                                .currentState?.value['south'],
                                          );
                                          var location = LandPropertyLoc(
                                            id: widget.land.id,
                                            street: landEditKey
                                                .currentState?.value['street'],
                                            barangay: landEditKey
                                                .currentState?.value['brgy'],
                                            municipality: landEditKey
                                                .currentState
                                                ?.value['municipality'],
                                            province: landEditKey.currentState
                                                ?.value['province'],
                                          );

                                          context
                                              .read<LandLocationEditBloc>()
                                              .add(UpdateLandLocationEdit(
                                                  land_loc_edit: location,
                                                  id: widget.land.id!));

                                          context
                                              .read<LandBoundariesEditBloc>()
                                              .add(UpdateLandBoundariesEdit(
                                                  land_boundaries_edit:
                                                      boundaries,
                                                  id: widget.land.id!));

                                          widget.NextBtn();
                                        })
                                  ],
                                )
                              ]),
                        ),
                      ),
                    );
                  }
                  if (state is LandBoundariesEditErrorState) {
                    return SomethingWentWrong(
                      message: onError,
                      onpressed: () {
                        context.read<LandBoundariesEditBloc>().add(
                            LoadLandBoundariesEdit(
                                land_boundaries_edit: LandPropertyBoundaries(),
                                id: widget.land.id!));
                      },
                    );
                  }
                  return Container();
                },
              );
            }
            ;
            if (state is LandLocationEditErrorState) {
              return SomethingWentWrong(
                message: onError,
                onpressed: () {
                  context.read<LandLocationEditBloc>().add(LoadLandLocationEdit(
                      land_loc_edit: LandPropertyLoc(), id: widget.land.id!));
                },
              );
            }
            return Container();
          },
        ),
      ),
    );
  }
}
