import 'package:cargocalculaterapp/constants/constants.dart';
import 'package:cargocalculaterapp/core/extension/build_context_extension.dart';
import 'package:cargocalculaterapp/core/functions/base_finctions.dart';
import 'package:cargocalculaterapp/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import '../../theme/app_text_styles.dart';
import '../../utils/app_utils.dart';

class CustomDropDown extends StatelessWidget {
  const CustomDropDown({
    super.key,
    this.name,
    required this.isRequired,
    this.errorText,
    this.isError,
    this.style,
    required this.onChange,
    this.value,
  });

  final String? name;
  final bool isRequired;
  final String? errorText;
  final bool? isError;
  final TextStyle? style;
  final String? value;
  final Function(String?) onChange;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (name?.isNotEmpty ?? false)
              Text(name ?? '', style: style ?? context.text.categoryName),
            AppUtils.kBoxWidth4,
            if (isRequired) SvgPicture.asset("assets/svg/ic_required.svg"),
          ],
        ),
        AppUtils.kBoxHeight8,
        InputDecorator(
          decoration: InputDecoration(
            fillColor: context.color.white10Transparent,
            enabledBorder: OutlineInputBorder(
              borderSide: BorderSide(
                color: context.color.borderColor,
              ),
              borderRadius: AppUtils.kBorderRadius12,
            ),
            contentPadding: AppUtils.kPaddingAll16,
          ),
          child: DropdownButtonHideUnderline(
            child: DropdownButton(
              dropdownColor: context.color.grayBackground,
              hint: Text(AppLocalization.current.warehouse,style: TextStyle(
                color: context.color.secondaryText,
                fontSize: 16,
                fontWeight: FontWeight.w400,
              ),),
              icon: Icon(
                Icons.keyboard_arrow_down_outlined,
                size: 24,
                color: context.color.textColor,
              ),
              isDense: true,
              iconSize: 0,
              value: value,
              items:
                  AppConst.warehouseOptions.keys.map((entry) {
                    return DropdownMenuItem(
                      value: entry,
                      child: Text(
                        Functions.getTranslatedItem(
                          AppConst.warehouseOptions[entry],
                          context,
                        ),
                        style: context.text.profileCategory,
                      ),
                    );
                  }).toList(),
              onChanged: onChange,
            ),
          ),
        ),
        if (isError ?? false) AppUtils.kBoxHeight4,
        if (isError ?? false)
          Text(
            errorText ?? "",
            style: AppTextStyles.timer.copyWith(fontSize: 12),
          ),
      ],
    );
  }
}
