import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/theme/theme.dart';
import 'package:flux/features/master_data/products/blocs/product_cubit.dart';
import 'package:flux/features/master_data/products/models/brand_model.dart';
import 'package:flux/features/master_data/products/ui/product_dialogs.dart';
import 'package:flux/features/master_data/products/ui/round_action_button.dart';

class BrandSelector extends StatelessWidget {
  final ProductState state;
  const BrandSelector(this.state, {super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: context.background,
        borderRadius: BorderRadius.circular(16),
        border: Border.all(color: context.accent.withValues(alpha: 0.1)),
      ),
      child: Row(
        children: [
          Expanded(
            child: DropdownButtonFormField<BrandModel>(
              initialValue: state.selectedBrand,
              //value: state.selectedBrand,
              decoration: const InputDecoration(
                labelText: "Seleziona Brand",
                prefixIcon: Icon(Icons.branding_watermark_outlined),
              ),
              items: state.brands.map((brand) {
                return DropdownMenuItem(value: brand, child: Text(brand.name));
              }).toList(),
              onChanged: (brand) =>
                  context.read<ProductsCubit>().selectBrand(brand),
            ),
          ),
          const SizedBox(width: 16),
          // Pulsanti rapidi Brand
          RoundActionButton(
            icon: Icons.add,
            onTap: () => showBrandDialog(context),
            tooltip: "Nuovo Brand",
          ),
          if (state.selectedBrand != null) ...[
            const SizedBox(width: 8),
            RoundActionButton(
              icon: Icons.edit_outlined,
              onTap: () => showBrandDialog(context, brand: state.selectedBrand),
              tooltip: "Modifica Brand",
            ),
          ],
        ],
      ),
    );
  }
}
