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/offline/offline_passo/land/land_property_boundaries/land_property_boundaries_bloc.dart';
import 'package:unit2/bloc/offline/offline_passo/land/land_property_location/land_property_location_bloc.dart';
import 'package:unit2/screens/offline/passo/land/edit/edit_land.dart';

import '../../../../../model/passo/land_property_boundaries.dart';
import '../../../../../model/passo/land_property_loc.dart';
import '../../../../../model/passo/land_property_owner.dart';
import '../../../../../widgets/passo/custom_button.dart';
import '../../../../../widgets/passo/custom_formBuilder_fields.dart';

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

class _LandLocationAndBoundariesOfflineEdit
    extends State<LandLocationAndBoundariesOfflineEdit> {
  @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<LandPropertyLocationBloc, LandPropertyLocationState>(
          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 SpecificLandPropertyLocationLoaded) {
              final landLoc = state.landpropertylocation;
              return BlocConsumer<LandPropertyBoundariesBloc,
                  LandPropertyBoundariesState>(
                listener: (context, state) {},
                builder: (context, state) {
                  if (state is SpecificLandPropertyBoundariesLoaded) {
                    return FormBuilder(
                      key: landOfflineKeyEdit,
                      initialValue: {
                        'street': landLoc.street,
                        'brgy': landLoc.barangay,
                        'municipality': landLoc.municipality,
                        'province': landLoc.province,
                        'north': state.landpropertyboundaries.north,
                        'south': state.landpropertyboundaries.south,
                        'east': state.landpropertyboundaries.east,
                        'west': state.landpropertyboundaries.west
                      },
                      enabled: true,
                      onChanged: () {
                        landOfflineKeyEdit.currentState!.save();
                        debugPrint(
                            landOfflineKeyEdit.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", TextInputType.text)),
                                    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", TextInputType.text)),
                                  ],
                                ),
                                Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceEvenly,
                                    children: <Widget>[
                                      Expanded(
                                        flex: 1,
                                        child: customTextField(
                                            "Municipality",
                                            "",
                                            "municipality",
                                            TextInputType.text),
                                      ),
                                      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",
                                              TextInputType.text))
                                    ]),
                                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", TextInputType.text),
                                      ),
                                      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", TextInputType.text))
                                    ]),
                                Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceEvenly,
                                    children: <Widget>[
                                      Expanded(
                                        flex: 1,
                                        child: customTextField("South", "",
                                            "south", TextInputType.text),
                                      ),
                                      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", TextInputType.text))
                                    ]),
                                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,
                                            landapprDetailsId: widget.land.id,
                                            assessedById: '1',
                                            assessedByName: 'Cyril',
                                            sketch: 'None',
                                            dateCreated: 'test',
                                            dateModified: 'test',
                                            north: landOfflineKeyEdit
                                                .currentState?.value['north'],
                                            east: landOfflineKeyEdit
                                                .currentState?.value['east'],
                                            west: landOfflineKeyEdit
                                                .currentState?.value['west'],
                                            south: landOfflineKeyEdit
                                                .currentState?.value['south'],
                                          );
                                          var location = LandPropertyLoc(
                                            id: widget.land.id,
                                            landapprDetailsId: widget.land.id,
                                            assessedById: '1',
                                            assessedByName: 'Cyril',
                                            dateCreated: 'test',
                                            dateModified: 'test',
                                            street: landOfflineKeyEdit
                                                .currentState?.value['street'],
                                            barangay: landOfflineKeyEdit
                                                .currentState?.value['brgy'],
                                            municipality: landOfflineKeyEdit
                                                .currentState
                                                ?.value['municipality'],
                                            province: landOfflineKeyEdit
                                                .currentState
                                                ?.value['province'],
                                          );

                                          context
                                              .read<LandPropertyLocationBloc>()
                                              .add(UpdateLandPropertyLocation(
                                                  landPropertyLocation:
                                                      location,
                                                  id: widget.land.id!));

                                          context
                                              .read<
                                                  LandPropertyBoundariesBloc>()
                                              .add(UpdateLandPropertyBoundaries(
                                                  landPropertyBoundaries:
                                                      boundaries,
                                                  id: widget.land.id!));

                                          widget.NextBtn();
                                        })
                                  ],
                                )
                              ]),
                        ),
                      ),
                    );
                  }

                  return Container();
                },
              );
            }
            ;

            return Container();
          },
        ),
      ),
    );
  }
}
