import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:intl/intl.dart';
import 'package:searchfield/searchfield.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:unit2/bloc/offline/offline_passo/admin/unit_construction/unit_construction_admin_bloc.dart';
import 'package:unit2/bloc/offline/offline_passo/building/building_and_structure/building_and_structure_bloc.dart';
import 'package:unit2/model/offline/offline_profile.dart';
import 'package:unit2/model/passo/building_and_structure.dart';

import '../../../../../model/passo/unit_construct.dart';
import '../../../../../theme-data.dart/form-style.dart';
import '../../../../../widgets/passo/custom_formBuilder_fields.dart';

// Function to get stored building type
Future<UnitConstruct> getStoredBldgType() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  String? jsonString = prefs.getString('selected_bldg_type');
  if (jsonString != null) {
    Map<String, dynamic> json = jsonDecode(jsonString);
    return UnitConstruct.fromJson(json);
  }
  // Return a default UnitConstruct if no data is found
  return UnitConstruct.defaultConstruct();
}

class AddBuildingAndStructureOffline extends StatefulWidget {
  final OfflineProfile offlineProfile;
  AddBuildingAndStructureOffline(this.offlineProfile);

  @override
  _AddBuildingAndStructureOffline createState() =>
      _AddBuildingAndStructureOffline();
}

