import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.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/models/model_model.dart';

void showBrandDialog(BuildContext context, {BrandModel? brand}) {
  final controller = TextEditingController(text: brand?.name);

  showDialog(
    context: context,
    builder: (context) => AlertDialog(
      title: Text(brand == null ? "Nuovo Brand" : "Modifica Brand"),
      content: TextField(
        controller: controller,
        autofocus: true,
        decoration: const InputDecoration(
          labelText: "Nome Brand",
          hintText: "es. Apple, Samsung...",
        ),
        onSubmitted: (_) => _submitBrand(controller, context, brand),
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.pop(context),
          child: const Text("Annulla"),
        ),
        ElevatedButton(
          onPressed: () => _submitBrand(controller, context, brand),
          child: const Text("Salva"),
        ),
      ],
    ),
  );
}

void _submitBrand(
  TextEditingController controller,
  BuildContext context,
  BrandModel? brand,
) {
  if (controller.text.trim().isNotEmpty) {
    context.read<ProductsCubit>().saveBrand(controller.text, id: brand?.id);
    Navigator.pop(context);
  }
}

void showModelDialog(BuildContext context, {ModelModel? model}) {
  final controller = TextEditingController(text: model?.name);

  showDialog(
    context: context,
    builder: (context) => AlertDialog(
      title: Text(model == null ? "Nuovo Modello" : "Modifica Modello"),
      content: TextField(
        controller: controller,
        autofocus: true,
        decoration: const InputDecoration(
          labelText: "Nome Modello",
          hintText: "es. iPhone 15, Galaxy S24...",
        ),
        onSubmitted: (_) => _submitModel(controller, context, model),
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.pop(context),
          child: const Text("Annulla"),
        ),
        ElevatedButton(
          onPressed: () => _submitModel(controller, context, model),
          child: const Text("Salva"),
        ),
      ],
    ),
  );
}

void _submitModel(
  TextEditingController controller,
  BuildContext context,
  ModelModel? model,
) {
  if (controller.text.isNotEmpty) {
    context.read<ProductsCubit>().saveModel(controller.text, id: model?.id);
    Navigator.pop(context);
  }
}

void confirmToggle(BuildContext context, String title, VoidCallback onConfirm) {
  showDialog(
    context: context,
    builder: (context) => AlertDialog(
      title: const Text("Sei sicuro?"),
      content: Text("Stai per cambiare lo stato di: $title"),
      actions: [
        TextButton(
          onPressed: () => Navigator.pop(context),
          child: const Text("Annulla"),
        ),
        TextButton(
          onPressed: () {
            onConfirm();
            Navigator.pop(context);
          },
          child: const Text("Conferma", style: TextStyle(color: Colors.red)),
        ),
      ],
    ),
  );
}
