import 'package:flutter/material.dart';
import 'package:mc_common_app/extensions/int_extensions.dart';
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/models/advertisment_models/vehicle_details_models.dart';
import 'package:mc_common_app/models/general_models/widgets_models.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/view_models/ad_view_model.dart';
import 'package:mc_common_app/widgets/button/show_fill_button.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';
import 'package:mc_common_app/widgets/txt_field.dart';
import 'package:provider/provider.dart';
import 'package:easy_localization/easy_localization.dart';

class AdDamagePartsSelectionSheet extends StatefulWidget {
  const AdDamagePartsSelectionSheet({Key? key}) : super(key: key);

  @override
  State<AdDamagePartsSelectionSheet> createState() => _AdDamagePartsSelectionSheetState();
}

class _AdDamagePartsSelectionSheetState extends State<AdDamagePartsSelectionSheet> {
  bool checkBoxValue = false;

  @override
  Widget build(BuildContext context) {
    AdVM adVM = context.watch<AdVM>();
    return SizedBox(
      height: MediaQuery.of(context).size.height * 0.85,
      child: Column(
        children: [
          Container(
            margin: const EdgeInsets.all(8),
            height: 8,
            width: 60,
            decoration: const BoxDecoration(color: MyColors.lightTextColor, borderRadius: BorderRadius.all(Radius.circular(20))),
          ),
          12.height,
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              LocaleKeys.damagePartList.tr().toText(fontSize: 24, isBold: true),
            ],
          ),
          8.height,
          TxtField(
            value: adVM.damagePartSearchValue,
            errorValue: "",
            hint: LocaleKeys.searchPart.tr(),
            onChanged: (value) {
              adVM.onSearchChangedForDamagePart(value);
            },
          ),
          8.height,
          Expanded(
            child: ListView.builder(
              shrinkWrap: true,
              itemCount: adVM.vehiclePartsSearchResults.isEmpty ? adVM.vehicleDamageParts.length : adVM.vehiclePartsSearchResults.length,
              itemBuilder: (BuildContext context, int index) {
                VehiclePartModel vehiclePart = adVM.vehiclePartsSearchResults.isEmpty ? adVM.vehicleDamageParts[index] : adVM.vehiclePartsSearchResults[index];

                return Column(
                  children: [
                    // CheckboxListTile(
                    //   value: false,
                    //   visualDensity: VisualDensity.compact,
                    //   contentPadding: EdgeInsets.zero,
                    //   controlAffinity: ListTileControlAffinity.leading,
                    //   title: "Rear Break Light".toText(
                    //     fontSize: 16,
                    //     isBold: true,
                    //   ),
                    //   onChanged: (value) {},
                    // ),
                    InkWell(
                      onTap: () {
                        if (vehiclePart.isSelected!) {
                          return;
                        }
                        adVM.vehicleDamageParts[index].isChecked = !(adVM.vehicleDamageParts[index].isChecked!);
                        setState(() {});
                      },
                      child: Row(
                        children: [
                          SizedBox(
                            height: 40.0,
                            width: 30.0,
                            child: Transform.scale(
                              scale: 1.3,
                              child: Checkbox(
                                value: vehiclePart.isSelected! ? true : vehiclePart.isChecked,
                                activeColor: vehiclePart.isSelected! ? MyColors.lightTextColor : MyColors.primaryColor,
                                onChanged: (value) {
                                  if (vehiclePart.isSelected!) {
                                    return;
                                  }
                                  adVM.vehicleDamageParts[index].isChecked = !(adVM.vehicleDamageParts[index].isChecked!);
                                  setState(() {});
                                },
                              ),
                            ),
                          ),
                          const SizedBox(width: 20),
                          vehiclePart.partName.toString().toText(
                                fontSize: 16,
                                color: vehiclePart.isSelected! ? MyColors.lightTextColor : MyColors.black,
                              )
                        ],
                      ),
                    ),
                    const Divider(thickness: 1),
                  ],
                );
              },
            ),
          ),
          SizedBox(
            width: double.infinity,
            child: ShowFillButton(
              title: LocaleKeys.addDamagePart.tr(),
              onPressed: () {
                for (var value in adVM.vehicleDamageParts) {
                  if (value.isChecked! && !value.isSelected!) {
                    adVM.addNewDamagePartCard(
                      damageCard: VehicleDamageCard(
                        partImageErrorValue: "",
                        partImages: null,
                        damagePartDescription: "",
                        partSelectedId: SelectionModel(selectedOption: value.partName!, selectedId: value.id!, errorValue: ""),
                      ),
                    );
                    value.isChecked = false;
                    value.isSelected = true;
                  }
                }
                Navigator.pop(context);
              },
            ).paddingOnly(bottom: 10),
          ),
        ],
      ),
    ).horPaddingMain();
  }
}