class _AddBuildingAndStructureOffline
    extends State<AddBuildingAndStructureOffline> {
  GlobalKey<FormBuilderState> formKey = GlobalKey<FormBuilderState>();
  final focus = FocusNode();

  final DateTime now;
  final String formatter;

  _AddBuildingAndStructureOffline()
      : now = DateTime.now(),
        formatter = DateFormat.yMMMMd('en_US').format(DateTime.now()),
        _unitConstruct = UnitConstruct.defaultConstruct();

  final actual_use = [
    "Residential",
    "Agricultural",
    "Commercial",
    "Industrial",
    "Mineral",
    "Timberland",
  ];
  double _unitBase = 0;
  String _structureType = "";
  double _areaValue = 0;
  double _depRate = 0;
  UnitConstruct _unitConstruct;
  bool _isLoading = true;

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  Future<void> _loadData() async {
    UnitConstruct unitConstruct = await getStoredBldgType();
    setState(() {
      _unitConstruct = unitConstruct;
      _isLoading = false;
    });
  }

  double _totalMarketValue(unitBase, bldgArea) {
    return unitBase * bldgArea;
  }

  double _amountofDepreciation(unitBase, area, depreciation) {
    return (unitBase * area) * depreciation;
  }

  double _adjustedMarketValue(unitBase, area, depreciation) {
    double marketVal = unitBase * area;
    double depAmount = (unitBase * area) * depreciation;
    return marketVal - depAmount;
  }

  void assignSelectedBldgValues() async {
    UnitConstruct? storedBldgType = await getStoredBldgType();
    _unitBase = double.parse(storedBldgType!.unitValue);
    _structureType = '${storedBldgType.bldgType} - ${storedBldgType.building}';

    formKey.currentState!.patchValue({'unit_value': storedBldgType.unitValue});
  }

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<BuildingAndStructureBloc, BuildingAndStructureState>(
      listener: (context, state) {
        // TODO: implement listener
      },
      builder: (context, state) {
        print(state);
        if (state is ShowBldgAndStructuresScreen) {
          return BlocConsumer<UnitConstructionAdminBloc,
              UnitConstructionAdminState>(
            listener: (context, state) {
              // TODO: implement listener
            },
            builder: (context, state) {
              if (state is UnitConstructLoaded) {
                return FormBuilder(
                  key: formKey,
                  onChanged: () {
                    formKey.currentState?.save();
                  },
                  autovalidateMode: AutovalidateMode.disabled,
                  child: SingleChildScrollView(
                    padding: const EdgeInsets.all(2.0),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Container(
                          margin: const EdgeInsets.only(
                              left: 0, top: 10, right: 0, bottom: 0),
                          child: FormBuilderDropdown<String?>(
                            name: 'actual_use',
                            autofocus: false,
                            decoration: normalTextFieldStyle("Actual Use", ""),
                            items: actual_use
                                .map((item) => DropdownMenuItem(
                                      value: item,
                                      child: Text(item),
                                    ))
                                .toList(),
                            onChanged: (value) {
                              assignSelectedBldgValues();
                            },
                          ),
                        ),
                        const SizedBox(
                          height: 10,
                        ),
                        Container(
                          margin: const EdgeInsets.only(
                              left: 0, top: 10, right: 0, bottom: 0),
                          child: SizedBox(
                            height: 45,
                            child: SearchField(
                              enabled: false,
                              itemHeight: 70,
                              suggestions: state.unit
                                  .map((UnitConstruct unit) =>
                                      SearchFieldListItem(
                                          '${unit.bldgType} - ${unit.building}',
                                          item: unit,
                                          child: ListTile(
                                            title: Text(
                                              '${unit.bldgType} - ${unit.building!.toUpperCase()}',
                                              overflow: TextOverflow.ellipsis,
                                            ),
                                          )))
                                  .toList(),

                              validator: FormBuilderValidators.required(
                                  errorText: "This field is required"),

                              searchInputDecoration: normalTextFieldStyle(
                                      _unitConstruct!.bldgType +
                                          ' - ' +
                                          _unitConstruct!.building,
                                      "")
                                  .copyWith(
                                      suffixIcon:
                                          const Icon(Icons.arrow_drop_down)),
                              ////agency suggestion tap
                              focusNode: focus,
                              suggestionState: Suggestion.expand,
                              onSuggestionTap: (unit) {
                                setState(() {});
                                focus.unfocus();
                              },
                            ),
                          ),
                        ),
                        const SizedBox(
                          height: 10,
                        ),
                        Container(
                          child: FormBuilderTextField(
                            name: 'unit_value',
                            decoration: normalTextFieldStyle("Unit Value", ""),
                            validator: FormBuilderValidators.compose([]),
                            keyboardType: TextInputType.phone,
                            onChanged: (value) {
                              // setState(() {
                              //   _areaValue = int.parse(value!);
                              // });
                            },
                          ),
                        ),
                        SizedBox(
                          height: 10,
                        ),
                        Row(
                          children: [
                            Expanded(
                              child: FormBuilderTextField(
                                name: 'dep_rate',
                                decoration: normalTextFieldStyle(
                                    "Depreciation Rate", ""),
                                keyboardType: TextInputType.phone,
                                validator: FormBuilderValidators.compose([]),
                                onChanged: (value) {
                                  setState(() {
                                    _depRate = double.parse(value!);
                                  });
                                },
                              ),
                            ),
                            const SizedBox(
                              width: 5,
                            ),
                            Expanded(
                              child: FormBuilderTextField(
                                name: 'bldg_area',
                                decoration: normalTextFieldStyle("Area", ""),
                                validator: FormBuilderValidators.compose([]),
                                keyboardType: TextInputType.phone,
                                onChanged: (value) {
                                  setState(() {
                                    _areaValue = double.parse(value!);
                                  });
                                },
                              ),
                            ),
                          ],
                        ),
                        const SizedBox(height: 10),
                        const Text('Market Value'),
                        const SizedBox(height: 5),
                        Container(
                          height: 45.0,
                          width: double.infinity,
                          decoration: BoxDecoration(
                            color: Colors.white,
                            border: Border.all(
                              color: Colors.grey,
                              width: 1.0,
                            ),
                            borderRadius: BorderRadius.circular(5.0),
                          ),
                          child: Align(
                              alignment: Alignment.center,
                              child: Text(NumberFormat.currency(
                                      locale: 'en-PH', symbol: "₱")
                                  .format(_totalMarketValue(
                                      _unitBase, _areaValue)))),
                        ),
                        const SizedBox(height: 10),
                        const Text('Amount of Depreciation'),
                        const SizedBox(height: 5),
                        Container(
                          height: 45.0,
                          width: double.infinity,
                          decoration: BoxDecoration(
                            color: Colors.white,
                            border: Border.all(
                              color: Colors.grey,
                              width: 1.0,
                            ),
                            borderRadius: BorderRadius.circular(5.0),
                          ),
                          child: Align(
                              alignment: Alignment.center,
                              child: Text(NumberFormat.currency(
                                      locale: 'en-PH', symbol: "₱")
                                  .format(_amountofDepreciation(
                                      _unitBase, _areaValue, _depRate)))),
                        ),
                        const SizedBox(height: 10),
                        const Text('Adjusted Market Value'),
                        const SizedBox(height: 5),
                        Container(
                          height: 45.0,
                          width: double.infinity,
                          decoration: BoxDecoration(
                            color: Colors.white,
                            border: Border.all(
                              color: Colors.grey,
                              width: 1.0,
                            ),
                            borderRadius: BorderRadius.circular(5.0),
                          ),
                          child: Align(
                              alignment: Alignment.center,
                              child: Text(NumberFormat.currency(
                                      locale: 'en-PH', symbol: "₱")
                                  .format(_adjustedMarketValue(
                                      _unitBase, _areaValue, _depRate)))),
                        ),
                        const SizedBox(height: 10),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Container(
                              width: 120,
                              height: 60,
                              padding: const EdgeInsets.all(8.0),
                              child: ElevatedButton(
                                onPressed: () async {
                                  try {
                                    final tempID =
                                        await SharedPreferences.getInstance();
                                    var bldgStructure = BldgAndStructure(
                                        id: 1,
                                        bldgapprDetailsId: tempID.getInt(
                                            'tempid')!,
                                        // assessedById: widget
                                        //     .offlineProfile.id
                                        //     .toString(),
                                        // assessedByName: widget
                                        //     .offlineProfile.firstName,
                                        // dateCreated: formatter,
                                        // dateModified: 'none',
                                        bldgType: _structureType,
                                        structType: _structureType,
                                        description: 'None',
                                        buccPercentage: '1',
                                        actualUse: formKey.currentState?.value[
                                                'actual_use'] ??
                                            "",
                                        floorCount: '1',
                                        bldgArea: _areaValue.toString(),
                                        unitValue: _unitBase.toString(),
                                        depRate: _depRate.toString(),
                                        marketValue: _totalMarketValue(
                                                _unitBase, _areaValue)
                                            .toString(),
                                        depAmount: _amountofDepreciation(
                                                _unitBase, _areaValue, _depRate)
                                            .toString(),
                                        adjustedMarketValue:
                                            _adjustedMarketValue(_unitBase,
                                                    _areaValue, _depRate)
                                                .toString(),
                                        genCode: "5TH");
                                    context
                                        .read<BuildingAndStructureBloc>()
                                        .add(AddBuildingAndStructure(
                                            bldgAndStructure: bldgStructure));
                                  } catch (e) {
                                    print(e);
                                  }
                                },
                                style: ElevatedButton.styleFrom(
                                  backgroundColor: Colors.black,
                                ),
                                child: const Text("Submit"),
                              ),
                            ),
                            const SizedBox(
                                width:
                                    5), // Use SizedBox for horizontal spacing in a Row
                            Container(
                              width: 120,
                              height: 60,
                              padding: const EdgeInsets.all(8.0),
                              child: ElevatedButton(
                                onPressed: () {
                                  context
                                      .read<BuildingAndStructureBloc>()
                                      .add(const LoadBuildingAndStructure());
                                },
                                style: ElevatedButton.styleFrom(
                                  backgroundColor: Colors.black,
                                ),
                                child: const Text("Cancel"),
                              ),
                            ),
                          ],
                        )
                      ],
                    ),
                  ),
                ); // Use your actual widget
              }
              return Container();
            },
          );
        }
        return Container();
      },
    );
  }
}
