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/bulding/general_description/general_description_bloc.dart';
import 'package:unit2/bloc/passo/bulding/property_info/property_info_bloc.dart';
import 'package:unit2/model/passo/general_description.dart';
import 'package:unit2/model/passo/unit_construct.dart';
import 'package:unit2/screens/passo/Building/edit_building/property_owner_info.dart';
import 'package:unit2/theme-data.dart/form-style.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';

import '../add_building.dart';

class GeneralDescriptionEdit extends StatefulWidget {
  final List<UnitConstruct> unit;
  final int tempId;
  final VoidCallback NextBtn;
  final VoidCallback PrevBtn;

  GeneralDescriptionEdit(this.unit, this.tempId, this.NextBtn, this.PrevBtn);
  @override
  _GeneralDescriptionEdit createState() => _GeneralDescriptionEdit();
}

class _GeneralDescriptionEdit extends State<GeneralDescriptionEdit> {
  final actual_use = [
    "Residential",
    "Agricultural",
    "Commercial",
    "Industrial",
    "Mineral",
    "Timberland",
  ];

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: ProgressHUD(
        padding: const EdgeInsets.all(24),
        backgroundColor: Colors.black87,
        indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
        child: BlocConsumer<GeneralDescriptionBloc, GeneralDescriptionState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            if (state is GenDescLoaded) {
              return FormBuilder(
                key: keys,
                initialValue: {
                  'bldg_permit': state.gendesc.bldgPermit,
                  'date_issued': state.gendesc.dateIssued.toString(),
                  'cct': state.gendesc.cct.toString(),
                  'coc_issued': state.gendesc.certCompletionIssued.toString(),
                  'coo_issued': state.gendesc.certOccupancyIssued.toString(),
                  'date_cnstructed': state.gendesc.dateIssued.toString(),
                  'date_occupied': state.gendesc.dateOccupied.toString(),
                  'bldg_age': state.gendesc.bldgAge.toString(),
                  'no_of_storeys': state.gendesc.noStoreys.toString(),
                  'area_of_1stFl': state.gendesc.area1Stfloor,
                  'area_of_2ndFl': state.gendesc.area2Ndfloor,
                  'area_of_3rdFl': state.gendesc.area3Rdfloor,
                  'area_of_4thFl': state.gendesc.area4Thfloor,
                  'total_area': state.gendesc.totalFloorArea.toString(),
                  'actual_use': state.gendesc.actualUse
                },
                enabled: true,
                onChanged: () {
                  keys.currentState!.save();
                  debugPrint(keys.currentState!.value.toString());
                },
                autovalidateMode: AutovalidateMode.disabled,
                skipDisabled: true,
                child: Expanded(
                  child: SingleChildScrollView(
                    scrollDirection: Axis.vertical,
                    child: Padding(
                      padding: const EdgeInsets.all(20.0),
                      child: Column(
                        children: [
                          Container(
                            margin: const EdgeInsets.only(
                                left: 0, top: 20, right: 0, bottom: 10),
                            child: const Text('GENERAL DESCRIPTION',
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 18),
                                textAlign: TextAlign.left),
                          ),
                          Container(
                            margin: const EdgeInsets.only(
                                left: 0, top: 10, right: 0, bottom: 0),
                            child: FormBuilderDropdown(
                              name: 'bldg_type',
                              autofocus: false,
                              decoration: normalTextFieldStyle(
                                  state.gendesc.bldgKind ?? "Kind of Building",
                                  "Kind of Building"),
                              items: widget.unit
                                  .map((e) => DropdownMenuItem(
                                        value: e,
                                        child:
                                            Text(e.bldgType + '-' + e.building),
                                      ))
                                  .toList(),
                            ),
                          ),
                          customDropDownField(
                              "Actual Use", "", 'actual_use', actual_use),
                          Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: <Widget>[
                                Expanded(
                                  flex: 1,
                                  child: customTextField(
                                      "Bldg. Permit No.", "", 'bldg_permit'),
                                ),
                                const SizedBox(width: 10.0),
                                Expanded(
                                    // optional flex property if flex is 1 because the default flex is 1
                                    flex: 1,
                                    child: customDatTimePicker(
                                        "Certificate of Occupancy Issued ON",
                                        "",
                                        'date_issued'))
                              ]),
                          customTextField(
                              "Condominium Certificate of Title (CCT)",
                              "",
                              'cct'),
                          Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: <Widget>[
                                Expanded(
                                  flex: 1,
                                  child: customDatTimePicker(
                                      "Certificate of Completion Issued ON",
                                      "",
                                      'coc_issued'),
                                ),
                                const SizedBox(width: 10.0),
                                Expanded(
                                    // optional flex property if flex is 1 because the default flex is 1
                                    flex: 1,
                                    child: customDatTimePicker(
                                        "Certificate of Occupancy Issued ON",
                                        "",
                                        'coo_issued'))
                              ]),
                          Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: <Widget>[
                                Expanded(
                                  flex: 1,
                                  child: customDatTimePicker(
                                      "Date Constructed /Completed",
                                      "",
                                      'date_cnstructed'),
                                ),
                                const SizedBox(width: 10.0),
                                Expanded(
                                    // optional flex property if flex is 1 because the default flex is 1
                                    flex: 1,
                                    child: customDatTimePicker(
                                        "Date Occupied", "", 'date_occupied'))
                              ]),
                          Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: <Widget>[
                                Expanded(
                                  flex: 1,
                                  child: customTextField(
                                      "Bldg. Age", "", 'bldg_age'),
                                ),
                                const SizedBox(width: 10.0),
                                Expanded(
                                    // optional flex property if flex is 1 because the default flex is 1
                                    flex: 1,
                                    child: customTextField(
                                        "No. of storeys", "", 'no_of_storeys'))
                              ]),
                          Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: <Widget>[
                                Expanded(
                                  flex: 1,
                                  child: customTextField(
                                      "Area of 1st Floor", "", 'area_of_1stFl'),
                                ),
                                const SizedBox(width: 10.0),
                                Expanded(
                                    // optional flex property if flex is 1 because the default flex is 1
                                    flex: 1,
                                    child: customTextField("Area of 2nd Floor",
                                        "", 'area_of_2ndFl'))
                              ]),
                          Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: <Widget>[
                                Expanded(
                                    flex: 1,
                                    child: customTextField("Area of 3rd Floor",
                                        "", 'area_of_3rdFl')),
                                const SizedBox(width: 10.0),
                                Expanded(
                                    // optional flex property if flex is 1 because the default flex is 1
                                    flex: 1,
                                    child: customTextField("Area of 4th Floor",
                                        "", 'area_of_4thFl'))
                              ]),
                          customTextField("Total Area", "", 'total_area'),
                          SizedBox(
                            height: 50,
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: [
                              CustomButton(
                                icon: const Icon(Icons.chevron_left_rounded,
                                    color: Colors.white),
                                onPressed: () {
                                  {
                                    widget.PrevBtn();
                                  }
                                  ;
                                },
                              ),
                              CustomButton(
                                icon: const Icon(Icons.chevron_right_rounded,
                                    color: Colors.white),
                                onPressed: () {
                                  {
                                    keys.currentState!.save();
                                    var genDescData = GeneralDesc(
                                        id: widget.tempId,
                                        bldgKind: keys
                                                .currentState
                                                ?.value['bldg_type']
                                                ?.building ??
                                            state.gendesc.bldgKind,
                                        strucType: keys
                                                .currentState
                                                ?.value['bldg_type']
                                                ?.bldgType ??
                                            state.gendesc.strucType,
                                        bldgPermit: keys
                                            .currentState?.value['bldg_permit'],
                                        dateIssued: keys
                                            .currentState?.value['coc_issued'],
                                        cct: keys.currentState?.value['cct'],
                                        certCompletionIssued: keys
                                            .currentState?.value['coc_issued'],
                                        certOccupancyIssued: keys
                                            .currentState?.value['coo_issued'],
                                        dateCompleted: keys.currentState
                                            ?.value['date_cnstructed'],
                                        dateOccupied: keys.currentState
                                            ?.value['date_occupied'],
                                        bldgAge: int.tryParse(keys.currentState?.value['bldg_age']),
                                        noStoreys: int.tryParse(keys.currentState?.value['no_of_storeys']),
                                        area1Stfloor: keys.currentState?.value['area_of_1stFl'],
                                        area2Ndfloor: keys.currentState?.value['area_of_2ndFl'],
                                        area3Rdfloor: keys.currentState?.value['area_of_3rdFl'],
                                        area4Thfloor: keys.currentState?.value['area_of_4thFl'],
                                        totalFloorArea: keys.currentState?.value['total_area'],
                                        floorSketch: null,
                                        actualUse: keys.currentState?.value['actual_use']);

                                    context.read<PropertyInfoBloc>()
                                      ..add(UpdateGeneralDesc(
                                          gen_desc: genDescData));

                                    widget.NextBtn();
                                  }
                                  ;
                                },
                              )
                            ],
                          )
                        ],
                      ),
                    ),
                  ),
                ),
              );
            }
            if (state is GenDescErrorState) {
              return SomethingWentWrong(
                message: onError,
                onpressed: () {
                  context.read<GeneralDescriptionBloc>().add(
                      LoadGenDesc(id: widget.tempId, gendesc: GeneralDesc()));
                },
              );
            }
            return Container();
          },
        ),
      ),
    );
  }
}
