import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:niogu_app/core/constants/app_font_size.dart';
import 'package:niogu_app/core/enums/item_type.dart';
import 'package:niogu_app/core/widgets/custom_button.dart';
import 'package:niogu_app/core/widgets/custom_text_form_field.dart';
import 'package:sizer/sizer.dart';

class ItemQuantityAmount extends StatefulWidget {
  final dynamic itemType;
  final String name;
  final String? variantName;
  final String remainingStock;
  final String unit;
  final String initialQuantity;
  final String label;
  final Function(String) onPressed;

  const ItemQuantityAmount({
    super.key,
    required this.itemType,
    required this.name,
    this.variantName,
    required this.remainingStock,
    required this.unit,
    required this.initialQuantity,
    required this.label,
    required this.onPressed,
  });

  @override
  State<ItemQuantityAmount> createState() => _ItemQuantityAmountState();
}

class _ItemQuantityAmountState extends State<ItemQuantityAmount> {
  late GlobalKey<FormState> _formKey;
  late TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _formKey = GlobalKey<FormState>();
    _controller = TextEditingController(text: widget.initialQuantity);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final bool isTablet = 100.w >= 600;

    final double maxWidth = 100.w >= 1280
        ? 1200
        : isTablet
        ? 800
        : 400;

    return Dialog(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.w)),
      backgroundColor: Colors.white,
      child: Container(
        padding: EdgeInsets.all(5.w),
        constraints: BoxConstraints(maxHeight: 85.h, maxWidth: maxWidth),
        child: Form(
          key: _formKey,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                "Masukkan Kuantitas",
                style: TextStyle(
                  fontSize: isTablet
                      ? AppFontSize.medium.sp
                      : AppFontSize.small.sp,
                  fontWeight: FontWeight.bold,
                  color: Colors.black87,
                ),
              ),
              SizedBox(height: 2.h),

              Flexible(
                child: SingleChildScrollView(
                  physics: const ClampingScrollPhysics(),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      CustomTextFormField(
                        label: "Nama Produk",
                        controller: TextEditingController(text: widget.name),
                        readOnly: true,
                      ),

                      if (widget.itemType == ItemType.product_variant &&
                          widget.variantName != null &&
                          widget.variantName!.isNotEmpty) ...[
                        SizedBox(height: 1.5.h),

                        CustomTextFormField(
                          label: "Varian Produk",
                          controller: TextEditingController(
                            text: widget.variantName,
                          ),
                          readOnly: true,
                        ),
                      ],

                      SizedBox(height: 1.5.h),

                      CustomTextFormField(
                        label: "Sisa Stok",
                        controller: TextEditingController(
                          text: "${widget.remainingStock} ${widget.unit}",
                        ),
                        readOnly: true,
                      ),

                      SizedBox(height: 2.h),

                      CustomTextFormField(
                        label: widget.label,
                        hint: "Gunakan titik (.), misal 0.5",
                        keyboardType: const TextInputType.numberWithOptions(
                          decimal: true,
                        ),
                        autoFocus: true,
                        controller: _controller,
                        validator: (value) {
                          if (value == null || value.isEmpty)
                            return "${widget.label} belum diisi";
                          if (double.tryParse(value) == null)
                            return "${widget.label} tidak valid";
                          if (double.parse(value) <= 0)
                            return "${widget.label} harus lebih dari 0";
                          return null;
                        },
                      ),

                      SizedBox(height: 1.h),
                    ],
                  ),
                ),
              ),

              SizedBox(height: 2.h),

              CustomButton(
                title: "Ubah",
                onPressed: () {
                  if (!_formKey.currentState!.validate()) return;

                  widget.onPressed(_controller.text.trim());

                  context.pop();
                },
              ),
              SizedBox(height: 1.h),
            ],
          ),
        ),
      ),
    );
  }
}
