import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.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/add/add_land.dart';

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

class LandLocationAndBoundariesOffline extends StatefulWidget {
  Function PrevBtn;
  Function NextBtn;
  LandLocationAndBoundariesOffline(this.PrevBtn, this.NextBtn);
  @override
  _LandLocationAndBoundariesOffline createState() =>
      _LandLocationAndBoundariesOffline();
}

class _LandLocationAndBoundariesOffline
    extends State<LandLocationAndBoundariesOffline> {
  @override
  Widget build(BuildContext context) {
    return BlocConsumer<LandPropertyLocationBloc, LandPropertyLocationState>(
      listener: (context, state) {
        // TODO: implement listener
      },
      builder: (context, state) {
        if (state is LandPropertyLocationInitial) {
          return BlocConsumer<LandPropertyBoundariesBloc,
              LandPropertyBoundariesState>(
            listener: (context, state) {
              // TODO: implement listener
            },
            builder: (context, state) {
              if (state is LandPropertyBoundariesInitial) {
                return 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))
                              ]),
                          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: () async {
                                    final tempID =
                                        await SharedPreferences.getInstance();
                                    print(tempID.getInt('landid'));

                                    context
                                        .read<LandPropertyLocationBloc>()
                                        .add(AddLandPropertyLocation(
                                          id: tempID.getInt('landid')! - 1,
                                          landapprDetailsId:
                                              tempID.getInt('landid')!,
                                          assessedById: 'test',
                                          assessedByName: 'test',
                                          dateCreated: 'test',
                                          dateModified: 'test',
                                          street: landKeyOffline
                                              .currentState?.value['street'],
                                          barangay: landKeyOffline
                                              .currentState?.value['brgy'],
                                          municipality: landKeyOffline
                                              .currentState
                                              ?.value['municipality'],
                                          province: landKeyOffline
                                              .currentState?.value['province'],
                                        ));

                                    context
                                        .read<LandPropertyBoundariesBloc>()
                                        .add(AddLandPropertyBoundaries(
                                            id: tempID.getInt('landid')! - 1,
                                            landapprDetailsId:
                                                tempID.getInt('landid')!,
                                            assessedById: 'test',
                                            assessedByName: 'test',
                                            dateCreated: 'test',
                                            dateModified: 'test',
                                            north: landKeyOffline
                                                .currentState?.value['north'],
                                            east: landKeyOffline
                                                .currentState?.value['east'],
                                            south: landKeyOffline
                                                .currentState?.value['south'],
                                            west: landKeyOffline
                                                .currentState?.value['west'],
                                            sketch: 'test'));
                                    widget.NextBtn();
                                  })
                            ],
                          )
                        ]),
                  ),
                );
              }
              return Container();
            },
          );
        }
        return Container();
      },
    );
  }
}
